Solved AWK to print last field including SPACE

September 28, 2012 at 03:29:08
Specs: linux
I have simple test.sh script, see below:

bill_code=`echo $record | awk -F"|" '{print $1}'`
Fullname=`echo $record | awk -F"|" '{print $3}'`
email=`echo $record | awk -F\ '{print $4}'`

The last field contains spaces: see csv below:
A0222|Y|DELACRUZ|-cc dell@yahoo.com -cc support@yahoo.com

When I run the program, then it return below:
A0222
DELACRUZ
-cc
dell@yahoo.com
-cc
support@yahoo.com


Wherein the email was concatenated. I expect to have like below:
A0222
DELACRUZ
-cc dell@yahoo.com -cc support@yahoo.com

It appears that when this program hit the space then it treat as delimiter.

Please advise, what would be the best code for this?

Thanks in advance.
quay


See More: AWK to print last field including SPACE

Report •

✔ Best Answer
October 3, 2012 at 08:25:17
It looks to me like bash 3.0 has a bug with parsing using command line substitution. Try using the bourne shell, sh, or korn shell, ksh, and your original code probably works.

If you insist on using your original code, I recommend upgrading your version of the bash shell from here:

http://www.gnu.org/software/bash/

My work around does NOT use command line substituion:

OFS="$IFS"
IFS="|"
set $record
IFS="$OFS"

So, your options are to use a different shell, upgrade your version of the bash shell, or use the work around I showed you.





#1
September 28, 2012 at 07:14:26
You need to fix the field seperator:

email=`echo $record | awk -F"|" '{print $4}'`


Report •

#2
September 28, 2012 at 17:26:26
THanks Nails.

my apology, I the field seperator should be "|", you are correct but I got same result.


Report •

#3
September 29, 2012 at 13:12:11
Interesting problem. Using your code, I am getting the correct results with SUSE Linux 10.3 using the Bash shell version 3.1.17.

However, I have seen some shells where command substitution splits variables at spaces although you have told it not to.

Try using the set command to do the parsing where the results are stored in the command-line parameters:

record="A0222|Y|DELACRUZ|-cc dell@yahoo.com -cc support@yahoo.com"
OFS="$IFS"
IFS="|"
set $record
IFS="$OFS"
echo $1
echo $3
echo $4

I am interested in what shell you are using and whether the above solution works for you.

If it does work, it should be more efficient than three external awk calls.


Report •

Related Solutions

#4
October 2, 2012 at 20:24:57

*.genesis.intranet.asia;118.230.*;*.intranet.asia;*.genesis.net;18.37.*;*.hahaha.net;*.pruasia;118.230.*;118.230.*.*;127.0.0.*;18.37.*.*;websvr01.ism.net.my;vpnwww.ism.net.my;ismncd.ism.net.my;192.168.*;*.ism.net.my;sfaepl1.prufunds-sfa.com;prue-hr.hahaha.net;pcaweb.intranet.asia;governance.hahaha.net;pamsg.intranet.asia;*.pamsg.intranet.asia

Report •

#5
October 2, 2012 at 21:04:17
Nails: Related to your question.
OS version is AIX 3 5
Bash version is: GNU bash, version 3.00.16(1)-release

Thanks for your code. I appreciate and learn from that method. Meantime, I manage to achieve what I am trying to get, by using sed command however the record in last field , I should put "_" underscore, because spaces always bring treat as delimiter. To share, see below:

With this record:
A0222|Y|DELACRUZ|-cc_dell@yahoo.com_-cc_support@yahoo.com

bill_code=`echo $record | awk -F"|" '{print $1}'`
AUTH=`echo $record | awk -F"|" '{print $3}'`
email=`echo $record | awk -F"|" '{print $4}'`

echo $bill_code
echo $AUTH
echo $email
echo $email | sed 's/_/ /g'

Result are:
A0222
DELACRUZ
-cc_dell@yahoo.com_-cc_support@yahoo.com
-cc dell@yahoo.com -cc support@yahoo.com --> this is goal.


Space is still my riddle, how to figure it out. Above is only a workaround.
Thanks.


Report •

#6
October 3, 2012 at 08:25:17
✔ Best Answer
It looks to me like bash 3.0 has a bug with parsing using command line substitution. Try using the bourne shell, sh, or korn shell, ksh, and your original code probably works.

If you insist on using your original code, I recommend upgrading your version of the bash shell from here:

http://www.gnu.org/software/bash/

My work around does NOT use command line substituion:

OFS="$IFS"
IFS="|"
set $record
IFS="$OFS"

So, your options are to use a different shell, upgrade your version of the bash shell, or use the work around I showed you.




Report •

#7
October 4, 2012 at 03:07:03
Ok sounds perfect.. Thanks and noted.


Report •

Ask Question