Site icon DesignLinux

How to Read a File Line By Line in Bash

Read a File Line By Line in Bash

At the time of writing of Bash scripts, sometimes you need to read a file line by line. In this tutorial, we will show you how to read file line by line in Bash.

Syntax for Read File Line by Line

The following is the common syntax for reading a file line-by-line:

while IFS= read -r line
do
  echo "$line"
done < filename

Single line version:

while IFS= read -r line; do echo $line; done < filename

Here, filename is the input file which you want to be open and read with read command. It will read the file line by line, it will assign a each line to the line variable. While loop will be terminated once the all lines are processed. The internal field separator (IFS) is set to the null string to preserve leading and trailing whitespace which is the default behavior of the read command.

Examples for Read a File Line By Line

For example, a file name with cities.txt which contains the list of cities along with the state names separated by comma (,):

Loa Angeles,California
Houston,Texas
San Antonio,Texas
Columbus,Ohio
Las Vegas,Nevada

You would use the following code to read the file line by line:

while IFS= read -r line
do
  echo "$line"
done < cities.txt

In above code it will assign each line to a variable and echo the variable. It will show the same output if you run the file content using the cat command.

Now, if you would like to print the cities of only Texas state, you would use the if statement and check if the line contains the Texas substring:

while IFS= read -r line
do
  if [[ "$line" == *"Texas"* ]]; then
    echo "$line"
  fi
done < cities.txt
Houston,Texas
San Antonio,Texas

The read command allows you to pass multiple variable which will split the line into fields based on the IFS. It will assign first field to the first variable and second to the second and so on. If there are more fields than variables, the leftover fields are assigned to the last variable.

In the below example, IFS is set to a comma (,) and passing two variables city and state to the read command. So it will assign starting from the line until the first comma to the first variable (city) and the remaining part of the line will be assigned to the second variable (state).

while IFS=, read -r city state
do
  echo "$city" belongs to "$state"
done < cities.txt
Loa Angeles belongs to California
Houston belongs to Texas
San Antonio belongs to Texas
Columbus belongs to Ohio
Las Vegas belongs to Nevada

File Reading Alternate Methods

Process Substitution

Process substitution allows you to pass output from command as a filename:

while IFS= read -r line
do
  echo "$line"
done < <(cat filename )

Using a Here String

Here String is a variant of Here document . The string (cat filename ) will keep the newlines:

while IFS= read -r line
do
  echo "$line"
done <<< $(cat filename )

Using File descriptor

Using file descriptor you can give the input to the loop:

while IFS= read -r -u9 line
do
  echo "$line"
done 9< filename

When working with file descriptor use the number between 4 to 9 to avoid conflict with shell internal file descriptors.

Conclusion

In Bash, we can read a file line-by-line by providing the filename as an input to a while read loop.

If you have any questions or suggestion, please leave a comment below.

Exit mobile version