Shell script utility to read a file line line
Posted in File-management
#!/bin/bash # Shell script utility to read a file line line. # Once line is read it can be process in processLine() function # You can call script as follows, to read myfile.txt: # ./readline myfile.txt # Following example will read line from standard input device aka keyboard: # ./readline # ----------------------------------------------- # Copyright (c) 2005 nixCraft <http://cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------- # User define Function (UDF) processLine(){ line="$@" # get all args # just echo them, but you may need to customize it according to your need # for example, F1 will store first field of $line, see readline2 script # for more examples # F1=$(echo $line | awk '{ print $1 }') echo $line } ### Main script stars here ### # Store file name FILE="" # Make sure we get file name as command line argument # Else read it from standard input device if [ "$1" == "" ]; then FILE="/dev/stdin" else FILE="$1" # make sure file exist and readable if [ ! -f $FILE ]; then echo "$FILE : does not exists" exit 1 elif [ ! -r $FILE ]; then echo "$FILE: can not read" exit 2 fi fi # read $FILE using the file descriptors # Set loop separator to end of line BAKIFS=$IFS IFS=$(echo -en "\n\b") exec 3<&0 exec 0<$FILE while read line do # use $line variable to process line in processLine() function processLine $line done exec 0<&3 # restore $IFS which was used to determine what the field separators are BAKIFS=$ORIGIFS exit 0
Download - Email this to a friend - Printable version
Is your site working? Monitor Your Web Site 24/7. Get SMS alerts on server downtime! Free 30-day trial including 20 SMS!
Related Other Helpful Shell Scripts:
- Script utility to read a file line line and separate line in fields
- Shell Script To Simulate UNIX More Command To Display Only The 15 Lines At A Time
- Shell Script To Delete All Even Numbered Line From a Text File
- Shell Script To Write Odd and Even Line To Respective Files
- Shell Script To Count English Language Articles Such As 'A', 'An' and 'The'
Discussion on This Shell Script:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: args, command line argument, exec, f1, fi fi, keyboard, loop method, readline, standard input device, while loop ~ Last updated on: October 31, 2008


good…….
thanks!
Hey this worked perfectly. I used it to automatically patch my sun system. I used smpatch analyze > patch.txt then used your script to read in the outputed patch numbers.
Excellent! very illustrative,
thanks for your contribution! ;-)
bad news for everybody who has lines with more than one space between words.
@michelek,
This can be easily fixed by adding following code before and after while loop:
while / for / untile loop is using $IFS to determine what the field separators are. I’ve modified this to use end of line. Here is final modified code:
The script has been updated to fix this issue. Just download updated version.
HTH
Thank you so much, this is just what I was looking for :)