1

Given a directory full of .sam files, for each file:

  1. if column3=blah and 451000 =< column4 <= 468999, write line to file_ribos.sam

  2. otherwise, write line to file_non_ribos.sam

Example Input

# file_1.sam    
abc  123  blah  451200
abc  123  blah  450999

Example Output

# file_1_ribos.sam
abc  123  blah  451200

# file_1_non_ribos.sam
abc  123  blah  450999

My code seems to be failing. What am I doing wrong?

for file in *.sam ; do
  awk -F"\t" '
   {if($3 == "blah" && $4 >= "451000" && $4 <= "468999") {
     {print $0} > "$(basename "$file" .sam)_ribos.sam";}
    else {print $0} > "$(basename "$file" .sam)_non_ribos.sam";}
  ' $file;
done
3
  • 1
    Please edit some sample input into your question...
    – jasonwryan
    Mar 12, 2015 at 17:11
  • 2
    This is not a bioinformatics site, most people here will have no idea what .sam files are. Please include an example of the files you are parsing when asking questions. You also need to show us your desired output and explain exactly how your "code is failing".
    – terdon
    Mar 12, 2015 at 17:14
  • 1
    Extra { unclosed in your code. Just before first print. Note that @Janis solution should work.
    – Archemar
    Mar 12, 2015 at 17:52

1 Answer 1

4

I've refactored your code a bit...

for file in *.sam ; do
  awk -v basename="$(basename $file .sam)" '
    { non = ($3 == "blah" && $4 >= 451000 && $4 <= 468999) ? "" : "_non"
      outfile = basename non "_ribos.sam"
      print > outfile
    }
  ' "$file"
done
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .