2

I am trying use a for loop for multiple files in my directory with a pipe command,but it does not seem to work. When am running the same command on a single file it seems to work. Where am I getting it wrong?

for x in *summary-FDR0.05 ; do sort -t $'\t' -k8,8rn $x | head -n 50000 | sortBed -i > sorted_top_50k_$x.bed; done

All my files end with summary-FDR0.05. When I run

sort -t $'\t' -k8,8rn sample13-summary-FDR0.05 | head -n 50000 | sortBed -i > sorted_top_50k_S_13_O1_122*K27ac.bed

This seems to work well. May I know where I am getting it worng

Error:

sort: multi-character tab `$\\t'

Thanks

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
ivivek_ngs
  • 917
  • 3
  • 10
  • 28
  • 1
    Looks *vaguely* like you are using `sh` instead of `bash` when you get the error message. – tripleee Feb 10 '16 at 17:08
  • but how do I get this rectified, does not posix compatibility allow to run `sh` under `bash` – ivivek_ngs Feb 10 '16 at 17:15
  • See http://stackoverflow.com/questions/26717870/why-does-bin-sh-behave-differently-to-bin-bash-even-if-one-points-to-the-other – tripleee Feb 10 '16 at 17:37
  • 2
    You can easily run `sh` code in Bash but you appear to want to do the opposite. Either write portable code (no `$'\t'` then, for example) which works in any POSIX shell; or write Bash code, and run it in Bash only. – tripleee Feb 10 '16 at 17:40
  • I am unable to solve it. any leads? – ivivek_ngs Feb 10 '16 at 18:29
  • How are you running the script? Does it have a shebang line? Does it work if you run it with `bash filename`? – tripleee Feb 10 '16 at 18:31
  • Yes it does have for `#!/bin/sh` . this fails. I also did `#!/bin/bash`. But fails again. I do not want to run it will file name. All the files are in a directory so I want to loop all the files in the directory and while each file is picked up , I want to execute the sorting in tab delimited file with column 8 and then take top 50k rows in each file and since it is bed12 format file want to use sortBed command and output finally. So the piping was done to do all at one go. – ivivek_ngs Feb 10 '16 at 18:40
  • It certainly should not fail with `#!/bin/bash`. Can you show reproduction steps for that failure? – Charles Duffy Feb 10 '16 at 19:03
  • 1
    BTW, there are other bugs in here which http://shellcheck.net/ will identify for you automatically. – Charles Duffy Feb 10 '16 at 19:04

1 Answers1

3

For POSIX compatibility, replace $'\t' with "$(printf "\t")".

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thanks a lot. This works. I was not able to understand what should be the POSIX compatibility in this case. Thanks a lot – ivivek_ngs Feb 10 '16 at 18:43
  • 1
    If this solved your problem, please click the hollow tick mark so it turns green. That way, this question will no longer come up as unresolved. Thanks. – tripleee Feb 11 '16 at 04:21