Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Read a File Line By Line in Bash - DesignLinux

Nov 13 2020
designlinux 0 Comments
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.

Related

Tags: bash, terminal

How to Work with Date and Time in Bash Using date Command

Prev Post

How to Install Python 3.9 on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 4
  • 82
  • 605,832

DesignLinux.com © All rights reserved

Go to mobile version