- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: scripting help needed
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
04-24-2002 02:22 AM
04-24-2002 02:22 AM
I want to search for a string in an ascii file and the output should include the fifth and the eighth line below the match.
tia
Clemens
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2002 02:32 AM
04-24-2002 02:32 AM
Re: scripting help needed
Can you put a detailed example that you need?
Regards,
Justo.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2002 02:40 AM
04-24-2002 02:40 AM
Re: scripting help needed
You can try using awk and its getline function.
Just to give you an idea, lets say you have a file containing:
111
222
333
444
555
666
777
888
999
000
Try this:
cat aaa | awk '/111/ {getline; getline; getline; getline; getline x; getline; getline; getline y; print x; print y}'
The output of this is:
666
999
You may want to build on this to customise it to your specific requirement.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2002 02:50 AM
04-24-2002 02:50 AM
Re: scripting help needed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2002 04:17 AM
04-24-2002 04:17 AM
Re: scripting help needed
thanks for your quick answer. It matches my needs nearly complete.
Now I need to put this awk command in a loop to change the search string several times.
So I have to exchange
cat aaa | awk '/111/ ... against
cat aaa | awk '/$xxx/ ...
But how do I have to quote this variable?
Thanks again
Clemens
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2002 05:33 AM
04-24-2002 05:33 AM
Re: scripting help needed
don't know the syntax for quoting of vars in regular expressions.
But you can try this:
cat aaa | awk '{if ($1 ~ YOUR_VAR) {getline; getline; getline; getline; getline x; getline; getline; getline y; print x; print y }}' YOUR_VAR=$PARAM
or change ($1 ~ YOUR_VAR)
with ($1 == YOUR_VAR)
if $PARAM is exactly the row in your input file.
YOUR_VAR=$PARAM is part of awk command.
Set your search string to PARAM.
Ruediger
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2002 05:49 AM
04-24-2002 05:49 AM
SolutionConsider the following contents of "file":
a
b bravo charlie junk
c charlie delta
d
e
f
g bravo 5th
h charlie 5th
this is it
1 bravo 8th
2 charlie 8th
3
4
5 this one
6
7
8 and this one
9
10
11
Try this script:
#!/usr/bin/sh
for var in "this is it" "bravo" "charlie delta"
do
awk ''/"$var"/' {getline; getline; getline; getline; getline x; getline; getline
; getline y; print x; print y}' file
done
Note the quoting of the arguments in the for loop. That enables the embedding of spaces in the search string.
The quoting of $var in the awk statement will preserve the spaces in the search and recognizes the difference between "this is it" (one space between the words) and "this is it" (two spaces between the words).
Here's another thing I found in testing. It may may not apply to your case but just in case it does...
Note that this awk will find multiple references of the search string in the file as long as they are not in the same 8 line block. For example, search for "charlie" in "file" will return the 5th and 8th lines after the line "b bravo charlie junk", will not return the lines following "c charlie delta", but will return the 5th and 8th following "2 charlie 8th". That's because the awk statement finds a match, checks the next 8 lines, then begins looking for another match.
Darrell
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2002 06:35 AM
04-24-2002 06:35 AM
Re: scripting help needed
awk -v arg=aaa -v pos1=5 -v pos2=9 '{
if (count==pos1) print $0
if (count==pos2) print $0
if (count>=1) count++
for (i=1;i<=NR;i++) {
if ($i==arg) count=1
}
} '
From the inputfile:
abc
aaa
abc1
abc2
abc3
abc4
abc5
abc6
abc7
abc8
abc9
abc10
abc11
It will give you the result:
abc5
abc9
(it starts counting after retrieval of searchstring "aaa".
Regards,
Ceesjan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2002 06:38 AM
04-24-2002 06:38 AM
Re: scripting help needed
your version did what I want it to do.
Thanks Darrell,
you teached me the quoting I couldn't work out by myself!
See you
Clemens