- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- AWK: 1st line to blank line, plus 4
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
05-18-2004 08:10 AM
05-18-2004 08:10 AM
I have a short awk script that prints the first line to the first blank line:
awk 'NR==1,/^$/' $1
How could I make this print to the first blank line, -plus- four lines?
e.g. in file "test"
"firstline
stuff
also stuff
first after break
foo
bar
fourth
for bridge
also also stuff"
I'd like to print from "firstline" to "fourth."
Thanks,
Rob Pierce
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2004 08:17 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2004 08:22 AM
05-18-2004 08:22 AM
Re: AWK: 1st line to blank line, plus 4
print $0;
if ( $0 ~ /^$/ ) {
for ( i=1;i<4;i++) {
getline;print $0;
}
exit;
}}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2004 08:30 AM
05-18-2004 08:30 AM
Re: AWK: 1st line to blank line, plus 4
Works like a champ!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2004 08:31 AM
05-18-2004 08:31 AM
Re: AWK: 1st line to blank line, plus 4
awk '{print $0}
$0 ~ /^$/ {
for (i = 1; i < 4; i++) {
getline; print $0;
exit;
}}'
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2004 08:34 AM
05-18-2004 08:34 AM
Re: AWK: 1st line to blank line, plus 4
awk '{print $0}
$0 ~ /^$/ {
for (i = 1; i < 4; i++) {
getline; print $0;
}
exit;
}'
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2004 08:35 AM
05-18-2004 08:35 AM
Re: AWK: 1st line to blank line, plus 4
either way will work. as will
awk '
NR==1,/^$/ {$1}
/^$/ {
for (i = 1; i < 4; i++) {
getline; print $0;
exit;}
}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2004 08:39 AM
05-18-2004 08:39 AM
Re: AWK: 1st line to blank line, plus 4
lt09:/tmp 110 > awk '{print}/^$/{for(i=1;i<=4;i++){getline;print}exit}' xx
kwerhqlre
serths
serths
serths
serths
yh
rharthrtharthh
serths
hsdrhare
dthsrh
lt09:/tmp 111 > perl -ne'(1../^$/)||$n++<4 and print' xx
kwerhqlre
serths
serths
serths
serths
yh
rharthrtharthh
serths
hsdrhare
dthsrh
lt09:/tmp 112 >
BUT!! What if there are not 4 lines after the empty line:
lt09:/tmp 112 > awk '{print}/^$/{for(i=1;i<=4;i++){getline;print}exit}' xx
kwerhqlre
serths
serths
serths
serths
yh
rharthrtharthh
rharthrtharthh
rharthrtharthh
rharthrtharthh
lt09:/tmp 113 > perl -ne'(1../^$/)||$n++<4 and print' xx
kwerhqlre
serths
serths
serths
serths
yh
rharthrtharthh
lt09:/tmp 114 >
See, the perl version is safe here, the awk version is not.
Feel free to leave this pointless. It's to make up for my miss :)
Enjoy, Have FUN! H.Merijn [ testing his awk skills again ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2004 10:17 AM
05-18-2004 10:17 AM
Re: AWK: 1st line to blank line, plus 4
Here my awk without the getline command:
awk '
/^$/ {flag=1 ; count=0}
{
if (! flag || count<5){
count++
}
}' test
Frank.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2004 11:53 AM
05-18-2004 11:53 AM
Re: AWK: 1st line to blank line, plus 4
Just being silly and pointless ...
Here my awk without everything.
Look ma.. no hands.
Still works though:
awk '{p+=x}/^$/{x=1}(p<5)' test
It combines the count and flag from Frank's example. A counter p is incremented by x where x starts out null. Ones an empty line is seen, x becomes one and the counter really starts. It prints through a default { print } action on any pattern test. That pettern is to see wether the counter exceeded the target.
I would NOT want to find this in production code. Impossible to explain!
Hein.