Domolith

Its Domorific!
  • rss
  • Home

Read File line by line with Shell Script (bash)

March 28, 2007 | 4:29 pm

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
Bash, Shell-Scripts
Comments rss
Comments rss
Trackback
Trackback

Camtasia Alternatives »

6 responses

thanks for the code dude.

Vikas | May 15, 2007 | 10:40 pm

thanks for the code dude.

shell scripts rock!

sheehan | June 11, 2007 | 12:59 pm

shell scripts rock!

You're my hero! After two hours of banging my head

Brian Napoletano | July 11, 2007 | 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.

thx

manna | August 8, 2007 | 5:18 am

thx

Simple script. It works! Thanks.

Caleb | November 6, 2007 | 2:51 pm

Simple script. It works! Thanks.

[...] a Perl-based tool called split-logfile. My savage ignorance of

the rasx() context » Blog Archive » The Importance of Using zgrep | January 2, 2008 | 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 […]

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Navigation

  • Bash
  • Facebook
  • Free
  • Open-Source
  • Reviews
  • Screencasts
  • Shell-Scripts
  • Software
  • StumbleUpon
  • Tutorials

Search

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