Bash Script Read Input From a File
This article 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 dissimilar methods and which method is more than efficient to utilize. In fustigate, a unmarried task can be achieved in many means but there is always an optimal way to get the task 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 prepare of codes when the condition is true.
while [ Condition ] do code block washed
Let's intermission downwards while loop syntax.
- while loop should start with a while keyword followed by a condition.
- A condition should be enclosed within [ ] or [[ ]]. The condition should always render true for the loop to be executed.
- The actual block of code will be placed between do and done.
NUMBER=0 while [[ $NUMBER -le 10 ]] practice repeat " Welcome ${NUMBER} times " (( NUMBER++ )) done
This is a very simple case, where the loop executes until NUMBER is not greater than 10 and prints the repeat statement.
Forth 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 there are different ways to laissez passer the file equally input and nosotros will encounter them all.
# SYNTAX while read VARIABLE exercise code done
Piping in Linux
Ordinarily nosotros will use the cat command to view the contents of the file from the last. Likewise, nosotros will piping the output of the cat command to other commands similar grep, sort, etc.
Similarly, nosotros will utilize the cat command hither to read the content of the file and pipe information technology to a while loop. For sit-in, I am using /etc/passwd file but it is not advisable to mess with this file and so take a fill-in re-create of this file and play with it if yous desire so.
cat /etc/passwd | while read LREAD do echo ${LREAD} done
Let's break down what will happen when the above code is submitted.
- true cat /etc/passwd will read the contents of the file and pass it as input through the pipe.
- read command reads each line passed as input from cat control and stores it in the LREAD variable.
- read command will read file contents until EOL is interpreted.
You lot can also use other commands like head, tail, and pipage information technology to while loop.
caput -northward v /etc/passwd | while read LREAD do echo ${LREAD} done
Input Redirection in Linux
Nosotros can redirect the content of the file to while loop using the Input redirection operator (<).
while read LREAD practice repeat ${LREAD} done < /etc/passwd | head -n 5
You lot tin 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} done < ${FILENAME}
You tin can also laissez passer file names equally an argument to your script.
while read LREAD do echo ${LREAD} washed < $i | head -n 5
Internal Field Separator
You may work with different types of file formats (CSV, TXT, JSON) and y'all may want to split the contents of the file based on a custom delimiter. In this case, you can employ "Internal field separator (IFS)" to split the content of the file and store information technology in variables.
Permit me demonstrate how information technology works. Take a await at the /etc/passwd file which has a colon (:) as the delimiter. You can now split up each word from a line and shop it in a separate variable.
In the below example, I am splitting /etc/passwd file with a colon as my separator and storing each carve up into unlike variables.
while IFS=":" read A B C D E F G practice echo ${A} echo ${B} repeat ${C} echo ${D} echo ${Due east} echo ${F} echo ${G} done < /etc/passwd
I displayed but one line divide in the above screenshot considering screenshot size.
Empty Lines in Linux
Empty lines are not ignored when you loop through the file content. To demonstrate this I accept 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 (\north and \t).
while read LREAD do echo ${LREAD} done < testfile
Encounter the result, blank line is not ignored. Also, an interesting thing to note is how white spaces are trimmed by the read control. A unproblematic 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 zero. Now allow's repeat the aforementioned example but this fourth dimension with a test operator.
while read LREAD do if [[ ! -z $LREAD ]] and then repeat ${LREAD} fi done < testfile
Now from the output, yous can run into empty lines are ignored.
Escape Characters
Escape characters like \n, \t, \c volition not be printed when reading a file. To demonstrate this I am using the same sample file which has few escape characters.
while read LREAD practise echo ${LREAD} done < testfile
You can see from the output escape characters have lost their pregnant and just n and t are printed instead of \n and \t. You lot can use -r to forestall backslash interpretation.
while read -r LREAD do repeat ${LREAD} done < testfile
That's it for this article. Nosotros would love to hear back from you if there are whatsoever feedbacks or tips. Your feedback is what helps the states to create better content. Keep reading and keep supporting.
If You Appreciate What We Do Hither On TecMint, You lot Should Consider:
TecMint is the fastest growing and well-nigh trusted community site for whatsoever 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 bachelor FREELY to all.
If you like what yous are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Source: https://www.tecmint.com/different-ways-to-read-file-in-bash-script/
Post a Comment for "Bash Script Read Input From a File"