Bash Read One Line at a Time From Two Files

This commodity is all about how to read files in bash scripts using a while loop. Reading a file is a common operation in programming. You should be familiar with different methods and which method is more efficient to utilize. In bash, a single job can be achieved in many ways merely there is always an optimal fashion to get the task done 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 [ Status ] do     code block done        

Let's pause down while loop syntax.

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

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

Along with while we will use the read command to read the contents of a file line by line. Beneath is the syntax of how while and read commands are combined. Now in that location are dissimilar ways to pass the file as input and nosotros volition meet them all.

# SYNTAX while read VARIABLE do     code done        

Piping in Linux

Commonly we volition use the cat command to view the contents of the file from the terminal. Likewise, nosotros will pipe the output of the cat command to other commands like grep, sort, etc.

Similarly, nosotros will utilise the cat control here to read the content of the file and pipe it to a while loop. For demonstration, I am using /etc/passwd file but it is not advisable to mess with this file so take a backup copy of this file and play with it if you desire so.

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

Let'due south break downwardly what volition happen when the above code is submitted.

  • cat /etc/passwd will read the contents of the file and laissez passer information technology as input through the piping.
  • read command reads each line passed as input from cat command and stores it in the LREAD variable.
  • read command volition read file contents until EOL is interpreted.

You can also use other commands like head, tail, and pipe it to while loop.

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

Input Redirection in Linux

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

while read LREAD practice     echo ${LREAD} done < /etc/passwd | caput -n five        
Input Redirection
Input Redirection

Y'all tin can likewise shop the file name to a variable and pass it through a redirection operator.

FILENAME="/etc/passwd"  while read LREAD exercise     echo ${LREAD} done < ${FILENAME}        
Store Filename in Variable
Store 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 -northward v        
Store Filename as Argument
Store Filename as Statement

Internal Field Separator

Yous may piece of work with unlike types of file formats (CSV, TXT, JSON) and you lot may desire to split up the contents of the file based on a custom delimiter. In this example, yous can utilize "Internal field separator (IFS)" to divide the content of the file and shop it in variables.

Allow me demonstrate how information technology works. Take a look at the /etc/passwd file which has a colon (:) as the delimiter. You tin can now split each give-and-take from a line and store it in a separate variable.

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

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

I displayed just i line divide 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 have created a sample file with the below content. There 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 do     echo ${LREAD} done < testfile        
Blank Line Not Ignored
Bare Line Not Ignored

See the result, blank line is not ignored. Also, an interesting affair to annotation is how white spaces are trimmed by the read control. A uncomplicated manner 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. Now let'southward repeat the aforementioned instance only this time with a test operator.

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

Now from the output, you can run across 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 aforementioned sample file which has few escape characters.

File with Escape Characters
File with Escape Characters
while read LREAD do     repeat ${LREAD} done < testfile        
Escape Character in Linux
Escape Grapheme in Linux

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

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

That's it for this article. Nosotros would love to hear back from you if there are any feedbacks or tips. Your feedback is what helps u.s.a. to create better content. Go on reading and continue supporting.

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

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

If y'all like what y'all are reading, delight consider buying us a coffee ( or 2 ) every bit a token of appreciation.

Support Us

We are thankful for your never ending support.

romeoboyes1974.blogspot.com

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

0 Response to "Bash Read One Line at a Time From Two Files"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel