splitting tab delimited strings

hi
i have a requirement to input a string to a shell script and to split the string to multiple fields,
the string is copied from a row of three columns (name,age,address) in an excel sheet.
the three columns (from excel) are seperated with a tab when pasted in the command prompt, but when the
string is assigned to a variable, the tab disappears to a space.
code is given below

 
echo "Enter the string : \c"
read line
name=`echo $line | awk -F"\t" '{print $1}'`
age=`echo $line | awk -F"\t" '{print $2}'`
address=`echo $line | awk -F"\t" '{print $3}'`

output

+ echo Enter the string : \c
Enter the string : + read line
john teth       45      1st cross, stmark road
+ + echo john teth 45 1st cross, stmark road
+ awk -F\t {print $1}
name=john teth 45 1st cross, stmark road
+ + echo john teth 45 1st cross, stmark road
+ awk -F\t {print $2}
age=
+ + echo john teth 45 1st cross, stmark road
+ awk -F\t {print $3}
address=

i would like to get
name=john teth
age=45
address=1st cross, stmark road

can any one guide me ...

In order to preserve the tabs, you must double-quote $line.

Simplest solution is to do the splitting with the read statement:

IFS=$'\t' read name age address

If your shell doesn't support that string syntax, then use a literal tab (control-v tab) in a single-quoted string.

Regards,
Alister

1 Like

I couldn't get it done by this way

IFS=$'\t' read name age address

i changed to

 
IFS=" " read name age address

and was working

thanks