- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: grep using awk then head and tail
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
тАО08-22-2002 11:58 AM
тАО08-22-2002 11:58 AM
My feeble attempts need help!
I'mwas attempting to use;
cat -n $file | grep PARM | awk '{print $1}' | head -n ${1} $file .
the cat -n $file | grep PARM | awk '{print $1}' works fine, but how do I pass that to head -n ?
Thank you ~jdk
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-22-2002 12:01 PM
тАО08-22-2002 12:01 PM
Solutionexample-
head -n $(echo 4) filename
In your example, replace all those commands for the "echo 4".
Their might be an easier way to do what you are trying to do. Can you rephrase your question in terms of what results you are looking for.
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-22-2002 12:01 PM
тАО08-22-2002 12:01 PM
Re: grep using awk then head and tail
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-22-2002 12:04 PM
тАО08-22-2002 12:04 PM
Re: grep using awk then head and tail
Use
cat $file | grep PARM | awk '{print $1}' | head -10
HTH
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-22-2002 12:07 PM
тАО08-22-2002 12:07 PM
Re: grep using awk then head and tail
Sample using an IOSCAN I did to a file:
cat -n $file | grep C895 | awk '{print $1}' | xargs -i head -n {} $file
Class I H/W Path Driver S/W State H/W Type Description
============================================================================
root 0 root CLAIMED BUS_NEXUS
ioa 0 0 sba CLAIMED BUS_NEXUS System Bus Adapter (803)
ba 0 0/0 lba CLAIMED BUS_NEXUS Local PCI Bus Adapter (782)
lan 0 0/0/0/0 btlan CLAIMED INTERFACE HP PCI 10/100Base-TX Core
/dev/diag/lan0 /dev/ether0 /dev/lan0
ext_bus 0 0/0/1/0 c720 CLAIMED INTERFACE SCSI C895 Fast Wide Single-Ended
#
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-22-2002 12:33 PM
тАО08-22-2002 12:33 PM
Re: grep using awk then head and tail
I need to go through a file, find all occurences of $name1, then echo out that line and the line prior to it.
I also need to find $name2 and echo out that line and several after it.
ie If I already know the line #, I can use "head -n line# | tail -1 or head +4.
Does that make more sense?
Thanks again, ~jdk
ps ALL of you get and deserve 10 points eitherway
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-22-2002 12:34 PM
тАО08-22-2002 12:34 PM
Re: grep using awk then head and tail
cat -n $file | grep C895 | awk '{printf "head -n %d %s \| tail -n %s\n",$1+2,"XZ
X",$1-1;}' | sed "s,XZX,${file},g" | xargs -i sh {}
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-22-2002 12:41 PM
тАО08-22-2002 12:41 PM
Re: grep using awk then head and tail
perl -n -e '$s2=$s1;$s1=$_;/mno/ && do {print "$s2$s1"}' $file
Hope this helps...
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-22-2002 12:47 PM
тАО08-22-2002 12:47 PM
Re: grep using awk then head and tail
cat -n cmaudit | grep BEGIN | awk '{print $1}' | head -n '{print $1}' cmaudit | tail -5 . Can you point out the error of my ways?
Harry, I'm using a test machine so I tried your mind blower, modified of course....it returned empty. I'm not quite sure I understand all it does, or if I could understand it anyway!
I will give the perl scripting a try, and get back to you Rod.
Thanks again! ~jdk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-22-2002 12:56 PM
тАО08-22-2002 12:56 PM
Re: grep using awk then head and tail
sed -n -e `grep -n $name1 $file | awk -F: '{print $1 - 1 "," $1 "p"}'` $file
sed -n -e `grep -n $name2 $file | awk -F: '{print $1 "," $1 + 7 "p"}'` $file
The first finds $name1 and prints line prior and current line.
The second finds $names and prints line plus 7 additional lines.
Hope this helps.
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-22-2002 01:08 PM
тАО08-22-2002 01:08 PM
Re: grep using awk then head and tail
sed -n -e "`grep -n \"mno\" infile | awk -F: '{print $1 - 1 \",\" $1 \"p\"}'`" infile
I needed to put the result of the grep in double quotes following the -e option of sed.
The \" are there so the shell will not treat them as matching the first ".
This will now work for multiple finds of $name1.
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-23-2002 09:10 AM
тАО08-23-2002 09:10 AM
Re: grep using awk then head and tail
#!/usr/perl5/bin/perl5.00502
$srcfil = "cmaudit" ;
$srcdir = "/tmp/autosys/" ;
$outdir = "/tmp/autosys/" ;
$outfil = "sstimes.log" ;
##################################
# Open files
#
open ( INPFL, "${outdir}${srcfil}" ) ||
die "Couldn't open ${outdir}${srcfil}" ;
open ( OUTFL, ">${outdir}${outfil}" ) ||
die "Couldn't open ${outdir}${outfil}" ;
while (
{
# chop ;
$line = "$_" ;
$begin = index($line, "BEGIN" ) ;
if ( $begin != -1 ) {
printf (OUTFL "%s", $line) ;
$nline =
printf (OUTFL "%s", $nline) ;
} else {
$finish = index($line, "FINISH" ) ;
if ( $finish != -1 ) {
printf (OUTFL "%s", $oline) ;
printf (OUTFL "%s", $line) ;
}
}
$oline = "$_" ;
} #End of while input
close (INPFL);
close (OUTFL);
exit;
Thanks for all the GREAT ides though!
Some days you get the bear, blah, blah
~jdk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-23-2002 09:45 AM
тАО08-23-2002 09:45 AM
Re: grep using awk then head and tail
X",$1-1;}' | sed "s,XZX,${file},g" | xargs -i sh {}
live free or die
harry
cat -n cmaudit | grep BEGIN | awk '{print $1}' | head -n '{print $1}' cmaudit | tail -5 . Can you point out the error of my ways?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-23-2002 09:47 AM
тАО08-23-2002 09:47 AM
Re: grep using awk then head and tail
HOW ABOUT THIS:
cat -n cmaudit |
grep -e BEGIN -e FINISH |
awk 'BEGIN {pos_beg=0;pos_end=0;}
{if (pos_beg == 0) {pos_beg=$1;next;}
if (pos_end == 0) {pos_end=$1;next;}
} END {printf "head -n %d %s \| tail -n %s\n",pos_end,"cmaudit",pos_end-pos_beg+
1;}' |
xargs -i sh {}
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-23-2002 10:23 AM
тАО08-23-2002 10:23 AM
Re: grep using awk then head and tail
~jdk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-23-2002 11:55 AM
тАО08-23-2002 11:55 AM
Re: grep using awk then head and tail
cat cmaudit |
awk 'BEGIN {startdisp=0;}
{if (match($0,/.*BEGIN.*/) > 0) {startdisp=1; }
if (startdisp == 1) {print $0;}
if (match($0,/.*FINISH.*/) > 0) {startdisp=0;}
}'
Will print every BEGIN-to-FINISH section.
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-23-2002 12:09 PM
тАО08-23-2002 12:09 PM
Re: grep using awk then head and tail
That also puts out too much.
Only need;
The BEGIN line and the line after it.
The FINISH line and the line before it.
~jdk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-23-2002 12:24 PM
тАО08-23-2002 12:24 PM
Re: grep using awk then head and tail
cat cmaudit |
awk 'BEGIN {startdisp=0;}
{
if (match($0,/.*BEGIN.*/) > 0) {print $0; startdisp=1; next;}
if (startdisp == 1) {print $0;startdisp=0;next;}
if (match($0,/.*FINISH.*/) > 0) {print prevline; print $0;startdisp=0;}
prevline=$0;
}'
******BEGIN CIMCCV001******
Thu May 30 18:02:17 EDT 2002
Thu May 30 18:02:19 EDT 2002
******FINISH CIMCCV001******
******BEGIN CIMCCV002******
Thu May 30 23:52:31 EDT 2002
Thu May 30 23:52:31 EDT 2002
******FINISH CIMCCV002******
******BEGIN CSICSY001******
Thu May 30 18:56:16 EDT 2002
Thu May 30 18:56:54 EDT 2002
******FINISH CSICSY001******
******BEGIN CSICSY004******
Thu May 30 18:56:44 EDT 2002
******FINISH CSICSY004******
though the last finish didn't have a time, except after it!
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-23-2002 12:42 PM
тАО08-23-2002 12:42 PM
Re: grep using awk then head and tail
This runs as fast as the perl.
Unfortunatly, I'm not gifted enough to have figured out this on my own :( .
Well, thank you, I am going to keep this one for reference for a long time I hope!
Have a GREAT weekend Harry,
~jdk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-23-2002 01:24 PM
тАО08-23-2002 01:24 PM
Re: grep using awk then head and tail
sed -n -e '/BEGIN/,/FINISH/p' $file
Is all you need...
-- Rod Hills