0

I have a file with this strings

select chrom,chromStart,chromEnd,name from snp147 where name="rs12414460      ";
select chrom,chromStart,chromEnd,name from snp147 where name="rs12456              ";
select chrom,chromStart,chromEnd,name from snp147 where name="rs12434212334               ";

I want to delete the spaces in the name field.

How can I obtain this:

select chrom,chromStart,chromEnd,name from snp147 where name="rs12414460";
select chrom,chromStart,chromEnd,name from snp147 where name="rs12456";
select chrom,chromStart,chromEnd,name from snp147 where name="rs1243421111111";

Please Help me

I tried: cut test.txt | cut -d ' ' -f1,2,3,4,5,6 but it doesn't work

codeforester
  • 39,467
  • 16
  • 112
  • 140
  • 2
    Possible duplicate of [How to remove trailing whitespaces with sed?](http://stackoverflow.com/questions/4438306/how-to-remove-trailing-whitespaces-with-sed) – Benjamin W. Apr 28 '17 at 18:35
  • Define "column". Try to provide a robust definition such that the last column of your sample input is not the string `";` – William Pursell Apr 29 '17 at 15:17

1 Answers1

2

This is more a job for sed than for bash.

sed 's/ *";/";/' test.txt

or (as a demonstration with a here-document):

sed 's/ *";/";/'  <<EOF
> select chrom,chromStart,chromEnd,name from snp147 where name="rs12414460      ";
> select chrom,chromStart,chromEnd,name from snp147 where name="rs12456              ";
> select chrom,chromStart,chromEnd,name from snp147 where name="rs12434212334               ";
> EOF
select chrom,chromStart,chromEnd,name from snp147 where name="rs12414460";
select chrom,chromStart,chromEnd,name from snp147 where name="rs12456";
select chrom,chromStart,chromEnd,name from snp147 where name="rs12434212334";
Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31
  • Should work, but it only prints the changes to your sceen and does not actually change the contents of the file. If you want it to actually change the file, then use the -i flag with sed: `sed -i ...` – kkeller Apr 29 '17 at 13:13
  • Yea I know it but it doesn't change nothing in the same way :( – Giacomo Callegaro Apr 29 '17 at 13:49