Computing.Net > Forums > Unix > IFS - Help =)

IFS - Help =)

Reply to Message Icon

Original Message
Name: LANkrypt0
Date: January 6, 2003 at 10:28:22 Pacific
Subject: IFS - Help =)
OS: Various
CPU/Ram: Various
Comment:

Just wanted to know if someone could give me a quick usage/tutorial/help thingy on properly and effectively using IFS through KSH.

Thanks in advance.


Report Offensive Message For Removal


Response Number 1
Name: James Boothe
Date: January 7, 2003 at 09:00:05 Pacific
Reply: (edit)

Regarding the read command, you can specify the Internal Field Separator within a pipe so that the main shell environment is not affected, such as:

date +%x | IFS=/ read mo da yr
print $mo $da $yr

When doing line-by-line processing, and you want to retain all white space as is, you can set IFS to null so that the read command will treat each line as a single word with white space intact, such as:

IFS=""
while read line
do print $line
done < myfile

"unset IFS" is not the same as setting to null. unset puts it back to its default of white space as field separation.


Report Offensive Follow Up For Removal

Response Number 2
Name: LANkrypt0
Date: January 7, 2003 at 12:29:36 Pacific
Reply: (edit)

Thanks. Quick question though.
Setting to the null (IFS=/) would have no separations, correct? You would not be able to use that and do a read x y z ? Also after setting IFS to something, before I begin the next continue, I would need to reset it back to whitespace (IFS=" "), correct?


Report Offensive Follow Up For Removal

Response Number 3
Name: James Boothe
Date: January 7, 2003 at 13:49:10 Pacific
Reply: (edit)

Yes, setting IFS to null would eliminate field separation, but you would still have line separation. So if you did read x y z, each full line would be treated as a single field (white space included) and would go into x, and nothing into y or z.

Yes again. If you change the IFS setting in your main shell, it remains that way until changed again. But this construct:

IFS=, read x y z

impacts only the one read command and does not impact the shell.

However, setting IFS to a single space does not get it back to default, which is white space. With IFS set to a single space, tab characters would be seen as data and not as separation. To get back to default, do "unset IFS".


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: IFS - Help =)

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge