Tuesday, May 20, 2014

Shell Script for reading a file line by line

Below script reads an input file line by line and displays each line with a half a second delay:

#Please provide file to be read as input while executing script
#!/bin/bash
count=$(cat $1 | wc -l)
for (( i=1; i<=$count; i++ ))
do
value=$(cat $1 | head -$i | tail -1)
echo $value
sleep 0.5
done

Above script reads each line and stores the same in value variable and henceforth value can be utilized for many other manipulations.

There are many other standard methods of reading a file specially with read line in while loop but I had prepared my own method to read the file. :)