aram
aram

Reputation: 23

using awk command in for/while loop

This is the content of my input file(rpm.txt).

U25,1007
U27,1269

I would like my code to use this file (rpm.txt) as input, change directory to U25, and search for 1007 in U25/test.csv, and print all columns in test.csv that match 1007; and repeat for every line.

This is the code for this, without the loop:

cat U25/test.csv | awk -F ',' '$1 == 1007 {print $0}' > x
cat U27/test.csv | awk -F ',' '$1 == 1269 {print $0}' >> x

Would anyone be able to help me write this as a loop?

Upvotes: 2

Views: 968

Answers (2)

perreal
perreal

Reputation: 97918

Some stupid way of doing this without any loops, by creating commands like grep "^1007," U25/test.csv and feeding them to shell:

sed 's!\(.*\),\(.*\)!grep "^\2," \1/test.csv!' input | sh

Upvotes: 0

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19305

What about

while IFS=, read dir num; do
    awk -v num="$num" ... '$1 == num ... '  <"$dir"/test.csv
done <rpm.txt >x

Upvotes: 2

Related Questions