PDA

View Full Version : Cat line-by-line



kvdbreem
October 28th, 2007, 05:03 AM
I have a question

Let's say we want to go through a file line-by-line

How would I do this? I've tried it with cat but that only works if the lines have no spaces. Otherwise, a file whose lines are



hello how are you
i am fine


when processed as follows:


#!/bin/bash
for i in `cat file`
do
echo ${i}
done


will come out as


hello
how
are
you
i
am
fine

ghostdog74
October 28th, 2007, 05:31 AM
what do you want to do with those lines? I hope its not just echoing them out because you can easily do that with tools like awk/cat/more/grep/sed....which internally will "loop" over files for you. however, still to do it in bash , you can use the while loop.



while read line;
do
echo $line
done < "file"

kvdbreem
October 28th, 2007, 06:41 AM
ghostdog:

Thanks for the tip. I had a friend actually asking me about this one at work. I couldn't help very well. I think he needed it for driving another program using paths or something.

cheers

geirha
October 28th, 2007, 02:38 PM
Read about the $IFS variable in the bash documentation, and try
IFS=$'\n'