Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pipe statement in While Loop

Status
Not open for further replies.

HopeLessGuy

Technical User
Jun 9, 2003
10
US
(Korn Script on Sun Solaris)

I currently have a while loop which reads the file till EOF. The file contains file names that are to be searched. In the while statement, I noticed that korn shell does not treat the pipe, | , as it original is used.

For example -

while read tmpFiles
do
i=$i+1
tmpArray=$tmpFiles
fName="${tmpArray}"

grep -c "yo" ${fName%% *} | awk -F: '$2 == 3 { print $1 }'

done < &quot;tmpList.dat&quot;

That's just rushed code, shouldn't be taken into account :p But I noticed, that while the loop executes. It will read the right side of the pipe ( | ) then read &quot;tmpList.dat&quot; then read the left side of the ( | ). Therefore the awk is useless since it doesn't receive the exact information from grep due to the order of execution. If I were to use the grep | awk out of the loop, it works. Any ideas?

Edit - I originally posted this in the Sun - Solaris Forum... had no clue there was a Unix Scripting Forum.
 
the grep, awk and | are behaving as expected.

It's the output from the grep that is causing the problem.

grep will return the count of occurences in pattern, or the pattern, or the filename depending on the option. BUT it will only return 1 token.

Try this:-

[[ $(grep -c &quot;yo&quot; ${fName##* }) -gt 0 ]] &&
{
print -- $fName| awk '$2 == 3 { print $1 }'
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top