How to use Bash read file line by line

Introduction

In this tutorial, we will explore a simple and effective method to achieve this using Bash scripting. I use while..do..done use bash read file line by line on a Linux system. you can refer to the Bash script tutorial.

Bash, the default shell for most Unix-based operating systems, is a powerful tool for automating tasks and managing files and directories. One common task is reading a file line by line, which can be handy for various purposes, such as data processing, log analysis, and more.

Prerequisites

  • A Unix-like operating system (Linux or macOS) with Bash installed.
  • A text file that you want to read line by line. You can use any plain text file for this tutorial.

To read a file line by line using Bash, you can use a combination of the while loop and the read command. Here’s an example:

Bash read file line by line

For Example 1:

Using bash to read file.txt file.

[huupv@huupv devopsroles]$ cat file.txt 
Huu 30
Phan 28
foo 00

The content bash_read_file.sh file

#!/bin/bash
#To take a filename as an argument
filename="$1"
while read -r name age
do
  _NAME="$name"
  _AGE="$age"
  echo "Name read from file - ${_NAME}"
  echo "${_NAME}: Age is - ${_AGE}"
done < "$filename"

Explain it:

  • filename=$1 This line assigns the value of the first command-line argument to the filename variable. 
  • The -r option prevents backslash characters from being interpreted as escape characters.
  • done < $filename This line marks the end of the loop. The < symbol is used to redirect the contents of the file specified  "$filename" as the input for the while loop. Each line will be read and processed until the end of the file is reached.

To change mode execute and run the bash script

[huupv@huupv devopsroles]$ chmod +x bash_read_file.sh
[huupv@huupv devopsroles]$ ./bash_read_file.sh file.txt

The screen terminal:

Name read from file - Huu
Huu: Age is - 30
Name read from file - Phan
Phan: Age is - 28
Name read from file - foo
foo: Age is - 00

For example 2

Bash script read /etc/passwd file with fields: filed1 filed2 filed3 filed4 filed5 filed6 filed7

The content bash_read_file.sh file

#!/bin/bash
#Another thing is to take a filename as an argument.
filename="$1"
IFSOLD=$IFS
IFS=:
while read -r field1 field2 field3 field4 field5 field6 field7
do
  #display fields in /etc/passwd file
  printf 'Username: %s \t Shell: %s \t Home Dir: %s\n' "$field1" "$field7" "$field6"

done < "$filename"
IFS=$IFSOLD

The while loop reads each line from the file using the read command. The IFS= read -r field command ensures that leading and trailing whitespaces are preserved in each line.

The screen output terminal:

Bash read file line by line

Conclusion

Through the article, you can use Bash read file line by line. By following these steps, you can effectively read a file line by line in Bash. 

Reading a file line by line in Bash is a fundamental skill that comes in handy for various scripting and automation tasks. With the while loop and the read command, you can efficiently process the contents of a file one line at a time, making it easier to work with large datasets, log files, and more. I hope will this your helpful.

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.