Tuesday, August 30, 2016
Linux Shell Script Bash Read line by line in While Loop and Print Even lines
August 30, 2016
bash
,
code
,
example
,
linux
,
read and print even lines
,
shell script
,
tutorial
What the code does:
It reads from the given file line by line using while loop. Next it check if the line number is even or, odd using if condition and prints the even lines.Sample Input File:
save it as FaxNumbers.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+(country code)-(area code)-(fax number) | |
+1-212-9876543 | |
+44-208-1234567 | |
+443-208434-12345674 | |
+233-11-114567 | |
+190-209-1234567 | |
+1-212-9876543 | |
+44-208-1234567 | |
+443-208434-12345674 | |
+233-11-114567 |
Code to Print Even Lines:
Save it as "EvenLinePrinter.sh".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
i=1 | |
while read line | |
do | |
var=$(( $i % 2 )) | |
if [ $var -eq 0 ]; then | |
echo "$line" | |
fi | |
i=$(( $i + 1 )) | |
done < "FaxNumbers.txt" |
Run The Code:
bash EvenLinePrinter.sh
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment