I am using an awk command to extract a particular portion of a string. Below is the command and its output on a Linux system:
oracle@host1:/tmp [default] (/home/oracle)
$uname -a
Linux host1 2.6.32-279.39.1.el6.x86_64 #1 SMP Fri Nov 15 05:38:26 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
oracle@host1:/tmp [default] (/home/oracle)
$echo "/u01/app/oracle/product/11.2.0.3/bin/tnslsnr;LISTENER;-inherit"|awk -F'/' '{for(i=1;i<NF;i++)NF=6;print $0}'|tr ' ' '/'
/u01/app/oracle/product/11.2.0.3
But the same command doesn't give desired output on an HP-UX system.
Below is the output from HP-UX System
[default](host2)/home/oracle-> uname -a
HP-UX host2 B.11.31 U ia64 1553842798 unlimited-user license
[default](host2)/home/oracle-> echo "/u01/app/oracle/product/11.2.0.3/bin/tnslsnr;LISTENER;-inherit"|awk -F'/' '{for(i=1;i<NF;i++)NF=6;print $0}'
/u01/app/oracle/product/11.2.0.3/bin/tnslsnr;LISTENER;-inherit
[default](host2)/home/oracle->
Can you guys help me understand why this command is not working on HP-UX. Also how do i get same result on HP-UX
RudiC
July 31, 2014, 7:41am
2
I don't know anything about HP-UX, but your awk- version might not like the NF assignment. Try
echo "/u01/app/oracle/product/11.2.0.3/bin/tnslsnr;LISTENER;-inherit"|awk -F'/' -vOFS=\/ '{print $1,$2,$3,$4,$5,$6}'
1 Like
Since you haven't changed anything to reconstruct the line, you get the output. Try $1 = $1
echo "/a/b/c/d/e/f/g/r/d/c/g" | awk -F '/' '{NF=6; $1=$1}1' OFS=/
1 Like
Hi Srinishoo,
Can you please explain me your command
what does $1=$1}1' mean in the below code?
echo "/a/b/c/d/e/f/g/r/d/c/g" | awk -F '/' '{NF=6; $1=$1}1' OFS=/
CarloM
July 31, 2014, 8:30am
5
On Linux you're most likely running mawk or GNU awk, both of which will re-calculate $0 if you assign to a field (or change NF, although that might depend on the version).
Whatever HP-UX is using probably does not (AIX awk doesn't, for instance) - you'd need to output the fields individually as RudiC suggested.
1 Like
One more command, can someone help me in modifiying this command to work on HP-UX system
On Linux:
#-> echo "/u01/app/oracle/diag/tnslsnr/hpuxdgl1/listener/alert/log.xml"|awk -F'/' '{for(i=1;i<=NF;i++) if ($i ~ /diag/) NF=i;print $0}' OFS=/
/u01/app/oracle/diag
On HP-UX
$ echo "/u01/app/oracle/diag/tnslsnr/hpuxdgl1/listener/alert/log.xml"|awk -F'/' '{for(i=1;i<=NF;i++) if ($i ~ /diag/) NF=i;print $0}' OFS=/
/u01/app/oracle/diag/tnslsnr/hpuxdgl1/listener/alert/log.xml
---------- Post updated at 07:11 PM ---------- Previous update was at 07:07 PM ----------
Please ignore my latest query..i think i could figure out the way get same result on HPUX
echo "/u01/app/oracle/diag/tnslsnr/hpuxdgl1/listener/alert/log.xml"|awk -F'/' '{for(i=1;i<=NF;i++) if ($i ~ /diag/) NF=i;$1=$1}1' OFS=/
/u01/app/oracle/diag
Srinishoo,
Your solution is working for me, but can you please explain me what does $1=$1 stand for ?
When you are printing the entire line, even though you make changes to the number of fields, you have to make changes within the line to reflect new number of fields
Here, $1=$1 is assigning the value of first field to the first field...
You can replace that part with $2=$2 as well
CarloM
July 31, 2014, 10:30am
8
$1=$1
just assigns field 1 to whatever value it already has - the point is to force the input record ($0) to be rebuilt.
The 1
prints the (by now rebuilt) input line. It's a pattern which is always true and the default action which is print $0.