user3313178 user3313178 - 1 year ago 52
Bash Question

Removing tab at end of a row in mac

I have the following tab-separated input file. I want to remove the trailing tab at the end of the line and do that "in file" so that I don't have to output the modified data to a new file. The reason for this is that there is a lot of data so I don't want to be inefficient and read in data and then write it out again.

Hybridization REF Gene_Symbol Chromosome Genomic_Coordinate TCGA-13-A5FT-01A-11D-A409-05 TCGA-13-A5FU-01A-11D-A409-05 TCGA-29-A5NZ-01A-11D-A409-05 TCGA-3P-A9WA-01A-11D-A409-05 TCGA-59-A5PD-01A-11D-A409-05 TCGA-5X-AA5U-01A-11D-A409-05 TCGA-OY-A56P-01A-12D-A409-05 TCGA-OY-A56Q-01A-11D-A409-05 TCGA-VG-A8LO-01A-11D-A409-05 TCGA-WR-A838-01A-12D-A409-05
cg00000029 RBL2 16 53468112 0.162232896986279 0.191627667901702 0.0712181967886229 0.0797617926225958 0.134907151266991 0.0541415985613948 0.0898579298345672 0.037865566345129 0.0681542463965581 0.101053013486289
cg00000108 C3orf35 3 37459206 NA NA NA NA NA NA NA NA NA NA
cg00000109 FNDC3B 3 171916037 NA NA NA NA NA NA NA NA NA NA

To do this, I've been using variations of:

sed -i '' 's/\t\n/\n/g' input.txt

But this doesn't even remove the trailing tab. I'm using a Mac OS X so no GNU. What is the solution?


I have posted the text file here

Answer Source

Any answers that assume \t will be interpreted directly by sed are assuming GNU sed, which you don't have in OSX. You do however have bash, so you have an easy printf conversion for your script:

$ sed -i '' -e $'s/\t$//' input.txt

The script in this case is being quoted with quotes that expand backslash escape sequences. Note that this is a BASH feature, which we're using to put stuff more sensibly into sed.

Comparing this to the variant of the script in your question, I've put a dollar sign into the search regex to denote the end of the search string -- this script basically says:

  • s - substitute
  • /\t$ - any tab that is at the end of the line,
  • // - with an empty string.

We don't need the g ("global") option for the substitution, because sed will automatically apply the substitution to each line of your file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download