- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Awk scripting 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
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-03-2003 07:35 AM
02-03-2003 07:35 AM
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2003 07:46 AM
02-03-2003 07:46 AM
Re: Awk scripting Question
cat /tmp/file1 |awk 'BEGIN {found=0;count=0}
found == 1 && count < 10 {print $0;count=count+1}
found == 1 && count >= 10 {found=0;count=0}
$1 ~ /matched1/ {found=1;count=1;print $0}
END'
Share and Enjoy! Ian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2003 07:47 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2003 07:49 AM
02-03-2003 07:49 AM
Re: Awk scripting Question
# print 1 line of context before and after regexp, with line number
# indicating where the regexp occurred (similar to "grep -A1 -B1")
sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2003 07:56 AM
02-03-2003 07:56 AM
Re: Awk scripting Question
#
awk 'BEGIN { lineno=0; goodlineno=0; }
{
# increment line number
lineno+=1;
# does it match our pattern?
if (match($0,/^matched 1/)) {
goodlineno=lineno;
print $0;
} else {
if ((goodlineno) && (lineno < (goodlineno+11))) {
print $0;
} else {
goodlineno = 0;
}
}
}
'
Though I think this is a JOB for perl.
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2003 08:04 AM
02-03-2003 08:04 AM
Re: Awk scripting Question
#!/opt/perl/bin/perl
#
$lineno = 0;
$goodlineno = 0;
while (<>) {
# increment line number
$lineno += 1;
# does it match our pattern?
if ($_ =~ /^matched 1/ ) {
$goodlineno = $lineno;
print $_;
}
else {
if (($goodlineno) && ($lineno < ($goodlineno + 11))) {
print $_;
}
else {
$goodlineno = 0;
}
}
}
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2003 08:14 AM
02-03-2003 08:14 AM
Re: Awk scripting Question
Harry Brown, I am just starting to fool around with perl, where do you enter the file name to parse to get your output in the perl program.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2003 08:14 AM
02-03-2003 08:14 AM
Re: Awk scripting Question
here's a perl one-liner:
perl -ne '{$n=11 if /matched 1/;$n--,print $_ if $n;$n=0 if eof}' logfile
Rgds, Robin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2003 08:17 AM
02-03-2003 08:17 AM
Re: Awk scripting Question
like I would in awk:
cat FILENAME | ./scriptname
Though I really like Robin's solution - quick and easy!
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2003 08:39 AM
02-03-2003 08:39 AM
Re: Awk scripting Question
perl -ne '/matched 1/&&$n=11;--$n&&print' logfile
Enjoy, have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-03-2003 08:41 AM
02-03-2003 08:41 AM
Re: Awk scripting Question
Here's a comparision of perl vs awk in speed against a file this large:
# ls -l match.data
-rw-rw-rw- 1 root sys 46755563 Feb 3 11:05 match.data
# wc match.data
2000000 9866667 46755563 match.data
#
Robin's perl script using "cat FILENAME | perlscript":
real 1:11.2
user 13.7
sys 9.5
My awk using "cat FILENAME | awkscript":
real 1:37.1
user 24.4
sys 16.3
Robin's perl script using "perlscript FILENAME":
real 1:11.2
user 13.7
sys 9.5
NOW notice that the ABOVE tests sent the OUTPUT to the screen, THESE results below are results when the OUTPUT is sent to a FILE:
Robin's perl script:
real 11.4
user 10.9
sys 0.3
My AWK script:
real 20.4
user 19.7
sys 0.3
This proves "perl" is almost twice as fast as "awk".
live free or die
harry