About

Script Categories

Script utility to read a file line line and separate line in fields

Posted in File-management

Script utility to read a file line line version 2. This is simpler version of readline script, it also demonstrate how to process text data file line by line and then separate line in fields, so that you can process it according to your need.

#!/bin/bash
#
# Shell script utility to read a file line line version 2
# This is simpler version of readline script
# This script also demonstrate how to process data file
# line by line and then separate line in fields, so that
# you can process it according to your need.
#
# You can call script as follows
# ./readline2
# -----------------------------------------------
# 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.
# -------------------------------------------------------------------------
 
### Main script stars here ###
# Store file name  here
FILE="/etc/passwd"
# field seprator, default is :, you can use blank space or
# other character, if you have more than one blak space in
# input line then use awk utility and not the cut :)
FS=":"
 
while read line
do
	# store field 1
	F1=$(echo $line|cut -d$FS -f1)
	# store field 2
	F2=$(echo $line|cut -d$FS -f6)
	# store field
	F3=$(echo $line|cut -d$FS -f7)
	echo "User \"$F1\" home directory is $F2 and login shell is $F3"
done < $FILE
 

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:

Discussion on This Shell Script:

  1. ghostdog74 Says:

    you can use IFS

    IFS=":"
    while read a b c d e f g
    do
    echo $a $b #and so on
    done < /etc/passwd

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!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tags: , , , , , , , , , , , , ~ Last updated on: April 9, 2008