- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Help in Grep/ VI
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
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
тАО07-20-2009 06:12 AM
тАО07-20-2009 06:12 AM
I have a 100MB text file in my HP UX server. It's a log file for license usage. I need to grep today's license usage and need to be sent to me in the EOD.
Problem is the date appears in the log file as Jul 17 2009, and i need to copy a single line above its appearance and 3 lines below this appearance.
The files looks similar to this format
User : ccxc 1.00License
Group :xxx In-Use Since: Jun 29 2006 09:57:57
Node : xxxx.xxx.com Serial Number:
Acid: Capacity Type: None
( This is a sample of single user usgae detail. Like this lot of appearances are there in the file. )
Since the date appears in the middle of this, i need to get all the four lines in my mail.
Please help me in this
Thanks in Advance,
Din
Solved! Go to Solution.
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2009 06:24 AM
тАО07-20-2009 06:24 AM
Re: Help in Grep/ VI
grep User logfile > license.txt
grep Group logfile >> license.txt
grep Node logfile >> license.txt
grep Acid logfile >> license.txt
You may need to refine the statement or create a temporary file with tail, but this is the basic approach I would use.
I'm sure you could process the file with awk as well.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2009 06:32 AM
тАО07-20-2009 06:32 AM
Re: Help in Grep/ VI
# cat .findit
#!/usr/bin/perl
use strict;
use warnings;
my $target = shift or die;
my ( $prev, $n ) = ( '', 0 );
while (<>) {
if (/$target/) {
print $prev, $_;
$n=3;
}
elsif ($n > 0) {
$n--;
print;
}
$prev = $_;
}
1;
...run as:
# ./findit pattern file
# ./findit "Jul 17" file
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2009 06:39 AM
тАО07-20-2009 06:39 AM
Re: Help in Grep/ VI
If you have a GNU version of 'grep' installed, you could do:
# grep -A3 -B1 "Jul 17" file
...where '-A' stands for "lines after" and '-B' stands for "lines before".
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2009 07:05 AM
тАО07-20-2009 07:05 AM
Re: Help in Grep/ VI
I don't have GNU grep installed on my HP UX server.
Thanks,
Din
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2009 07:35 AM
тАО07-20-2009 07:35 AM
Re: Help in Grep/ VI
If i try as you said, i will get the output, but it will not be in the right format. I need the format as it's available in the log file.
Like a set of sections arrange in the log file.
Thanks in advance,
Din
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2009 08:24 AM
тАО07-20-2009 08:24 AM
Re: Help in Grep/ VI
Since you don't have GNU 'grep' and you don't seem to like Perl, how about 'awk':
# cat ./showit
#!/usr/bin/awk -f
BEGIN{prev=""; n=0; seen=0};
{if ($0~target) {
printf "%s\n%s\n", prev, $0;
seen=1;
n=3;
}
if (n > 0 && seen < 1) {
n--;
printf "%s\n", $0;
}
prev = $0;
seen = 0;
}
...run as:
# ./showit -v target="Jul 17" file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2009 11:26 AM
тАО07-20-2009 11:26 AM
Solution>Steven: grep User logfile > license.txt
grep Group logfile >> license.txt
grep Node logfile >> license.txt
grep Acid logfile >> license.txt
Instead of using 4 greps, you can do them all at once and the lines will be in order:
grep -e "^User:" -e "^Group:" -e "^Node:" -e "^Acid:" logfile > license.txt
But that will find more than your one date.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2009 11:56 PM
тАО07-20-2009 11:56 PM
Re: Help in Grep/ VI
sed -n '/User :/,/Acid: /p'
This prints from line containing User: (included0 to line containg Acid: (included)
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2009 06:01 AM
тАО07-21-2009 06:01 AM
Re: Help in Grep/ VI
How can i do this...?
Thanks in Advance,
Din
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2009 06:20 AM
тАО07-21-2009 06:20 AM
Re: Help in Grep/ VI
> I need to take out the content between Jul 17 2009's first appearance and Jul 17 2009's last appearance.
# perl -e 'while (<>) {print if /Jul 17/.../Jul 17/}'
...will provide the content. If it occurs multiple times you will see that too. Is that what you want, or do you _only_ want the first stanza that meets the criteria?
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2009 06:31 AM
тАО07-21-2009 06:31 AM
Re: Help in Grep/ VI
...and of course there is always:
# sed -ne '/Jul 17/,/Jul 17/p' file
Regards!
...JRF...
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-22-2009 06:06 AM
тАО07-22-2009 06:06 AM
Re: Help in Grep/ VI
The key would be to have a daily log file.
Let's suppose your log file is licenses.log. Every day, a cron jog would:
- email licenses.log to you
- append licenses.log to licenses.history (your 100MB file)
- erase licenses.log (or empty it)
This approach has the advantage that your script is completely independant of the syntax of the content.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-22-2009 07:24 AM
тАО07-22-2009 07:24 AM
Re: Help in Grep/ VI
The command that you gave has really worked out, but it's only 80% accurate.
But somehow i managed it by editing manually. Thanks for your inputs.
Regards,
Din
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-22-2009 07:25 AM
тАО07-22-2009 07:25 AM
Re: Help in Grep/ VI
Thanks in advance,
Din
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-22-2009 08:35 AM
тАО07-22-2009 08:35 AM
Re: Help in Grep/ VI
> The command that you gave has really worked out, but it's only 80% accurate. But somehow i managed it by editing manually.
OK.
> Is there any possiblity of getting 100% accurate output that i want...
Yes, but first you need to tell me what "80% accurate" constitutes (by example!). Please provide sample data (as an attachment is best) and the output you want.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-22-2009 08:43 AM
тАО07-22-2009 08:43 AM
Re: Help in Grep/ VI
I need that to be excepted in the output. Anyway this is the sample only, like this lot of dates are coming in the middle of the output.
Thanks for your support...
Regards,
Din
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-22-2009 08:57 AM
тАО07-22-2009 08:57 AM
Re: Help in Grep/ VI
I see your output (with the July 16 dates that you don't want) _BUT_ I need both the output _AND_ the input (attached).
In addition to the actual input (as a file), which of the various scripts of mine did you use?
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-22-2009 09:12 AM
тАО07-22-2009 09:12 AM
Re: Help in Grep/ VI
Thanks and Regards,
Din
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-22-2009 09:39 AM
тАО07-22-2009 09:39 AM
Re: Help in Grep/ VI
OK, you have confused me. Your original question was to find a pattern and list it; the line above it; and three lines below it. I offered a couple of soultions for that.
Then, you changed the question to "I need to take out the content between Jul 17 2009's first appearance and Jul 17 2009's last appearance."
I interpreted that to mean that all you wanted was blocks that looked like:
begin Jul 17
some 17 data
some more 17 data
endof Jul 17
begin Jul 18
some 18 data
some more 18 data
end of Jul 18
...hence I suggested a simple 'sed' where only the blocks beginning and ending with /Jul 17/ were output.
If there is a match on /Jul 17/ but there is never another line with that again, all lines from the first match will be output.
If you want to provide an _actual_ input file (attached) and a second post of the _actual_ output (contrived) from that input, then we can better communicate and find a solution that gives you exactly what you need.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-22-2009 09:57 AM
тАО07-22-2009 09:57 AM
Re: Help in Grep/ VI
My actual requirement is as the last question only...
Sorry for the inconvenience.
Thanks,
Din