- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- scripting help
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
тАО07-23-2009 06:03 AM
тАО07-23-2009 06:03 AM
Been banging my head on this one and I can't find the right touch.
I have output from a command that produces the following format;
Handle
another line
another line
another line
Handle
System Information
line 1
line 2
line 3
line 4
Handle
another line
another line
another line
another line
Handle
etc...
What I am wanting to do is print everything between the lines of Handle but only if the pattern of System Information is matched. The rest of the data I do not want.
I did come up with this and it works OK, but I know there is a better solution.
awk 'BEGIN { RS = "System Information" } END { RS = "" } {print $1, $2, $3, "\n" $4, $5, $6}'
Like I said, this works OK but if the number of variables/lines changes then the print changes. What I really want is to print all lines (line 1, line 2, line 3 line n, ...) regardless of the number of lines.
The shell can be either ksh or bash.
Many thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-23-2009 06:07 AM
тАО07-23-2009 06:07 AM
Re: scripting help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-23-2009 06:59 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-23-2009 07:48 AM
тАО07-23-2009 07:48 AM
Re: scripting help
Any thing for shell (using awk, sed, or something)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-23-2009 07:52 AM
тАО07-23-2009 07:52 AM
Re: scripting help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-23-2009 08:25 AM
тАО07-23-2009 08:25 AM
Re: scripting help
awk '/System Information/,/Handle/' file
This will print the line 1 line 2 line 3 line 4 line n...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-23-2009 08:41 AM
тАО07-23-2009 08:41 AM
Re: scripting help
OK, you can do the same in 'awk' with:
# awk 'BEGIN{RS=""};{if (/Handle/ && /System/) {print}}' file
> I only want the line 1, line 2, line 3 - the rest of the output (another line) I do not want.
That changes things a bit since in either the Perl or 'awk' we have used the blank line to signal a record's endpoint.
One kludge way would be to pipe the output of the 'awk' or 'perl' script to 'head -5'.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-23-2009 08:56 AM
тАО07-23-2009 08:56 AM
Re: scripting help
#!/usr/bin/sh
while read LINE
do
if [[ "${LINE}" = "Handle" ]] ; then
HANDLE=1
elif [[ "${LINE}" = "System Information" ]] ; then
echo "Handle"
echo ${LINE}
SYSINF=0
HANDLE=0
elif [[ "${SYSINF}" = "0" && "${HANDLE}" = "0" && "${LINE}" != "" ]] ; then
echo ${LINE}
fi
done < testfile
Running this, with your example data yields:
$ sh ./test.sh
Handle
System Information
line 1
line 2
line 3
line 4
$
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-23-2009 09:00 AM
тАО07-23-2009 09:00 AM
Re: scripting help
Amy thanks James and Patrick
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-23-2009 09:11 AM
тАО07-23-2009 09:11 AM
Re: scripting help
A bit better than the pipe to 'head' to snip any additional lines you don's want is this variation. It also handles repetitive blocks that match:
# perl -nle '$/="";if (m/Handle/ && m/System/) {@a=m/(.+?$)(.+?$)(.+?$)(.+?$)(.+?$)/sm and print @a}' file
Regards!
...JRF...