Reputation: 1481
Straight to the point: I need to do the following (psuedo-code):
if [ -f <file_that_exists> ]
then
while read
do
awk '{print "do stuff for " $1}' THEN immediately below it awk '{print "do stuff for" $2}'
Then continue to next line
The file I'm trying to parse has two columns (host pairs if you will):
host_1 host_2
host_3 host_4
What I need the output to look like is the following:
Do stuff for host_1
Do stuff for host_2
Do stuff for host_3
Do stuff for host_4
What I'm trying now looks like:
Do stuff for host_1
Do stuff for host_3
Do stuff for host_5
Do stuff for host_7
then
Do stuff for host_2
Do stuff for host_4
Do stuff for host_6
Do stuff for host_8
I'm not sure if I'm being very clear, so if you need further clarification please let me know.
Thanks!
Upvotes: 0
Views: 243
Reputation: 246774
This is a case where you want to use a for-loop to iterate over the words in your file:
for host in $(< filename); then
Do something with $host
done
or, read 2 words from each line
while read host_a host_b; do
Do something with $host_a
Do something with $host_b
done < filename
Upvotes: 0
Reputation: 13994
I believe what you're aiming for can be done in a single awk
statement:
awk '{ print "Do stuff for " $1; print "Do stuff for " $2 }' filename
Or alternatively:
awk '{ printf "Do stuff for %s\nDo stuff for %s\n", $1, $2 }' filename
No need to use the shell to read from the file; just pass the file name to awk
directly. The key to the above solution is that awk
will read from the file, record by record (here: line by line), and have each field (here: host_1, host_2, etc.) in that line be encoded as $1 and $2. If you had a variable number of fields in each line, the solution would be a bit different (possibly involving a loop within awk
) but the above is the simple case.
Upvotes: 3