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

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
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • Tomer Jun 14, 2010 @ 8:00

    Thanks for you clear and exact examples

  • ravinder Jun 6, 2010 @ 17:13

    Hi Ravinder this side, the script provide by you is quite usful for me thanx alot.

    I need some more help regarding this hope you will be there to help me.

  • ghostdog74 Sep 14, 2008 @ 11:16

    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

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.