Bash Scripting - How to read a file line by line
Last Updated :
22 Nov, 2021
In this article, we are going to see how to read a file line by line in Bash scripting.
There might be instances where you want to read the contents of a file line by line using a BASH script. In this section, we will look at different ways to do just that. We will use BASH commands and tools to achieve that result. We make use of the read and cat commands, for loops, while loops, etc to read from the file and iterate over the file line by line with a few lines of script in BASH.
Method 1: Using read command and while loop
We can use the read command to read the contents of a file line by line. We use the -r argument to the read command to avoid any backslash-escaped characters.
#!usr/bin/env bash
file="temp.txt"
while read -r line; do
echo -e "$line\n"
done <$file
In the following example, we can see that we have an iteration over a file line by line and we store the contents of a single line in the variable "line". The file name is stored in the variable file and this can be customized as per requirements. You can run the script using the following command (Here filereader.sh can be any name you give to your script).
bash filereader.sh
We use the read command with -r argument to read the contents without escaping the backslash character. We read the content of each line and store that in the variable line and inside the while loop we echo with a formatted -e argument to use special characters like \n and print the contents of the line variable.
The file can be also inputted by parsing it as a positional parameter.
bash filereader.sh filename
The filename can be any file you want to read the contents of. You need to change the script where the file variable has been declared.
file=$1
This will take the first parameter after the script name as the filename to be used in the script. Hence we can make the script dynamically change the file as per the input given.
Method 2: Using cat and for loop
From this approach, we can read the contents of a file using the cat command and the for a loop. We use the same approach for a loop as in the while loop but we store the contents of the file in a variable using the cat command. The for loop iterates over the lines in the cat command's output one by one until it reaches EOF or the end of the file.
Here as well we can incorporate the positional parameter as a filename. We need to replace the filename with $1 in the script.
file=$(cat $1)
This will take the file from the command line argument and parse it to the cat command to further process the script.
#!usr/bin/env bash
file=$(cat temp.txt)
for line in $file
do
echo -e "$line\n"
done
Thus, from the above examples, we were able to read the contents of a file line by line in a BASH script.
Similar Reads
How to Read a File Line by Line to String in Golang? To read a file line by line the bufio package Scanner is used. Let the text file be named as sample.txt and the content inside the file is as follows: GO Language is a statically compiled programming language, It is an open-source language. It was designed at Google by Rob Pike, Ken Thompson, and Ro
2 min read
How To Read a File Line By Line Using Node.js? To read a file line by line in Node.js, there are several approaches that efficiently handle large files and minimize memory usage. In this article, we'll explore two popular approaches: using the Readline module (which is built into Node.js) and using the Line-reader module, a third-party package.
3 min read
How to read a Large File Line by Line in PHP ? We will use some file operations to read a large file line by line and display it. Read a file: We will read the file by using fopen() function. This function is used to read and open a file. Syntax: fopen("filename", access_mode); Parameter: filename: Filename is the name of the file access_mode: I
2 min read
Reading Lines by Lines From a File to a Vector in C++ STL Prerequisites: STL in C++Vector in C++File handling in C++ The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container classes, algorithms, and iterators. Vector in C
2 min read
How To Import Text File As A String In R IntroductionUsing text files is a common task in data analysis and manipulation. R Programming Language is a robust statistical programming language that offers several functions for effectively managing text files. Importing a text file's contents as a string is one such task. The purpose of this a
6 min read
Bash Scripting - Bash Read Password without Echoing back Generally, you can see on the login page, whenever we type a password either it show dot (â¢) or asterisk (*) instead of a password. They do so because to protect our system or account. In such a case, the login page read the password and in place of the password, it will show a dot or asterisk. And
3 min read