Operating System - HP-UX
1832220 Members
2412 Online
110041 Solutions
New Discussion

Re: KSH script - little help?

 
SOLVED
Go to solution

KSH script - little help?

Could some please help me with a little problem I am having. I have a file, foo, that I would like to go through with a 'for' loop and interrogate it line by line looking for a certain string. Here's the problem I am having. If the line has three words on it:
200 Records Loaded
when I do my for i in `cat foo` ,
it returns 200, Records, and Loaded as three separate values instead of returning the whole line to me at once. What I want to do is get the whole line at once, see if it contains the word Loaded, and then echo the line out to a report. Any suggestions on how I can do this? Thank you in advance for your help.
5 REPLIES 5
Sachin Patel
Honored Contributor
Solution

Re: KSH script - little help?

Here is what I uses.

cat foo | while read line ;
do
echo $line
done

Sachin
Is photography a hobby or another way to spend $

Re: KSH script - little help?

PERFECT!!! Thank you so much. That would be the 10:-)
Ricardo Bassoi
Regular Advisor

Re: KSH script - little help?

Hi,

One of the solutions is:

#!/bin/ksh
cat foo | while read line;do
echo " String found : $line" >> output.txt
done
exit 0;

Regards,

Bassoi
If you never try, never will work
A. Clay Stephenson
Acclaimed Contributor

Re: KSH script - little help?

You could make this more efficient by doing the search on the front-end:

grep "target string" myfile | while read X
do
echo "X=\${X}\""
done

This would return only the lines that contain the target string.
If it ain't broke, I can fix that.
Fred Martin_1
Valued Contributor

Re: KSH script - little help?

I frequently use awk to avoid that problem, since awk reads entire lines by default. Sound like the readline thing works well though.
fmartin@applicatorssales.com