Reputation: 4191
$ cat ff
./res/values/strings.xml:1293: <string name="sub1LineNumber">Sub1 Number :</string>
./res/values/strings.xml:1302: <string name="xdivert_partial_set">XDivert has been set only for 1st subscription</string>
./res/values/strings.xml:1870: <string name="set_sub_1">Subscription 1: </string>
./res/values/strings.xml:1860: <string name="sub_1">SUB 1</string>
for f in $(cat ff |awk -F : '{print $1" +" $2}'); do echo $f; done
./res/values/strings.xml
+1293
./res/values/strings.xml
+1302
./res/values/strings.xml
+1870
./res/values/strings.xml
+1860
I want to print $1 and $2 in format $1 +$2 in one line, but it print above, what's wrong?
Upvotes: 0
Views: 1341
Reputation: 4502
The delimeter for for
is any space, so the space in the awk output will be treated as a separate value.
Instead of a for loop, you can read lines of output at a time with the while read
construct. For example,
awk -F : '{print $1 " +" $2}' ff | while read f; do
echo "$f"
done
Upvotes: 1
Reputation: 753765
awk
doesn't print two lines; your echo
prints the two lines because there's a space between the file name and the line number so the for
loop sees the file name as one value for f
and then the line number as a second value, and so on.
You also don't need to use cat
; awk
can read files too.
For the shown loop body, you can write:
awk '{print $1 " +" $2}' ff
For a more general loop body, you're best off avoiding the space:
for f in $(awk '{print $1 "+" $2 }' ff)
do
...whatever...
done
Upvotes: 1
Reputation: 4353
You don't need the for
wrapper around the awk
command.
If you just run the awk
command, you'll get the output you want:
$ cat ff | awk -F : '{print $1 " +" $2}'
./res/values/strings.xml +1293
./res/values/strings.xml +1302
./res/values/strings.xml +1870
./res/values/strings.xml +1860
What's happening is that the $(...)
wrapper and the for
loop are causing each space-separated word in your awk
output to become a separate input to the loop. Remove the loop, and remove the problem.
Upvotes: 7