Thursday, August 1, 2013

HOW TO READ A FILE LINE BY LINE USING SHELL SCRIPT


A simlpe shell script to read any file line by line 
where the name of file is passed as the argument:
~]# touch navneet.sh
~]# chmod 755 navneet.sh
~]# vi navneet.sh
 
 file_name=$1                                         #positional parameter $1 is passed as the argument to the script
flag=0                                                       #initialize the variable read the file
cat $file_name | while read LINE       #pipe the content of file to while loop
do  
let flag++                                                #post increment of flag counter
echo "$flag $LINE"                              #display each line with line number
done                                                      #termination of loop 
esc:wq                                                  #save file

The above script displays the content of the file name entered by line.

0 comments:

Post a Comment