- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Using awk and sed
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
06-25-2002 11:15 AM
06-25-2002 11:15 AM
Using either awk and sed I want to acheive this:
When I search for line 2 I want to print line1
line1
line2
How to do it?
Using GNU grep I can acheive it. But I want to use either sed or awk or both.
Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2002 12:30 PM
06-25-2002 12:30 PM
Re: Using awk and sed
I am not sure I understand exact what you want to do but in general awk will be a good tool for such job. Here is a example, perhaps this can give you an idea. The example is from the "real world" but I have simplified it here.
I have a file with a long list of information about backup tapes and want the media id of all tapes which is not protected. The second field of the first line contains the id and the second fild of the second line protected or not.
The file:
Mediumidentifier 0901:3cf68aa3:55a6:0001
Protected Permanent
Mediumidentifier 0901:3cf68aa3:55a6:0002
Protected None
The awk script:
$1 == "Mediumidentifier" { IDE = $2}
$1 == "Protected" { if ($2 == "None") print IDE }
The script reads the file, line for line, if the first field is "Mediumidentifier" it load the variable IDE whith the second field of this line.
When it reads the next line and find the first field is "Protected" it check the second fild for "None". If so it print out the variable IDE.
As "Mediumidentifier" appers in the file before "Protected" the id of the not protected medium will pe printed.
Hope this can help you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2002 01:52 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2002 09:56 PM
06-25-2002 09:56 PM
Re: Using awk and sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2002 06:20 AM
06-26-2002 06:20 AM
Re: Using awk and sed
awk '
BEGIN{last="null"}
/searchstr/{print last}
last=$0
' filename
searchstr = the search string
filename = the file you're working on
Basically it just copies each new line into the variable "last" ... so if you find a match for searchstr, "last" holds the last line that was read.
The BEGIN statement pre-loads the "last" variable in case you get a match for searchstr on the very first line.
Fred