Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

February 9, 2013

Unix Shell read file in line and parse it

Read file in line:
while read fname
do
  echo $fname
done < /etc/passwd
This snippet means printout file and the result of each row is like the following one:

: as separator, the 1st column is user name, the 6th column is home directory
list:x:38:38:Mailing List Manager:/var/list:/bin/sh
If we want to printout the 1st & 6th column, we need use parser.

then do as follows:
while read fname
do
  echo "$fname"|cut -d: -f1,6
done < /etc/passwd
-d:define separator as :, -f1,6: designate the 1st & 6th column.

there is another efficient way -- using awkfilter
while read fname
do 
   echo $fname|awk -F:'{print $1,$6}'
done < /etc/passwd