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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read a line from a file?

Status
Not open for further replies.

jjrrgg01

Programmer
Sep 25, 2001
7
AR
Hi, everybody
I am new in TEK-TIPS Forums and new in the aix-unix world.
Now I am making my first programms in ksh, and I need to know the way to read just a line of a file, just only using aix-unix commands. I am rading and reading books but I do not find a way to do this. If someone can help me thank you very much. :)

jjrrgg01.
 
That's a bit of an open question really - do you want to read the top line, the last line, all lines one at a time or a specific line in a file??

If you want to do it simply (but slowly) using only shell scripting then something like the following should do the trick...

#!/usr/bin/ksh
FILE="MyFile"
j=1
numLines=$( wc -l $FILE | tr -s " " )
numLines=$( echo $numLines | cut -d" " -f1 )
while [ $j -le $numLines )
do
echo $( head -$j $FILE | tail -1 )
j=$(( $j + 1 ))
done

...whilst this does read in (and display) one line at a time it's not the most efficient - but it's relatively easy to understand (although you may need to check out the tr command?).

The easiest way to do something like this is to learn and use awk. Get hold of "sed and awk" by O'Reilly and associates and work your way through - it won't be wasted time. That and "UNIX In A Nutshell" by O'Reilly seem to have a permanent place on most Sys Admins desks no matter how experienced they are!

To prove a point the same thing in awk would be written (from the command line)..

awk '{print $0}' MyFile

...slightly simpler I'm sure you'll agree ;-)

What are you trying to do with the lines of the file anyway? If you need to read them one at a time, awk really is the only way to go.

Hope it helps.
Dave V.
 
Perhaps I'm misinterpreting this, but if you're just looking for the line in a file which contains a certain piece of text, just use

grep <text required> filename

to return all lines containing the text. Alternatively, to read one line at a time, you could use:

for line in `cat filename`
do
echo $line
done

but that's just about the same as cat-ing the file anyway! Good luck.
 
You could well be right Ken, but then I always was the sort of person to take the back of the telly to see what was wrong before realising the fuse had blown in the plug :)

Dave V.
 
vickersdc and KenCunningham thank you very for your help, the both answers open my mind to get a solution for my trouble. The reason why i need to read a file line by line is because I need to check a backup log file, I need to know if it done or not, and then alarm me, the reason is because I work at night and I am alone, and I can not be infront of my PC waiting the backup end, other reason is because I am not authorized to modify the back up .sh, so I need a &quot;backup end alarm&quot; meanwhile I am doing other josbs. Again Thank you very much for your help.

Julio G. (jjrrgg01)

 
Well in that case you could probably make better use of the tail command as follows:

tail -f FILENAME

This opens the file for reading and whenever a new line is appended to the file you can see the change immediately. Try it out - use two windows and create a new file, e.g MyFile, then tail it...

touch MyFile
tail -f MyFile

...now from the other window add a new line to the file...

echo &quot;BACKUP HAS FINISHED&quot; >> MyFile

...and you'll see the newly appended line appear in the file from the 'tail -f' window..

Hope it helps.
Dave V.
 
vickersdc (IS/IT--Manageme), your tips too helpfull, again thank you very much.

Julio G. (jjrrgg01)
 
May be you can use
[tt]
awk 'NR==[red]number[/red] {print}' /your/file
[/tt]
if you know the line number.

I hope it works...
Unix was made by and for smart people.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top