how to protect white space in for loop

Hi All,

I know there's a really simple answer to this but I just can't think of it :slight_smile:

I'm processing a file which has lines containing white space i.e.

And I want to perform some awk on each line but when I do the following:

for US in $( cat /tmp/unique-strings.tmp | sed 's/\[/\\[/g' | sed 's/\]/\\]/g' | sed 's/\//\\\//g' )
do

echo ""
echo "$US"
echo ""

done

Each line gets broken down into it's component parts like this:

How can I get it to echo each complete line? (In my code I obviously want to do more than just echo the line...)

Thanks,

p.

Sorry I don't get it. Please just show the input file and how the output should look like. If you work each element with for from a list, every element will be echoed line by line, that's as it is. In the example you use sed, not awk :smiley:

Something like this?

awk '{for(i=1;i<=NF;i++){printf("%s\n\n", $i)}}' file

Regards

Sorry, I'm not explaining myself clearly.

I would like the entire sentence to be treated as one string but it's getting split into component parts (each word).

Is there a way I can stop this split occuring?

Don't beat me, but you could just "cat" the file and everything will be fine hehe :wink:
But I guess you will do something with every sentence... I would prefer this

cat infile |\
while read LINE; do
    echo "This is my line:" ${LINE}
    #now do something with it in here
done

If you are going to parse the contents of the line, better use awk or sed like Franklin52 did.

while read record
do
echo "$record" | sed 's/\[/\\[/g' | sed 's/\]/\\]/g' | sed 's/\//\\\//g'| read US
echo ""
echo "$US"
echo ""

done < /tmp/unique-strings.tmp

Brilliant that's what I needed thanks :slight_smile:

Believe it or not it's the first time I've used a while statement in one of my scripts! :slight_smile: