Bash Read Line in a File and Put Into Variable
In some cases, you need to read a file line by line and there are several ways to read a file in a bash script. The while loop is the best option to read a file line by line in Linux and in this commodity, we volition show y'all read a file line by line in fustigate script with several examples that prints each line.
Basic Syntax
The bones syntax to read a file line by line as shown beneath:
while IFS= read -r line
do
repeat "$line"
washed < inputfile
Where :
- -r : This option is used to prevents backslash escapes from beingness interpreted.
- IFS : This selection is used to preclude leading/trailing white-infinite from being trimmed.
- inputfile : This is the proper noun of the file you desire to read line by line.
Create a Sample File for Testing
For testing purpose, create a sample file named file.txt equally shown below:
nano file.txt
Add the following contents:
India
Bangladesh
Pakistan
Australia
England
Srilanka
Salve and close the file when you are finished.
Read File from Command Line
You tin hands read a file from the control line without using the cat command.
Let's read the file file.txt line by line using while loop as shown below:
while IFS= read -r line; do echo $line; washed < file.txt
This control volition read each line from the file file.txt and store the content of the line in $line variable then printed it afterward.
Bharat
Bangladesh
Pakistan
Australia
England
Srilanka
Read File Using Bash Script
Yous tin can besides create a bash script and read whatever file line by line.
Let'due south create a readfile.sh script.
In this example, n variable is used to continue the value of the line number of the file and while loop is used to read this file with line number.
nano readfile.sh
Add together the post-obit contents:
#!/bin/bash
n=1
while IFS= read -r line; do
# reading each line
echo "Line No. $n : $line"
north=$((north+1))
washed < /root/file.txt
Save and close the file when you lot are finished.
Then, run the script equally shown below:
bash readfile.sh
You should see the following output:
Line No. ane : Bharat
Line No. 2 : People's republic of bangladesh
Line No. 3 : Pakistan
Line No. 4 : Australia
Line No. 5 : England
Line No. 6 : Srilanka
Passing Filename as an Argument and Reading the File
You can also have a filename as an argument and read the file line by line.
Let'southward create a readfile.sh script.
nano readfile.sh
Add together the post-obit contents:
#!/bin/fustigate
while IFS= read -r line; practice
echo "$line is the state name"
done < $1
Save and close the file when you are finished.
Then, run the script as shown beneath:
bash readfile.sh
You should meet the following output:
India is the country name
Bangladesh is the country name
Islamic republic of pakistan is the country proper noun
Australia is the land name
England is the country name
Srilanka is the state name
Read a File Using a Process Substitution
Procedure Substitution allows the input or output of a command to appear as a file.
Let's take a look at the following example.
nano readfile.sh
Add the post-obit contents:
#!/bin/bash
while IFS= read -r line
do
echo "Welcome to $line"
done < <(true cat /root/file.txt )
Save and close the file when you are finished. And so, run the script equally shown below:
bash readfile.sh
You should run across the post-obit output:
Welcome to Bharat
Welcome to People's republic of bangladesh
Welcome to Pakistan
Welcome to Australia
Welcome to England
Welcome to Srilanka
Read a File Using a Here String
Here Cord allows yous to pass multiple lines of input to a command.
Let's see the following example:
nano readline.sh
Add together the post-obit contents:
#!/bin/bash
while IFS= read -r line
practice
echo "$line"
done <<< $(true cat /root/file.txt )
Save and close the file when you are finished. Then, run the script as shown below:
bash readfile.sh
You should see the following output:
Bharat Bangladesh Islamic republic of pakistan Commonwealth of australia England Srilanka
Conclusion
In this tutorial, we've learned how to read a file line by line in a bash script with several examples.
Feel gratuitous to ask questions below in the comments if you lot have whatever!
Source: https://www.webservertalk.com/bash-read-line-by-line
0 Response to "Bash Read Line in a File and Put Into Variable"
Post a Comment