h1

Shell Tips 7: The Input Field Separator Trick

December 7, 2010

There is an environment variable called IFS which can perform simple tokenisation on input. By defulat
IFS is set to whitespace, blanks, tabs etc.
OLDIFS=”$IFS” #Save original field separator
IFS=”,”
Line=”a,b,c,d” #Now separate on commas
let Count=0
for item in $Line
do
let Count=Count+1
echo “Item $Count = $item”
done
IFS=”$OLDIFS” #Restore original field separator

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: