- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- AWK variables
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2003 06:45 AM
02-25-2003 06:45 AM
does anyone know how to reference a variable in awk, i.e.
I want to pick up a specified column from row1 and use it for the output of row2
say we have :
header|1|
a|b|c|d|e|f|
footer|0|
and I wanted to change this to :
header|1|
a|b|c|d|e|f|
footer|1|
but the new footer variable is picked up from the header numeric (in this case 1)
so the following :
header|5|
a|b|c|d|e|f|
footer|2|
needs to be :
header|5|
a|b|c|d|e|f|
footer|5|
thanks in advance gurus!
John
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2003 06:55 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2003 01:16 PM
02-25-2003 01:16 PM
Re: AWK variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2003 03:34 PM
02-25-2003 03:34 PM
Re: AWK variables
/header/ {x=$2;print $0;next;}
/footer/ {sub($2,x);print $0;next;}
{print $0;}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2003 01:05 AM
02-26-2003 01:05 AM
Re: AWK variables
I can't get this to work as its trying to pick up the $2 entry for the footer string and not the header string
I know by default, sub() uses $0 if a third parameter is ommitted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2003 02:03 AM
02-26-2003 02:03 AM
Re: AWK variables
here a little correction to curt's answer:
cat yourfile | awk -F"|" '
/header/ {x=$2;print $0;next;}
/footer/ {sub("|" $2 "|","|" x "|", $0);print $0;next;}
{print $0;}'
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2003 02:22 AM
02-26-2003 02:22 AM
Re: AWK variables
This works for me, and you cut out a process by not using cat/pipe:
awk -F\| '/header/{x=$2}/footer/{sub($2,x)}{print}' yourfile
rgds, Robin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2003 02:46 AM
02-26-2003 02:46 AM
Re: AWK variables
I can simulate curts example, but I am having problems applying to a specific case. The full extent of what I am trying to do is as follows :
original file
-------------
ZHD|MDN0980268|D0215001|R|EMEB|X|BGAS|20030220114355|FORMF||OPER|
490|1100009199347|0|0|1|||||||METER MOVED OUTSIDE
ZPT|3|1111379456
I want to change into this
--------------------------
ZHV|MDN0980268|D0215001|R|EMEB|X|BGAS|20030220114355|FORMF|||OPER|
490|1100009199347|0|0|1|||||||METER MOVED OUTSIDE|
ZPT|MDN0980268|1||1|20030224000001|
But the script I have :
gives me this :
ZHV|MDN0980268|D0215001|R|EMEB|X|BGAS|20030220114355|FORMF|||OPER|
490|1100009199347|0|0|1|||||||METER MOVED OUTSIDE|
ZPT|1|1||1|20030224000001|
I don't understand why the second pipe delimitted field in the last column is '1' and not 'MDN0980268'
yours in confusion
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2003 02:48 AM
02-26-2003 02:48 AM
Re: AWK variables
thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2003 02:50 AM
02-26-2003 02:50 AM
Re: AWK variables
for i in `ls n098*`
do
awk -F"|" '
$1 ~ /^ZHD$/ {x=$2;printf("ZHV|%s|%s|%s|%s|%s|%s|%s|%s|%s||%s|\n",$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)}
$1 ~ /^ZPT$/ {print "ZPT|"sub($2,x)"|1||1|20030224000001|"}
$1 ~ /^490$/ {print $0"|"}' $i > /data/archive2/jb/formfill_new/$i
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2003 03:32 AM
02-26-2003 03:32 AM
Re: AWK variables
The "1" that you are seeing is the return value from the sub function. You should do the substitution before printing, or simply put x in the print statement directly e.g.
awk -F"|" '
$1 ~ /^ZHD$/ {x=$2;printf("ZHV|%s|%s|%s|%s|%s|%s|%s|%s|%s||%s|\n",$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)}
$1 ~ /^ZPT$/ {print "ZPT|"x"|1||1|20030224000001|"}
$1 ~ /^490$/ {print $0"|"}'
rgds, Robin