Operating System - HP-UX
1833777 Members
2211 Online
110063 Solutions
New Discussion

Is it possible to change the column separator for the read statement?

 
SOLVED
Go to solution
Jack C. Mahaffey
Super Advisor

Is it possible to change the column separator for the read statement?

I got a script that reads a text file that has the columns separated by colons (:). Is it possible to change the default separator for the read command.

My read statement looks like the following:

while read -r col1 col2 col3 col4 col5
do
...
done < ${vgconfigfile}


I know I can do it in perl. Can I do it with the shell?

jack...
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Is it possible to change the column separator for the read statement?

save=${IFS}
IFS=:
read -r col1 col2 ...

IFS=${save}

IFS is the boy you want to change. Man sh_posix for details and look for Internal Field Separator
If it ain't broke, I can fix that.
S.K. Chan
Honored Contributor

Re: Is it possible to change the column separator for the read statement?

Try this in your script. Use the IFS statement to define your field separator.
#!/usr/bin/sh
IFS=:
...
while read -r col1 col2 col3 col4 col5
do
...
....
Jordan Bean
Honored Contributor

Re: Is it possible to change the column separator for the read statement?


Or, to alter the IFS just for the read statement and not the rest of the script, do it this way:

#!/usr/bin/sh
while IFS=: read -r col1 col2 ...
do
...
done < ${vgconfigfile}

Jack C. Mahaffey
Super Advisor

Re: Is it possible to change the column separator for the read statement?

Thanks all..

Jordan, thanks for the inline example.

jack...