Linux How to Read File as Input

This article is all near how to read files in bash scripts using a while loop. Reading a file is a mutual operation in programming. You lot should be familiar with different methods and which method is more than efficient to employ. In fustigate, a single job can be accomplished in many means only at that place is always an optimal style to go the task done and nosotros 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 status and iterates over a given set of codes when the condition is truthful.

while [ CONDITION ] do     lawmaking block washed        

Let's break down while loop syntax.

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

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

Along with while we will use the read control 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 different ways to laissez passer the file as input and we will see them all.

# SYNTAX while read VARIABLE exercise     lawmaking done        

Piping in Linux

Ordinarily we will use the cat command to view the contents of the file from the terminal. Also, we will pipe the output of the cat command to other commands like grep, sort, etc.

Similarly, we volition use the true cat control here to read the content of the file and pipe it to a while loop. For sit-in, I am using /etc/passwd file but it is not appropriate to mess with this file so take a fill-in 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's break down what will happen when the above code is submitted.

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

You can also employ other commands like caput, tail, and pipage it to while loop.

head -northward 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 do     echo ${LREAD} washed < /etc/passwd | head -n five        
Input Redirection
Input Redirection

You can also shop the file name to a variable and pass information technology through a redirection operator.

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

Yous can also pass file names as an argument to your script.

while read LREAD do     echo ${LREAD} washed < $1 | head -n 5        
Store Filename as Argument
Shop Filename every bit Statement

Internal Field Separator

You lot may work with different types of file formats (CSV, TXT, JSON) and you may want to split the contents of the file based on a custom delimiter. In this example, yous tin can utilise "Internal field separator (IFS)" to divide 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 tin can now carve up each word from a line and store it in a separate variable.

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

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

I displayed just i line split up in the to a higher place 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. In that location are 4 lines and few empty lines, leading whitespace, trailing white infinite, 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
Blank Line Not Ignored

See the result, blank line is not ignored. Also, an interesting thing 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 test operator with the -z flag which checks if the string length is null. Now let's echo the same example only this fourth dimension with a examination operator.

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

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

Escape Characters

Escape characters like \northward, \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 practice     repeat ${LREAD} done < testfile        
Escape Character in Linux
Escape Graphic symbol in Linux

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

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

That's it for this article. We would love to hear back from you lot if there are any feedbacks or tips. Your feedback is what helps us to create amend content. Keep reading and go on supporting.

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

TecMint is the fastest growing and nigh trusted community site for any kind of Linux Manufactures, 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 you similar what y'all are reading, please consider ownership us a coffee ( or 2 ) equally a token of appreciation.

Support Us

We are thankful for your never ending support.

harrisglarprive.blogspot.com

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

0 Response to "Linux How to Read File as Input"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel