I am getting a variable as x=2006/01/18
now I have to extract each field from it.
Like x1=2006, x2=01 and x3=18.
Any idea how?
Thanks a lot for help.
Thanks
CSaha
I am getting a variable as x=2006/01/18
now I have to extract each field from it.
Like x1=2006, x2=01 and x3=18.
Any idea how?
Thanks a lot for help.
Thanks
CSaha
[/tmp]$ echo "2006/01/18" | { IFS="/" read a b c; echo $a $b $c ; }
2006 01 18
On a deeper note, I think I know where you are heading to...
If that solution does not work, pick up some clues from this thread Breaking input with "read" command
echo "2006/01/18" | awk -F"/" '{print "x1="$1",x2="$2 ",x3=" $3}'
Excellent Vino !!!
After posting I tried this and resolved
$ echo "2006/01/18" | awk -F"/" '{print $1}'
2006
$ echo "2006/01/18" | awk -F"/" '{print $2}'
01
$ echo "2006/01/18" | awk -F"/" '{print $3}'
18
See this also ...
here is one more in sed,
echo 2006/01/18 | sed 's/\(.*\)\/\(.*\)\/\(.*\)/year: \1 month: \2 day: \3/'
year: 2006 month: 01 day: 18
You can also use 'cut'
year=`echo 2006/01/18 | cut -d/ -f1`
month=`echo 2006/01/18 | cut -d/ -f2`
day=`echo 2006/01/18 | cut -d/ -f3`
But, i think tah the better solution is Vino post
echo 2006/01/18 | IFS=/ read year month day
Jean-Pierre.