Domolith

It's Domorific!
  • rss
  • Home
  • About

Read File line by line with Shell Script (bash)

entro | March 28, 2007

I realize this is a very basic issue, but when searching online I found a dearth of information regarding reading a text file line by line using a shell script. So below I have included the basic code you need.

#!/bin/bash
#### skeleton code for reading a file line by line

## fill variable "thefile" with the name and path of the file you want to read line by line
thefile=/home/var/nameoffileyouwanttoprocess

## the loop below pipes the output of the file into a variable ($line) line by line
cat $thefile | while read line
do
echo $line #placeholder command to make sure everything is groovy
done

Hopefully this helps some people!

Categories
Uncategorized
Tags
bash, shell script
Comments rss
Comments rss
Trackback
Trackback


14 Responses to “Read File line by line with Shell Script (bash)”

  1. Vikas says:
    May 15, 2007 at 10:40 PM

    thanks for the code dude.

    Reply
  2. sheehan says:
    June 11, 2007 at 12:59 PM

    shell scripts rock!

    Reply
  3. Brian Napoletano says:
    July 11, 2007 at 5:00 PM

    You’re my hero! After two hours of banging my head against the wall, I stumbled across your solution, and it works! Every other solution I tried kept going word-by-word.

    Reply
  4. manna says:
    August 8, 2007 at 5:18 AM

    thx

    Reply
  5. Caleb says:
    November 6, 2007 at 2:51 PM

    Simple script. It works! Thanks.

    Reply
  6. the rasx() context » Blog Archive » The Importance of Using zgrep says:
    January 2, 2008 at 6:00 PM

    […] a Perl-based tool called split-logfile. My savage ignorance of Perl led me to look at bash in “Read File line by line with Shell Script”—which turned out to be hopeless slow for large Web logs. So some person named “hans” at […]

    Reply
  7. Steve says:
    May 9, 2008 at 1:09 PM

    Absolutely helpful!!!! Thank you SOOOOO much! Simply brilliant!

    Reply
  8. Daffy says:
    May 9, 2008 at 2:04 PM

    Thanks! Exactly what I was looking for!

    Reply
  9. mdn says:
    May 16, 2008 at 12:38 PM

    thanks that was really helpful!

    Reply
  10. Santhosh says:
    June 12, 2008 at 3:11 AM

    Thanks a lot!!!!!!!!!!!!!

    Reply
  11. Ciaa says:
    June 12, 2008 at 11:30 PM

    great stuff!!! useful indeed!

    Reply
  12. Leigh Anderson says:
    September 30, 2008 at 1:43 PM

    Wow… I feel like an idiot. Was trying to get something JUST LIKE THIS and I couldn’t because my edumacashun is fail. This helped me tremendously.

    Reply
  13. flawless says:
    August 25, 2009 at 8:04 AM

    Thanks a lot , you saved me man 😉

    Reply
  14. aksshay says:
    March 28, 2012 at 3:24 AM

    I was looking for this code with explaination from a long time. Thanks. Really helpful.

    Reply

Leave a Reply

Click here to cancel reply.

Posts

  • March 2007 (1)

Search

rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox