Skip to content Skip to sidebar Skip to footer

Bash Read File Line Into Multiple Variables

This article is all nearly how to read files in bash scripts using a while loop. Reading a file is a common operation in programming. You should exist familiar with different methods and which method is more efficient to use. In bash, a single job can be achieved in many ways but there is e'er an optimal mode to get the job washed and we should follow it.

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 [ Condition ] do     code block done        

Let'due south break downwards 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 will be placed betwixt do and done.
NUMBER=0  while [[ $NUMBER -le 10 ]] do     repeat " Welcome ${NUMBER} times "     (( NUMBER++ )) done        
While Loop
While Loop

This is a very simple instance, where the loop executes until NUMBER is not greater than x and prints the repeat argument.

Forth with while nosotros will utilise 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. At present at that place are unlike ways to pass the file as input and we will see them all.

# SYNTAX while read VARIABLE do     code done        

Pipe in Linux

Normally we volition use the true cat command to view the contents of the file from the last. Also, we will pipe the output of the cat control to other commands like grep, sort, etc.

Similarly, we will use the cat control here to read the content of the file and pipage it to a while loop. For demonstration, I am using /etc/passwd file merely it is non appropriate to mess with this file so take a backup copy of this file and play with it if y'all want so.

cat /etc/passwd | while read LREAD do     repeat ${LREAD} done        
Piping in Linux
Piping in Linux

Allow's break downwardly what will happen when the above code is submitted.

  • cat /etc/passwd volition read the contents of the file and pass it as input through the pipe.
  • read command reads each line passed as input from true cat command and stores it in the LREAD variable.
  • read command volition read file contents until EOL is interpreted.

You lot tin can too use other commands like head, tail, and pipage it to while loop.

head -n 5 /etc/passwd | while read LREAD do     repeat ${LREAD} washed        
Head Command
Head Command

Input Redirection in Linux

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

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

You can also store the file proper noun to a variable and pass it through a redirection operator.

FILENAME="/etc/passwd"  while read LREAD do     echo ${LREAD} washed < ${FILENAME}        
Store Filename in Variable
Shop Filename in Variable

Yous tin also pass file names every bit an argument to your script.

while read LREAD do     echo ${LREAD} done < $1 | caput -n 5        
Store Filename as Argument
Store Filename every bit Argument

Internal Field Separator

You may work with different types of file formats (CSV, TXT, JSON) and you may want to separate the contents of the file based on a custom delimiter. In this example, y'all can use "Internal field separator (IFS)" to divide the content of the file and store it in variables.

Let me demonstrate how information technology works. Take a look at the /etc/passwd file which has a colon (:) equally the delimiter. You tin now dissever each discussion from a line and store it in a separate variable.

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

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

I displayed just 1 line split in the above screenshot considering screenshot size.

Empty Lines in Linux

Empty lines are non ignored when y'all loop through the file content. To demonstrate this I accept created a sample file with the below content. In that location are 4 lines and few empty lines, leading whitespace, trailing white space, tab characters in line ii, and some escape characters (\n and \t).

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

Come across the outcome, bare line is non ignored. Also, an interesting matter to note is how white spaces are trimmed by the read command. A simple way to ignore blank lines when reading the file content is to use the exam operator with the -z flag which checks if the string length is zero. At present let's repeat the same case but this time with a exam operator.

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

Now from the output, you lot can see empty lines are ignored.

Escape Characters

Escape characters like \n, \t, \c will non 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 practise     echo ${LREAD} washed < 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 \n and \t. You tin can employ -r to prevent backslash estimation.

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

That'due south it for this commodity. We would love to hear back from you if there are any feedbacks or tips. Your feedback is what helps us to create better content. Go along reading and go on supporting.

If You lot Appreciate What We Do Here On TecMint, You lot Should Consider:

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

If you like what you are reading, delight consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

pohlsithe1984.blogspot.com

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

Post a Comment for "Bash Read File Line Into Multiple Variables"