Operating System - HP-UX
1831284 Members
2988 Online
110022 Solutions
New Discussion

How to search a few lines from a file?

 
SOLVED
Go to solution
zhaogui
Super Advisor

How to search a few lines from a file?

Hi,
can anybody tell me how to print out from a text file 5 lines starting from the first occurence of one particular 'mystring'?
It seems sed -n '/mystring/,+5p' myfile can't work.
Thanks,
10 REPLIES 10
Steven Sim Kok Leong
Honored Contributor

Re: How to search a few lines from a file?

Hi,

>> can anybody tell me how to print out from a text file 5 lines starting from the first occurence of one particular 'mystring'?

# tail +`cat -n file | grep mystring | awk '{print $1}'` | head -5

Hope this helps. Regards.

Steven Sim Kok Leong
zhaogui
Super Advisor

Re: How to search a few lines from a file?

I think it should be
"tail +$(grep -n "^DMS" listener.ora|awk -F: '{print $1}'|head -1) listener.ora|head -5"

Actually I am searching for host name/IP address in Oracle listener.ora file given Database name "ORACLE_SID". Is there better way to do that? Can I use sed ?

Thanks,
Steven Sim Kok Leong
Honored Contributor

Re: How to search a few lines from a file?

Hi,

I have now included the head -1 to take core of multiple occurrences of mystring.

# cat -n file | grep mystring | awk '{print $1}' | head -1

This greps for the line no. of the first line containing the string.

# tail +`cat -n file | grep mystring | awk '{print $1}' | head -1`

This tails from the line containing the string until the end of the file.

# tail +`cat -n file | grep mystring | awk '{print $1}' | head -1` | head -5

This extracts the 1st 5 lines from the line containing the string until the end of the file.

Hope this helps. Regards.

Steven Sim Kok Leong
Robin Wakefield
Honored Contributor
Solution

Re: How to search a few lines from a file?

Hi,

You could use awk:

awk '/mystring/{n=1}n>0 && n<6{print;n++}' myfile

Rgds, Robin.
Robin Wakefield
Honored Contributor

Re: How to search a few lines from a file?

Hi,

Here's one way with sed:

sed -n '/string/{
N
N
N
N
p
}' filename

Rgds, Robin.
zhaogui
Super Advisor

Re: How to search a few lines from a file?

Thanks a lot.
zhaogui
Super Advisor

Re: How to search a few lines from a file?

Sorry, I should give points to the answer with " awk '/mystring/{n=1}n>0 && n<6{print;n++}' myfile ". Because the last one still got problem, it won't show only the 5 lines from the first occurence of the string. Actually it shows all 5 lines from all the occurence of string. How to do what I want using sed?

Thanks,

zhaogui
Super Advisor

Re: How to search a few lines from a file?

some more scripts
zhaogui
Super Advisor

Re: How to search a few lines from a file?

scripts in zip format
Rodney Hills
Honored Contributor

Re: How to search a few lines from a file?

You can modify Robins script with "q"

sed -n '/string/{
N
N
N
N
p
q
}' filename

This will then quit after the first group is printed.

HTH

-- Rod Hills
There be dragons...