Bash Read a File Into a Variable
This article is all about how to read files in fustigate scripts using a while loop. Reading a file is a common operation in programming. Yous should exist familiar with unlike methods and which method is more efficient to use. In bash, a single job can be achieved in many ways but there is always an optimal way to get the job 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 condition and iterates over a given set of codes when the condition is true.
while [ Condition ] do lawmaking block done
Let'south break down while loop syntax.
- while loop should outset with a while keyword followed by a condition.
- A status should be enclosed inside [ ] or [[ ]]. The status should e'er return truthful for the loop to be executed.
- The actual cake of code will exist placed between do and washed.
NUMBER=0 while [[ $NUMBER -le 10 ]] do echo " Welcome ${NUMBER} times " (( NUMBER++ )) done
This is a very uncomplicated example, where the loop executes until NUMBER is not greater than 10 and prints the echo statement.
Along with while we will employ 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 different means to pass the file as input and we volition see them all.
# SYNTAX while read VARIABLE do code done
Pipe in Linux
Unremarkably nosotros will use the cat command to view the contents of the file from the terminal. Likewise, we will pipe the output of the true cat command to other commands like grep, sort, etc.
Similarly, we volition employ the cat control hither 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 appropriate to mess with this file so take a fill-in copy of this file and play with it if you want so.
cat /etc/passwd | while read LREAD do repeat ${LREAD} done
Let'south break down what will happen when the above lawmaking is submitted.
- true cat /etc/passwd will read the contents of the file and pass it equally input through the pipe.
- read command reads each line passed as input from cat command and stores it in the LREAD variable.
- read command will read file contents until EOL is interpreted.
You tin likewise use other commands like caput, tail, and pipage it to while loop.
caput -due north 5 /etc/passwd | while read LREAD do echo ${LREAD} washed
Input Redirection in Linux
We can redirect the content of the file to while loop using the Input redirection operator (<).
while read LREAD do repeat ${LREAD} done < /etc/passwd | head -n 5
You can also store the file name to a variable and pass it through a redirection operator.
FILENAME="/etc/passwd" while read LREAD practice echo ${LREAD} done < ${FILENAME}
Y'all tin besides pass file names as an argument to your script.
while read LREAD exercise echo ${LREAD} done < $1 | head -due north 5
Internal Field Separator
Yous may work with dissimilar types of file formats (CSV, TXT, JSON) and yous may want to split the contents of the file based on a custom delimiter. In this case, yous tin employ "Internal field separator (IFS)" to split the content of the file and store it in variables.
Permit me demonstrate how it works. Accept a look at the /etc/passwd file which has a colon (:) as the delimiter. You lot can at present divide each word from a line and store information technology in a separate variable.
In the beneath 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 exercise echo ${A} echo ${B} echo ${C} echo ${D} echo ${East} echo ${F} echo ${G} done < /etc/passwd
I displayed just i line split in the to a higher place screenshot considering screenshot size.
Empty Lines in Linux
Empty lines are not ignored when you loop through the file content. To demonstrate this I have created a sample file with the below content. There are four lines and few empty lines, leading whitespace, abaft white infinite, tab characters in line 2, and some escape characters (\n and \t).
while read LREAD do echo ${LREAD} done < testfile
See the result, blank line is non ignored. Also, an interesting thing to notation is how white spaces are trimmed by the read control. A simple style 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. Now allow's repeat the same example merely this time with a test operator.
while read LREAD do if [[ ! -z $LREAD ]] then echo ${LREAD} fi done < testfile
Now from the output, you can come across 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 exercise echo ${LREAD} done < testfile
You can encounter from the output escape characters have lost their meaning and only n and t are printed instead of \north and \t. You tin can use -r to prevent backslash interpretation.
while read -r LREAD practice echo ${LREAD} washed < testfile
That's it for this article. We would honey to hear back from you if at that place are any feedbacks or tips. Your feedback is what helps us to create ameliorate content. Keep reading and go on supporting.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted customs 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 bachelor FREELY to all.
If you like what y'all 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/
0 Response to "Bash Read a File Into a Variable"
Post a Comment