bash - Loop for change the column - how not mistake $ of column with $i -
i make loop change columns in awk condition. however, $ symbol making mistake replacement "i". idea how fix it?
#!/bin/bash in {2..5} awk '$$i>=10 && $$i<=20' permut1.txt >> out.txt done
input:
abc 1 1 2 3 4 bbb 0 1 2 0 1 ccc 1 1 0 2 2 ddd 0 1 3 1 3 fff 15 15 4 15 15 ggg 15 15 15 15 15
i want output:
ggg 15 15 15 15 15
in awk, $
prefix operator argument must non-negative integer. that's quite different meaning of $
in bash.
the easiest way pass variable bash awk use -v var=value
command line option in awk command:
awk -v field=2 '$field >= 10 && $field <= 20' permut1.txt
the above print lines second field between 10 , 20. iterate in bash multiple scans of data, each 1 scanning different column:
for in 2 3 4; awk -v field=$i '$field >= 10 && $field <= 20' permut1.txt done
but suspect trying iterate in awk on fields, , print lines satisfy 3 tests. again, fact awk $
operator can make relatively simple. awk feature simplifies logic next
command, reads next input line , restarts pattern matching loop. makes easy require 3 tests match:
awk '{ (field = 2; field < 5; ++field) { if ($field < 10 || $field > 20) next; } # can here if none of fields outside # range. $0 entire line. print $0; }' permut1.txt
because default pattern action precisely print $0
, can shorten script:
awk '{ (field = 2; field < 5; ++field) if ($field < 10 || $field > 20) next; } } 1' permut1.txt
the 1
@ end condition true, no action (or, in other words, default action); if preceding rule doesn't execute next
command of fields, 1
condition executed, , default action cause line printed.
Comments
Post a Comment