- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Yet another awk question...
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
Discussions
Discussions
Forums
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
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
тАО10-15-2004 08:42 AM
тАО10-15-2004 08:42 AM
Yet another awk question...
cat file.txt
Start
id fname lname
-- ----- -----
1 John Wayne
2 Bill Smith
Stop
Start
id fname lname
-- ----- -----
Stop
Start
id fname lname
-- ----- -----
1 Mark Williams
Stop
cat file.awk
awk '/Start/{x=1;y=0}/Stop/{if (y>3){l[y]="\n"; while(i++
Outputs:
id fname lname
-- ----- -----
1 John Wayne
2 Bill Smith
id fname lname
-- ----- -----
1 Mark Williams
How do I print a line before the header...
eg:
This report was run on Sat. May 1, 2005
id fname lname
-- ----- -----
1 John Wayne
2 Bill Smith
This report was run on Sat. May 1, 2005
id fname lname
-- ----- -----
1 Mark Williams
Thanks
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-15-2004 08:45 AM
тАО10-15-2004 08:45 AM
Re: Yet another awk question...
awk 'BEGIN{print "today is something"};/Start/{x=1;y=0}/Stop/{if (y>3){l[y]="\n"; while(i++
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-15-2004 08:53 AM
тАО10-15-2004 08:53 AM
Re: Yet another awk question...
today is something
id fname lname
-- ----- -----
1 John Wayne
2 Bill Smith
id fname lname
-- ----- -----
1 Mark Williams
Is there a way to get it to print the header every time it sees Start??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-15-2004 08:55 AM
тАО10-15-2004 08:55 AM
Re: Yet another awk question...
Just add an "if" statement-
awk '/Start/{x=1;y=0}/Stop/{if (y>3){l[y]="\n"; while(i++
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-15-2004 09:00 AM
тАО10-15-2004 09:00 AM
Re: Yet another awk question...
produces:
This report was run on Sat May 1, 2005
This report was run on Sat May 1, 2005
This report was run on Sat May 1, 2005
This report was run on Sat May 1, 2005
This report was run on Sat May 1, 2005
Start fname lname
Start ----- -----
Start John Wayne
Start Bill Smith
This report was run on Sat May 1, 2005
This report was run on Sat May 1, 2005
This report was run on Sat May 1, 2005
This report was run on Sat May 1, 2005
This report was run on Sat May 1, 2005
This report was run on Sat May 1, 2005
This report was run on Sat May 1, 2005
This report was run on Sat May 1, 2005
This report was run on Sat May 1, 2005
Start fname lname
Start ----- -----
Start Mark Williams
This report was run on Sat May 1, 2005
This report was run on Sat May 1, 2005
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-15-2004 09:02 AM
тАО10-15-2004 09:02 AM
Re: Yet another awk question...
If you want it printed when "Start" is found, why not add it their.
awk '/Start/{x=1;y=0; print "This report was run on Sat May 1,2005"}/Stop/{if (y>3){l[y]="\n"; while(i++
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-15-2004 09:06 AM
тАО10-15-2004 09:06 AM
Re: Yet another awk question...
id fname lname
-- ----- -----
It still prints the header line...
This report was run on Sat May 1, 2005
id fname lname
-- ----- -----
1 John Wayne
2 Bill Smith
This report was run on Sat May 1, 2005
This report was run on Sat May 1, 2005
id fname lname
-- ----- -----
1 Mark Williams
Maybe a nested if would work?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-15-2004 02:51 PM
тАО10-15-2004 02:51 PM
Re: Yet another awk question...
Ah, a continuation of an earlier threat.
That solution came form me.
When it sees 'start', it merely starts counting and remembering
When it sees 'stop' AND has seen enough lines it starts to dump its memory with a while loop.
So, for this new requirement you need to add a print in the 'stop' section, just before executing the while... print.
As per above.
In more readable form:
awk '
/Start/{x=1;y=0}
/Stop/ {if (y>3) {
l[y]="\n";
print "hello";
while(i++
x=i=0
}
{l[y]=$0;
y+=x}
'
As an alternative you can also overwrite the 'start' line with the desired header, and have the while loop print it out eventually by tweaking its start and end:
awk '/Start/{x=1;y=0;$0="hello"}/Stop/{if (y>3){l[y]="\n"; while(i<=y){print l[i++]}};x=i=0}{l[y]=$0;y+=x}'
Cheers,
Hein
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-15-2004 09:48 PM
тАО10-15-2004 09:48 PM
Re: Yet another awk question...
or pribably a better solution is
perl -ane 'INIT{print "hello world"}; if(/Start/ .. /Stop/) { print "@F\n"}'
but I've recently decided to ditch awk for perl...
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-16-2004 05:34 AM
тАО10-16-2004 05:34 AM
Re: Yet another awk question...
# awk '/Start/{x=1;y=0}/Stop/{if (y>3){l[y]="\n"; if ($l[1]="id") { print "This report was run on Sat. May 1, 2005" }
> while(i++
id fname lname
-- ----- -----
1 John Wayne
2 Bill Smith
This report was run on Sat. May 1, 2005
id fname lname
-- ----- -----
1 Mark Williams
HTH.