Operating System - Linux
1753809 Members
8327 Online
108805 Solutions
New Discussion юеВ

Changing IFS within a shell script

 
SOLVED
Go to solution
Michele (Mike) Alberton
Regular Advisor

Changing IFS within a shell script

Hi !

I need to access different column data files within the same shell script, but they use different field separators so that before doing

while read A B C
do
...
done < file.dat

I need to setup IFS to ";" for instance.

Once done that I would like to reset IFS to the original value (tabs, spaces) but doing

IFS="", or IFS=" " doesn't work like at the beginning.

How can I do that ?

At the beginning running

echo $IFS"+"

gives me back

$+

so that I thought the default value was "", but it's not that easy ... sigh.

Thanks in advance !

Cheers,

Mike

 

 

P.S. This thread has been moved from HP-UX > General to HP-UX > languages - HP Forums moderator

9 REPLIES 9
john korterman
Honored Contributor
Solution

Re: Changing IFS within a shell script

Hi,
have you tried the simple approach; save the original value at the very beginning of your script:
SAVE_IFS="$IFS"
and then restore the value later...

IFS="$SAVE_IFS"

regards,
John K.

it would be nice if you always got a second chance
Massimo Bianchi
Honored Contributor

Re: Changing IFS within a shell script

Ohters will tell you better trick and solution, mine is a workaround...

at the beginning of the script

SAVE_IFS=$IFS


...
work as needed
...

at the end of the script.

IFS=${SAVE_IFS}


Massimo

Stefan Farrelly
Honored Contributor

Re: Changing IFS within a shell script

The default value for IFS is a newline, not a null. In any posix shell I get;

echo "A${IFS}B"
A
B

Im from Palmerston North, New Zealand, but somehow ended up in London...
Michele (Mike) Alberton
Regular Advisor

Re: Changing IFS within a shell script

That's why I love this FORUM !

Simply great, thanks to both, well earned !

Cheers,

Mike
David Totsch
Valued Contributor

Re: Changing IFS within a shell script

You could also do it like this:

(
IFS="${IFS}|"
while read A B C
do
.
.
.
done
)

The parenthesis set-up a sub-environment. You can even cd(1) within the parenthesis and outside them go back to where you were w/o having to record where you came from.

Enjoy,
-dlt-
Michele (Mike) Alberton
Regular Advisor

Re: Changing IFS within a shell script

Thanks David,

I implemented the backup and restore approach, but definitely this looks very elegant.
I'm still wondering if there's any chance to reset the value of IFS, without the need of restoring it through an external var.├Г┬╣
I odumped the value and it really looks a NULL, but it is not...

Again, thanks !

Mi
Michael Schulte zur Sur
Honored Contributor

Re: Changing IFS within a shell script

Hi Mike,

thats just, what Davids approach does.
() creates a subenvironment, which ceases to exist after ), so you can do to IFS all you want and no need to restore it.

right Dave?

Michael
curt larson_1
Honored Contributor

Re: Changing IFS within a shell script

the default value of IFS is the space character, the tab character, and the newline
IFS=" \t\n"

do an print $IFS | vis -n to see the characters
Michele (Mike) Alberton
Regular Advisor

Re: Changing IFS within a shell script

Usage of parenthesis to group the set of instructions using the new IFS worked ok.
Thanks to all those who helped me in this