bash Line Parsing

Date Submitted Mon. Sep. 4th, 2006 12:19 PM
Revision 1 of 1
Scripter sehrgut
Tags bash | Line | Parse
Comments 1 comments
Parsing newline-delimited data records in bash is simple, if you have this odd redirect up your sleeve. An annoying thing about bash is that it usually equates all whitespace characters, so the first block in the snippet won't let you use a file linewise, but will end up echoing each whitespace-delimited token on a separate line.

bash provides the "read" builtin which can be used to differentiate between newlines and spaces.
# This doesn't do what it looks like it does.
for line in $(cat $1); do
        echo $line
# This, however, does.
while read line; do
        echo $line
        done < $1

Keith B
I've got good news, and I've got bad news:
The universe is merely a figment of my imagination.
Now are you ready for the bad news?


Comments You missed something
Wed. Sep. 24th, 2008 5:33 AM    Beginner KennethCC