Hi everyone!
How can I parse a delimited field using AWK?
For example, if I have lastName#firstName or lastName*firstName. I'd like an AWK script that would return lastName and then another that would return firstName? Is this possible?
Hi everyone!
How can I parse a delimited field using AWK?
For example, if I have lastName#firstName or lastName*firstName. I'd like an AWK script that would return lastName and then another that would return firstName? Is this possible?
You mean like this?:
# echo "Fat#Bob" | nawk -F"[*#]" '{print $2,$1}'
Bob Fat
# echo "Fat*Bob" | nawk -F"[*#]" '{print $2,$1}'
Bob Fat
HTH
No I meant more like if I have a string like fat#bob
I get...
Using one AWK script would return the last name, in this case fat.
Another AWK script would return the first name, in this case bob.
Not necessary to use awk:
Name="lastName#firstName"
LastName=${Name%\#*}
FirstName=${Name##*\#}
# echo lastName#firstName |awk -F# '{print $1}'
lastName
# echo lastName#firstName |awk -F# '{print $2}'
firstName
Like so?
$ s="fat#bob"
$ echo ${s%#*}
fat
$ echo ${s#*#}
bob
@Scrutinizer, it doesn't give the right output on a HP-UX system:
$ s="fat#bob"
$ echo ${s%#*}
fat#bob
$ echo ${s#*#}
fat#bob
Regards
Which version of HP-UX and which shell are you using?
$ s="fat#bob";echo ${s%#*};echo ${s#*#}
fat
bob
$ uname -sr
HP-UX B.11.11
$ ps -p$$
PID TTY TIME COMMAND
23283 pts/ta 0:00 ksh
$
Consider that the above mentioned parameter expansion is required by SUS/POSIX, so on your system you may need to use a POSIX compliant shell.
---------- Post updated at 12:37 PM ---------- Previous update was at 12:31 PM ----------
I see that on HP-UX B.11.11 /usr/bin/sh(a M-ksh88f) is labeled as POSIX shell.
I dont know why it doesn't support the parameter expansion mentioned above.
The version on my system is:
uname -sr
HP-UX B.11.00
Yes, HP-UX's /usr/bin/sh is a ksh Version M-11/16/88f and it doesn't support that parameter expansion.
On those systems /usr/bin/ksh, which is actually ksh88c, supports it.
Hi Franklin, your version seems a tad old (Support for HP-UX 11.0 (B.11.00) ended December 2006.). Do you have to escape the # to get it to work? Perhaps the have been bug fixes?
Right, it works well if I escape the # like:
s="fat#bob"
echo ${s%\#*}
echo ${s#*\#}
Regards
OK
So ksh Version M-11/16/88f supports that parameter expansion, but it has some bugs (or this behavior is documented).
Thanks for all the help everyone!