Here, we’ll look at three ways in Shell scripts to read a file line by line. The easiest way to read a file line by line using input redirection is to use input redirection in a while loop.

To demonstrate, create a text file called “mycontent.txt” here with the following contents:

[root@localhost ~]# cat mycontent.txt This is a sample file We are going through contents line by line to understand How to Read Files Line by Line in a Shell Script How to Read Files Line by Line in a Shell Script Create a script called “example1.sh” that uses input redirection and loops:

[root@localhost ~]# cat example1.sh

! /bin/bash

while read rows do echo “Line contents are : $rows “done < myContent.txt $rows” done < myContent.txt $rows “done < myContent.txt

  • Begin the while loop and save the contents of each row in the variable “rows”
  • The output is displayed using echo, and the $rows variable is each line in the text file
  • Use echo to display the output, which includes a custom string and variables, and the $rows variable is each line in the text file Tips: You can reduce the above script to a single command like this:

[root@localhost ~]# while read rows; do echo “Line contents are : $rows”; Done < myContent.txt How to read a file line by line in a Shell script

Method 2, using the cat command and pipeline operator The second method is to use the cat command and pipeline operator |, then use the pipe to transfer the output as the input to the while loop.

Create a script file “example2.sh” with the contents of:

[root@localhost ~]# cat example2.sh

! /bin/bash

cat mycontent.txt | while read rows do echo “Line contents are : $rows “done How to read a file in a Shell script How to read a file in a Shell script How to work:

  • Use a pipe to send the output of the cat command as input to the while loop.
  • | pipe will output the cat saved in the content of the “$rows” variable.
  • Use echo to display the output, which includes a custom string and variables, and the $rows variable is each line in the text file Tips: You can reduce the above script to a single command like this:

[root@localhost ~]# cat mycontent.txt |while read rows; do echo “Line contents are : $rows”; How can I read a file line by line in a Shell script

The third method appends the text file name to the script when it executes by adding the $1 parameter.

Create a script file named “example3.sh” as follows:

[root@localhost ~]# cat example3.sh

! /bin/bash

While read rows do echo “Line contents are: $rows “done < $1

  • Begin the while loop and save the contents of each row in the variable “rows”
  • The output is displayed using echo, and the $rows variable is each line in the text file
  • Using the awk command, you can read the contents of a file line by line with only one command.

Create a script file named “example4.sh” as follows:

[root@localhost ~]# cat example4.sh

! /bin/bash

Cat mycontent. TXT | awk ‘{print “Line contents are:” $0}’ run results: how to read the file Line by Line in a Shell script to read the file Line by Line in the Shell script

This summary showed you how to use a shell script to read the contents of a file line by line, and by reading lines individually, you can help search for strings in a file.