« Inline editing with sed | Main | My new obsession »


Iterating a file using a basic for loop in bash

Another one I always use - doing some command over every line of a file:

for i in `cat foo.txt`; do echo $i; done

Nice and simple, but works a treat.

Found this useful? Then Digg It.


Comments (2)

anon:

This doesn't work using unxutils (for win32: see http://unxutils.sf.net ) if the file names have spaces in.

jesse:

Yeah, that won't do what you expect if there are lines with whitespace in them.

If you strictly want to process things a line at a time, you can do something like--

cat foo.txt | while read i; do echo $i; done

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

All spam will be reported

About

This page contains a single entry from the blog posted on September 24, 2006 9:37 PM.

The previous post in this blog was Inline editing with sed.

The next post in this blog is My new obsession.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.
Powered by
Movable Type 3.36

Valid XHTML 1.0!