How to Write a Shell Script to Read 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 apply. In bash, a single job can be accomplished in many ways but at that place 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 code cake washed
Allow'southward break down while loop syntax.
- while loop should start with a while keyword followed by a condition.
- A condition should be enclosed inside [ ] or [[ ]]. The condition should always return true for the loop to be executed.
- The actual cake of code will be placed betwixt practice and done.
NUMBER=0 while [[ $NUMBER -le ten ]] do repeat " Welcome ${NUMBER} times " (( NUMBER++ )) done
This is a very simple example, where the loop executes until NUMBER is not greater than 10 and prints the echo statement.
Forth with while nosotros will apply 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 ways to laissez passer the file as input and we will see them all.
# SYNTAX while read VARIABLE do code done
Piping in Linux
Normally nosotros will use the cat control to view the contents of the file from the terminal. Also, nosotros will piping the output of the cat command to other commands similar grep, sort, etc.
Similarly, we will use the cat command here to read the content of the file and pipe it to a while loop. For demonstration, I am using /etc/passwd file simply it is non advisable to mess with this file so take a backup copy of this file and play with it if you want so.
cat /etc/passwd | while read LREAD do echo ${LREAD} washed
Permit's pause down what volition 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 true cat control and stores information technology in the LREAD variable.
- read control will read file contents until EOL is interpreted.
Y'all can also use other commands similar head, tail, and pipage information technology to while loop.
head -n five /etc/passwd | while read LREAD do echo ${LREAD} done
Input Redirection in Linux
We can redirect the content of the file to while loop using the Input redirection operator (<)
.
while read LREAD do echo ${LREAD} done < /etc/passwd | head -northward v
You tin can also shop the file name to a variable and laissez passer information technology through a redirection operator.
FILENAME="/etc/passwd" while read LREAD do echo ${LREAD} done < ${FILENAME}
You tin also pass file names as an statement to your script.
while read LREAD do echo ${LREAD} done < $1 | head -north v
Internal Field Separator
You lot may work with different types of file formats (CSV, TXT, JSON) and yous may want to dissever the contents of the file based on a custom delimiter. In this example, yous can employ "Internal field separator (IFS)" to divide the content of the file and shop it in variables.
Allow me demonstrate how it works. Accept a look at the /etc/passwd file which has a colon (:)
as the delimiter. You tin can now split each word from a line and store information technology in a separate variable.
In the below example, I am splitting /etc/passwd file with a colon as my separator and storing each split into dissimilar variables.
while IFS=":" read A B C D E F G do echo ${A} echo ${B} echo ${C} echo ${D} echo ${E} repeat ${F} echo ${Grand} done < /etc/passwd
I displayed only 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 have created a sample file with the below content. At that place are 4 lines and few empty lines, leading whitespace, trailing white infinite, tab characters in line 2, and some escape characters (\n and \t).
while read LREAD do repeat ${LREAD} washed < testfile
Run across the result, blank line is not ignored. Also, an interesting affair to note is how white spaces are trimmed past the read command. A simple way to ignore blank lines when reading the file content is to employ the examination operator with the -z
flag which checks if the string length is zero. At present let's echo the same case but this time with a test operator.
while read LREAD do if [[ ! -z $LREAD ]] and then repeat ${LREAD} fi done < testfile
Now from the output, you lot can encounter empty lines are ignored.
Escape Characters
Escape characters like \due north
, \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.
while read LREAD do repeat ${LREAD} done < testfile
Yous can run into from the output escape characters take lost their meaning and just n and t are printed instead of \north
and \t
. You lot can employ -r
to foreclose backslash interpretation.
while read -r LREAD practice echo ${LREAD} washed < testfile
That's it for this article. We would love to hear back from yous if in that location are any feedbacks or tips. Your feedback is what helps united states of america to create ameliorate content. Keep reading and keep supporting.
If You lot Capeesh What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and about trusted community site for whatever 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 similar what you lot are reading, please consider buying us a coffee ( or ii ) as a token of appreciation.
We are thankful for your never ending back up.
Source: https://www.tecmint.com/different-ways-to-read-file-in-bash-script/
0 Response to "How to Write a Shell Script to Read Files"
Post a Comment