Read All Lines Exclude Last 2 in Unix Shell Script Using While Loop

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 efficient to use. In fustigate, a single chore tin can exist achieved in many means only there is e'er an optimal style to get 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 true.

while [ Condition ] do     code block done        

Let'southward suspension downward while loop syntax.

  • while loop should showtime with a while keyword followed by a status.
  • A condition should be enclosed within [ ] or [[ ]]. The condition should always return 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 ]] do     echo " Welcome ${NUMBER} times "     (( NUMBER++ )) done        
While Loop
While Loop

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

Along with while nosotros volition utilise 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. At present there are different means to pass the file equally input and we will come across them all.

# SYNTAX while read VARIABLE practise     lawmaking done        

Pipage in Linux

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

Similarly, we volition use the cat control here to read the content of the file and pipe information technology to a while loop. For demonstration, I am using /etc/passwd file but information technology is not appropriate to mess with this file so take a backup copy of this file and play with it if yous desire so.

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

Let's break down what will happen when the to a higher place code is submitted.

  • cat /etc/passwd volition read the contents of the file and pass it every bit input through the pipage.
  • read control reads each line passed as input from cat control and stores it in the LREAD variable.
  • read control 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.

head -n 5 /etc/passwd | while read LREAD exercise     repeat ${LREAD} done        
Head Command
Head Command

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 -n five        
Input Redirection
Input Redirection

You tin as well shop the file proper noun to a variable and laissez passer it through a redirection operator.

FILENAME="/etc/passwd"  while read LREAD practice     echo ${LREAD} washed < ${FILENAME}        
Store Filename in Variable
Shop Filename in Variable

You can also pass file names as an statement to your script.

while read LREAD practice     echo ${LREAD} washed < $one | head -n v        
Store Filename as Argument
Shop Filename as Argument

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, you lot tin use "Internal field separator (IFS)" to split the content of the file and shop it in variables.

Let me demonstrate how it works. Accept a look at the /etc/passwd file which has a colon (:) as the delimiter. You can now split each discussion from a line and store it in a separate variable.

In the below case, 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 Eastward F G exercise     echo ${A}     repeat ${B}     echo ${C}     echo ${D}     echo ${E}     echo ${F}     echo ${G} done < /etc/passwd        
Internal Field Separator
Internal Field Separator

I displayed just one line split in the above screenshot because screenshot size.

Empty Lines in Linux

Empty lines are non ignored when yous loop through the file content. To demonstrate this I have created a sample file with the beneath content. In that location are 4 lines and few empty lines, leading whitespace, abaft white space, tab characters in line ii, and some escape characters (\n and \t).

File with Empty Lines
File with Empty Lines
while read LREAD do     repeat ${LREAD} washed < testfile        
Blank Line Not Ignored
Bare Line Not Ignored

Meet the result, blank line is not ignored. As well, an interesting thing to note is how white spaces are trimmed by the read command. A simple fashion to ignore blank lines when reading the file content is to utilize the test operator with the -z flag which checks if the string length is zippo. Now let's repeat the same example only this fourth dimension with a test operator.

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

Now from the output, you can see empty lines are ignored.

Escape Characters

Escape characters similar \n, \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 do     echo ${LREAD} done < testfile        
Escape Character in Linux
Escape Graphic symbol in Linux

You tin see from the output escape characters take lost their pregnant and only n and t are printed instead of \north and \t. Yous tin can use -r to preclude backslash interpretation.

while read -r LREAD practice     repeat ${LREAD} done < testfile        
Prevent Backslash Interpretation
Prevent Backslash Interpretation

That's it for this article. We would love to hear dorsum from you if there are whatsoever feedbacks or tips. Your feedback is what helps us to create better content. Keep reading and keep supporting.

If Y'all Appreciate What We Practice Here On TecMint, You lot Should Consider:

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

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

Support Us

We are thankful for your never ending back up.

orozcoabelity.blogspot.com

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

0 Response to "Read All Lines Exclude Last 2 in Unix Shell Script Using While Loop"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel