How do I read a file line by line under UNIX using KSH or BASH shell?
You can use the following syntax using ksh:
#!/bin/ksh file="/home/vivek/data.txt" while read line do # display $line or do somthing with $line echo "$line" done <"$file"
Same code should work with bash too:
#!/bin/bash file="/home/vivek/data.txt" while IFS= read -r line do # display $line or do somthing with $line echo "$line" done <"$file"
You can also read field wise:
#!/bin/bash file="/etc/passwd" while IFS=: read -r f1 f2 f3 f4 f5 f6 f7 do # display fields using f1, f2,..,f7 echo "Username: $f1, Shell: $f7, Home Dir: $f6" done <"$file"
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop












{ 8 comments… read them below or add one }
The bash example is correct for all shells. The ksh example is not correct; it will strip leading and trailing whitespace.
To demonstrate, use:
on a file with leading and/or trailing whitespace on one or more .
Reading a file line by line with a single loop is fine in 90% of the cases.
But if things get more complicated then you may be better off opening the file as a file descriptor.
Situations such as:
* the read of the file is only a minor aspect and not the main task, but you want to avoid opening an closing the file multiple times. For example preserving where you have read up to or avoiding file closure (network, named pipes, co-processing)
* the file being read requires different methods of parsing, that is better done in simpler but different loops. Reading emails (header and body) is an example of this, but so is reading files with separate operational and data blocks.
* you are reading from multiple files at the same time, and switching between different files, in a pseudo-random way. That is you never quite know whch file you will need to read from next.
I have a scenario like this. I have a file which has servername, port and ipaddress
svr1
port1
ip1
svr2
port2
ip2
…
…
I tried couple of ways reading these records and assigning them a field and print the output to a file like below but nothing is helpful
servername port ipaddress
svr1 port1 ip1
scr2 port2 ip2
and so on
can someone please help me.
Thanks
I figured it out. May be not professional but it is working for me.
Thanks
HOW DO YOU SPLIT INPUT INTO FIELDS FOR PROCESSING?
I have a question concerning how you’d actually process individual fields of input.
For example, let’s say you’re doing the usual
while read LINE; do
process the statements
done firstbyteinq and looking at firstbyteinq with a read, but 1). the cut doesn’t seem to be doing it just to the record being read, it does it to ALL the records all at one time; 2). the read of the firstbyteinq then advances the other file one record!!!
It seems to me there ought to be some way to “declare” the positions from a to b as fields and THEN use the while read, but I’m not sure how to do that.
I’ll try anything. Thanks VERY much in advance.
princess anu, there’s no need to use arithmetic; just read the file in groups of three lines:
A correction; you need to check that the read succeeded; checking the first should be enough:
I have scenario :
One file which contain values like:
ABS
SVS
SGS
SGS
another file is template which has to use values from 1st file (One value at a time, line by line )
ARGS=ABS >> save in one file
New file:
ARGS=SVS >> save in another file
Can you please help me to achive this