awk & cut record separator problem

Hi All,

I've got some strange behaviour going on when trying to manipulate a file that contains spaces.

My input file looks something like this:

xxxxxxxxx,yyyy,sss sss sss,bbbbbbb

If I use awk:

When running from the command line I get:

sss sss sss

But when running from a loop I get:

My loop looks like this:

for LINES in $( cat tgp.txt )
do  TITLE=$( echo "$LINES" | awk -F'~' '{ print $3 }' )
echo "$TITLE"
done

I get the same behaviour for cut commands like this:

Has some sort of system input/output field separator been changed? Or am I just doing something wrong? :slight_smile:

Thanks, p.

Add following line

IFS=","    

or

IFS=""

Here's some more testing to prove my point:

I am right in thinking this is not normal behaviour?

Sorry, must've posted my reply at the same time...

Where do I put this? in my shell script?

fpmurphy - you were right! Thanks :slight_smile:

I just had to tweek it like this:

i.e. setting it to NULL.

I put this in my script before the cut's and it's doing the job!

Thanks again,

p.

Interestingly the next point in my script where a file had to be manipulated exhibited the same problem - this file only had one field but the script saw just one entry eventhough there are many lines... I found that adding:

fixed this problem... very weird.

seriously, for what you are doing in the above code, the "better" way is just to use awk. (or a while loop to iterate lines in files , with IFS set. )

HI,

It is boc the space between sss and sss, command cat will tread them as differen lines. So below content in your script will generate three lines but not only one single line. Anyway, while loop can solve your problem.

xxxxxxxxx,yyyy,sss sss sss,bbbbbbb
while read line
do 
echo $line | awk 'BEGIN{FS=","}{print $3}'
done < filename