#!/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 -r 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 IFS=$BAKIFS exit 0
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 39 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • 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 | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Cool, I found this script very useful. I modified this and used to make our syncronisation across servers fully automated. Great Job
THANK YOU for sharing the script..it’s very generous/kind sharing…THUMBS UP
Don’t listen that fool Dedeepthi ↑
Here is the real code:
—–cut here—————
#!/bin/bash
for i in ‘cat file.txt’
do
echo “$i”
done
—–cut here—————
Hi,
You can try this simple code to read the file “file.txt” line-by-ine….
while read line
do
echo $line;
sleep 1;
done < file.txt
hi i want to update a port number in a file such like……
file content…..abc.cfg
# Temporary use by Boyajian. Thank you.
# Plz feel free to change as per ur need. Thnx – Ayan
###—SBJ_VIEW_NAME=devB_4140sdd
#SBJ_VIEW_NAME=bagay_dev_mts_1440sdd
SBJ_VIEW_NAME=devD_4018
#SBJ_VIEW_NAME=devD_4017
ACE_MATCH_SERVER_HOST=localhost
ACE_MATCH_SERVER_PORT=16711
ACE_JASI_SERVER_PORT=75675
ENT_CLIENT_HOSTS=localhost,imts1,charon
ENT_SERVER_PORT=45785
ENT_SERVER_HOST=imts1
ENT_VIRTUAL_MEMORY=1000000000
ENT_NUM_ACCUMULATORS=8
SBJ_NO_SIG_CHECK=1
AREA_PROD_BRANCH=s2010
now if i want to update the value of ACE_MATCH_SERVER_PORT from 16711 to 78777
what would be the code to do this stub? please give a solution at the earliest
for line in `cat abc.txt`;do
touch path/$line
done
this 2 line is code enuff
Hi Very informative post. Thanks for this.
But I have one confussion that
what following code is doing. Please healp.
BAKIFS=$IFS
IFS=$(echo -en “\n\b”)
exec 3<&0
exec 0<"$FILE"
Guys,
I need to read a file and see for the occurence /local/svncheckout/* at the begining of each line. If this instance is found I need to delete that particular line.
I need to write this program using shell script (bash shell)
Thanks a lot
I edited the code to input the contents of $line when ever the loop runs. it inputs line by line in a file that contains a list of id’s I pulled out of etc/passwd. for some reason it’s stops at the 25th line. it starts combining the variables to I dont’ know what’s going on. how it could work for the first 25 entries then break.
Thanks for posting it!!!
Thank you for the post.
It’s work perfectly but i need some changes
can u help me?
i want a search a specific pattern in a file and copy a a search result to xml file
Thanks in advance.
This is not working for me.
sh read.sh v1.txt
: not found:
v1.txt
: not found: }
: not found:
: not found:
read.sh: 40: Syntax error: “elif” unexpected (expecting “then”)
Thank you for the post!!
THANK YOU VERY MUCH!!!
I have used your example countless time when i wanted to write a quick hack. Thank You!