fields to vars

?>

fields to vars

PROBLEM

You want to affect fields of lines you get from a file to variables

PROBLEM SAMPLE

You want to display only the name, uid and gid from the password file, one information per line or you have a variable that already contains such information.

SOLUTION

IFS (Internal Field separator) is a variable internally used by the shell during line analisys. It contains by default white spaces (space, tab and newline). You can change its content to use the separator you want.

EXEMPLE 1

		SFS="$IFS" # save it
		IFS=":$IFS" # /etc/passwd has ':' as separator
		cat /etc/passwd | while read name uid gid
			do
				echo name: $name
				echo uid : $uid
				echo gid : $gid
			done
		IFS="$SFS" # restore it
		

EXEMPLE 2

		VAR="val1:val2:val3"
		# now we want 3 variables with the 3 values contained in VAR
		SFS="$IFS"
		IFS=":$IFS"
		# suppose arguments are aleady treated
		# set VAR content as arguments, 
		# thus they are analysed by the shell using IFS
		set $VAR
		echo $1
		echo $2
		echo $3