Artificial intelligent assistant

awk directory of files 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

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

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 0efdbcf1a0dd0228a0407899c009490d