Linux Unix help !!

"Give respect to Time, One day at right Time, Time will respect You"

Tuesday, January 4, 2011

IFS bash variable

The IFS internal variable
One way around this problem is to change Bash's internal
IFS (Internal Field Separator) variable so that it splits
fields by something other than the default whitespace
(space, tab, newline), in this case, a comma.

eg:
#!/bin/bash
IFS=$',' # seperate input by comma
vals='/mnt,/var/lib/vmware/Virtual Machines,/dev,/proc,/sys,/tmp,/usr/portage,/var/tmp'
for i in $vals; do echo $i; done
unset IFS

with unset IFS, so that it returns to its default value.
This will avoid any potential problems that could arise
in the rest of your script.


$IFS (internal field separator) : This variable determines how Bash recognizes fields,
or word boundaries, when it interprets character strings.
$IFS defaults to whitespace (space, tab, and newline), but may be changed, for example,
to parse a comma-separated data file. Note that $* uses the first character held in $IFS.


Fields seperator:
echo $datapath | awk -F "/" '{print $NF}'|awk 'BEGIN { FS="[- . ]" } {print $1}'

The logic is that the intialisation
BEGIN{FS="[ ]|[;]|[,]"} ; OFS=","}
sets up the Field Separator variable FS as a regular expression which matches any one of " " ";" ","
and the Output Field Separator OFS to be ",".

 

No comments:

Post a Comment

Write Here .. your comments are always wellcome ..but no spam please !!

Followers

Pls LIKE my Story !!!