How to Read From a File in Bash

This article is all well-nigh how to read files in bash scripts using a while loop. Reading a file is a common operation in programming. You lot should exist familiar with different methods and which method is more efficient to utilize. In fustigate, a single task can be achieved in many ways but there is always an optimal mode to get the task done and we should follow information technology.

Before seeing how to read file contents using while loop, a quick primer on how while loop works. While loop evaluates a condition and iterates over a given set of codes when the condition is true.

while [ Status ] do     code block done        

Let's break down while loop syntax.

  • while loop should outset with a while keyword followed by a condition.
  • A condition should be enclosed inside [ ] or [[ ]]. The condition should always return truthful for the loop to be executed.
  • The actual block of code volition be placed betwixt do and done.
NUMBER=0  while [[ $NUMBER -le 10 ]] do     echo " Welcome ${NUMBER} times "     (( NUMBER++ )) washed        
While Loop
While Loop

This is a very simple example, where the loop executes until NUMBER is non greater than ten and prints the repeat statement.

Forth with while we volition use the read command to read the contents of a file line by line. Below is the syntax of how while and read commands are combined. Now there are dissimilar ways to pass the file equally input and we will run into them all.

# SYNTAX while read VARIABLE do     code done        

Pipage in Linux

Usually we will apply the cat command to view the contents of the file from the terminal. Also, we will pipe the output of the cat control to other commands similar grep, sort, etc.

Similarly, we will employ the cat command here to read the content of the file and pipe information technology to a while loop. For demonstration, I am using /etc/passwd file but information technology is non advisable to mess with this file and then take a backup copy of this file and play with it if you want so.

true cat /etc/passwd | while read LREAD exercise     echo ${LREAD} washed        
Piping in Linux
Piping in Linux

Let's intermission down what will happen when the to a higher place code is submitted.

  • cat /etc/passwd will read the contents of the file and pass it equally input through the pipe.
  • read control reads each line passed equally input from cat control and stores information technology in the LREAD variable.
  • read control will read file contents until EOL is interpreted.

Yous tin as well use other commands similar head, tail, and pipe it to while loop.

head -north 5 /etc/passwd | while read LREAD do     echo ${LREAD} done        
Head Command
Caput Command

Input Redirection in Linux

We can redirect the content of the file to while loop using the Input redirection operator (<).

while read LREAD do     echo ${LREAD} done < /etc/passwd | head -northward v        
Input Redirection
Input Redirection

You can too store the file name to a variable and pass it through a redirection operator.

FILENAME="/etc/passwd"  while read LREAD practise     echo ${LREAD} done < ${FILENAME}        
Store Filename in Variable
Store Filename in Variable

You tin can too pass file names every bit an argument to your script.

while read LREAD exercise     echo ${LREAD} done < $1 | caput -n 5        
Store Filename as Argument
Store Filename equally Argument

Internal Field Separator

Yous may work with unlike types of file formats (CSV, TXT, JSON) and you lot may want to separate the contents of the file based on a custom delimiter. In this case, you can utilize "Internal field separator (IFS)" to split the content of the file and store it in variables.

Let me demonstrate how it works. Take a look at the /etc/passwd file which has a colon (:) as the delimiter. You lot tin now carve up each word from a line and store it in a carve up variable.

In the below example, I am splitting /etc/passwd file with a colon as my separator and storing each split into different variables.

while IFS=":" read A B C D E F G practice     repeat ${A}     echo ${B}     echo ${C}     echo ${D}     repeat ${E}     echo ${F}     echo ${Thou} done < /etc/passwd        
Internal Field Separator
Internal Field Separator

I displayed only ane line split in the higher up screenshot considering screenshot size.

Empty Lines in Linux

Empty lines are not ignored when yous loop through the file content. To demonstrate this I have created a sample file with the below content. At that place are 4 lines and few empty lines, leading whitespace, trailing white space, tab characters in line 2, and some escape characters (\n and \t).

File with Empty Lines
File with Empty Lines
while read LREAD practise     echo ${LREAD} washed < testfile        
Blank Line Not Ignored
Blank Line Non Ignored

Run into the result, bare line is not ignored. As well, an interesting thing to note is how white spaces are trimmed by the read command. A unproblematic way to ignore blank lines when reading the file content is to apply the test operator with the -z flag which checks if the string length is zero. At present let's echo the same example simply this time with a test operator.

while read LREAD do     if [[ ! -z $LREAD ]]     then         repeat ${LREAD}      fi done < testfile        
Blank Lines Ignored
Blank Lines Ignored

At present from the output, you can see empty lines are ignored.

Escape Characters

Escape characters like \n, \t, \c will not be printed when reading a file. To demonstrate this I am using the same sample file which has few escape characters.

File with Escape Characters
File with Escape Characters
while read LREAD do     echo ${LREAD} done < testfile        
Escape Character in Linux
Escape Graphic symbol in Linux

You can see from the output escape characters have lost their significant and only n and t are printed instead of \northward and \t. You can utilise -r to prevent backslash interpretation.

while read -r LREAD practice     echo ${LREAD} done < testfile        
Prevent Backslash Interpretation
Forestall Backslash Interpretation

That's it for this article. We would love to hear back from y'all if there are any feedbacks or tips. Your feedback is what helps u.s.a. to create better content. Go along reading and keep supporting.

If You Appreciate What We Do Here On TecMint, Y'all Should Consider:

TecMint is the fastest growing and almost trusted community site for any kind of Linux Articles, Guides and Books on the spider web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If yous similar what you are reading, please consider ownership us a coffee ( or 2 ) every bit a token of appreciation.

Support Us

We are thankful for your never catastrophe back up.

cummingsshandid.blogspot.com

Source: https://www.tecmint.com/different-ways-to-read-file-in-bash-script/

0 Response to "How to Read From a File in Bash"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel