2

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?

3
  • By the way, you can safely omit {print $0} as that is what awk does by default anyway. Commented Jun 3, 2017 at 10:40
  • Why? Why not just have awk read rpm.txt and then search for 1007 in U25/test.csv, etc. in one invocation of the tool? No cd-ing or shell loops required. Commented Jun 3, 2017 at 14:08
  • That's a great idea! Would you be able to help me translate that to a command? I know it would probably use awk -v, but not sure of the final command. Commented Jun 3, 2017 at 18:07

2 Answers 2

2

What about

while IFS=, read dir num; do
    awk -v num="$num" ... '$1 == num ... '  <"$dir"/test.csv
done <rpm.txt >x
Sign up to request clarification or add additional context in comments.

1 Comment

I am not able to get this to work, what am I required to put in the . . . in your command?
0

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

1 Comment

This works for me! Would you be able to explain your command a little more to me? Particularly !(.*),(.*)!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.