Bash Parse a file forwards / Parse a file in reverse (backwards)

By | September 26, 2015
ilovebash

ilovebash

Bash Parse a file forwards / Parse a file in reverse (backwards)

Bash shell has some very simple commands that can help you do everyday tasks. One of the main functions I need a script to do is to read each line in a file from beginning to end and do something with each line the script reads.

Say for example you have a file called file1.txt of bank withdrawals like this :

2015-01-02 Withdrawal 100.00
2015-01-03 Withdrawal 10.00
2015-01-05 Withdrawal 50.00
2015-01-09 Withdrawal 20.00
2015-01-12 Withdrawal 110.00
2015-01-08 Withdrawal 200.00

ok thats a very simple file but you get the idea.

Part 1 – Parse a file forwards in Bash

So your task maybe is to find all transactions where you took out more than 100, the easiest way is to parse the file record by record and output the records you want to see.

cat file1.txt | while read LINE ; do

# the echo "$LINE" displays the whole record, the cut -d" " -f3 uses space as delimiter and gets the 3rd field. it then puts the value in a variable called VALUE1
VALUE1=`echo "$LINE" | cut -d" " -f3`

# Now you need to make an arithmetic decision based on whats in VALUE1 variable
if [[ $VALUE1 -gt 99 ]]
then
# if your value is greater than 99 (therefore 100+),
# you could also have done if [[ $VALUE1 -ge 100 ]] which means is greater than or equal to

# Now display the whole record
echo "$LINE"
fi

done

So that reads file1.txt line by line, the while read LINE ; do puts the current record into a variable called LINE, which inside the loop has extracted part of it into the VALUE1 variable, from where a decision process is made and ourpur displayed. And the done at the bottom ends the loop.

Part 2 – Parse a file in reverse (backwards) in Bash

Now maybe you want to adjust the above script to show your latest transactions first instead of in the order they are in the file. There are 2 VERY SIMPLE ways to do this (maybe more even), you could sort the file in reverse, but there is actually a much simpler method… check the script below and see if you can spot it….

tac file1.txt | while read LINE ; do

# the echo "$LINE" displays the whole record, the cut -d" " -f3 uses space as delimiter and gets the 3rd field. it then puts the value in a variable called VALUE1
VALUE1=`echo "$LINE" | cut -d" " -f3`

# Now you need to make an arithmetic decision based on whats in VALUE1 variable
if [[ $VALUE1 -gt 99 ]]
then
# if your value is greater than 99 (therefore 100+),
# you could also have done if [[ $VALUE1 -ge 100 ]] which means is greater than or equal to

# Now display the whole record
echo "$LINE"
fi

done

Do you see it?? The first word has gone from cat, and become tac !! tac is cat backwards!

Thats a very simple script, but as someone who has done bash scripting for over 20 years, I can honestly say that a very large portion of my scripts in one way or another will have a loop very similar to that in it!

If you liked this tutorial share it with your friends, or check out my other bash tutorials HERE

Or if there are any other tutorials you would like me to write then reply to this post, or email me on admin@bashworkz.com

Check out -  Background Bash Script - Push bash script to background

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.