Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi

I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match.

Which option is to be used to exclude the line containing the pattern?

sed -n  '/Conn.*User/,$p' > consumers.txt

Try:

awk '/Conn.*User/{p=1;next}p' consumers.txt

To print line after pattern:

 sed -n '/pattern/{n;p;}' infile

This shows the immediate next line to the pattern. Not showing ALL the lines.

Try:

sed -n '/pattern/,${/pattern/!p;}' file

(but this will delete all lines with /pattern/ from the output)

sed '1,/pattern/d' file

But this will give the wrong result if the pattern is on line 1

GNU sed:

sed '0,/pattern/d' file

--
Using awk:

awk 'p; /pattern/{p=1}' file

---
On Solaris use /usr/xpg4/bin/awk rather than awk

Does your server support the -A flag on grep? That might be a neater solution:-

grep -A10 pattern  file

Maybe I've missed the point. If you want all lines after a certain point, perhaps you could use grep to get the record number and then tail to get you the output you need:-

#!/bin/ksh

grep -n pattern  file|tr ":" " "|read line xxx       # Get the interesting line number
wc -l file | read total xxx                          # Get total lines
((readrecs=$total-$line))                            # Work out how many lines from end to read
tail -$readrecs   file

Do either of these help?

Robin

In sed you need a loop:

sed -n '/pattern/{
:loop
n
p
b loop
}' file

---------- Post updated at 12:33 PM ---------- Previous update was at 12:22 PM ----------

BTW in sed you don't need to repeat a pattern on the same line:

sed -n '/pattern/,${//!p;}' file
sed -n '/pattern/ s//replacement/p' file

All you need is egrep -A piped into grep -v. Set the -A for the number of rows that you want to see after the row you are looking for and use grep -v to filter the row that you were looking for. you don't need sed or a for loop.

$ egrep -A1 "alias top" .bashrc
alias top='xtitle Processes on $HOST && top'
alias make='xtitle Making $(basename $PWD) ; make'

$ egrep -A1 "alias top" .bashrc | grep -v "alias top"
alias make='xtitle Making $(basename $PWD) ; make'

My server doesn't support -A on grep. Besides, the statement that I've written is already a complex one and I want to use sed as part of multiple piped commands. Using 3-4 lines of code there would not be feasible.

---------- Post updated at 02:04 AM ---------- Previous update was at 01:51 AM ----------

Made it work somehow.. although still looking for a better solution. Here's what I am using for the time being:

sed -n '/Conn.*User/,${//!p;}'|sed '1d'

So have the requirements changed? This will remove the line that contains the pattern plus the line after that line, so effectively the first two lines.....

The first sed command searches for the pattern and prints the lines after the pattern match (including the line that contains the pattern).

The second sed command (after pipe) deletes the first line from the output of the first sed command.

The requirement is still the same. This is just a workaround until I find a real fix.

No, the first sed command ( sed -n '/Conn.*User/,${//!p;}' ) prints the lines after the pattern match excluding the line(s) that contain the pattern.

What is your OS and version?