sed print all lines after pattern match

HiCan someone show me how to print all lines from a file after a line matching a pattern using sed?Thanks

try

sed '1,/regex/d'

will not include the first pattern line.

example:

$ cat data
123
ABC
DEF
GHI
$ sed '1,/A/d' data
DEF
GHI

Thanks. Doesn't quite work perhaps because I am trying to pipe the lines through sed rather than read a file. What I am trying to do is lose the message of the day on stdout when running a command though ssh by preceding the command with an echo of a string "PARSE_FROM_HERE". The intention is to lose all of the MOTD stuff and just pick up the ls output. I have trouble posting code from here so I hope this is not too garbled, if so I will post it tonight: -

ssh [IP ADDRESS] "echo PARSE_FROM_HERE;ls -lrt" | sed '1,/PARSE_FROM_HERE/d'

What about:

ssh [IP ADDRESS] "ls -lrt"

Try:

sed '0,/PARSE_FROM_HERE/d'

:b:, that's right! I take a mistake.

ssh [IP ADDRESS] "echo PARSE_FROM_HERE;ls -lrt " |sed '0,/PARSE_FROM_HERE/d'

This gives me the MOTD, the PARSE_FROM_HERE line and the ls output. If I substitute a 1 for the 0 I get the MOTD and nothing from the PARSE_FROM_HERE line. Which seems to be the opposite of what is expected from redraiment's results above.

Hi redraiment,

Actually you were right, but if the pattern is on the first line (if there is no MOTD) then it will not work. Besides it is not a general solution as 0,.. only works for GNU sed:

1,/pattern/ can be still be used if we use

printf '\nPARSE_FROM_HERE\n'

-or-

"echo;echo PARSE_FROM_HERE;...

instead.

Not sure if this is what you mean but this gives me MOTD and ls output with the PARSE_FROM_HERE string deleted: -

ssh [IP ADDRESS] "printf \"\nPARSE_FROM_HERE\n\";ls -lrt " |sed '1,/PARSE_FROM_HERE/d'

Thanks! sed range pattern will include more than one line. But, using the line number, can be the same line. like:

sed '1,1d'

:stuck_out_tongue:

Normally the MOTD does not get printed if you are executing a remote command with ssh, unless there is something wrong with your config, or perhaps you mean the banner? What happens if you do:

ssh [IP ADDRESS] "ls -lrt" 2>/dev/null

Yes sorry I guess it is a banner as it never changes. The redirection does seem to work giving me the ls output with no banner, seems I have been over complicating the issue. I wanted to simplify some code at work that uses a C program to intercept the banner by catching the echo of PARSE_FROM_HERE. I thought it could be done with sed instead but it seems that even that was not necessary as your redirection gives me the desired output. Thanks for the help.

---------- Post updated at 02:36 PM ---------- Previous update was at 01:51 PM ----------

This seems to be the answer to what I wanted in that it gives me just the pure ls -lrt output with no banner and no total string. Thanks for all the help folks...

ssh [IP ADDRESS] "echo PARSE_FROM_HERE;ls -lrt" 2>/dev/null |sed '/PARSE_FROM_HERE/,/$/d'

Hi, the total string comes from the ls command, so I would expect this to works too:

ssh [IP ADDRESS] ls -lrt 2>/dev/null | sed 1d

-or-

ssh [IP ADDRESS] "ls -ltr | sed 1d" 2>/dev/null

Yes they both work fine thanks, very compact. Much nicer and more readable than the original code. Having said that I don't understand the sed 1d, can you elaborate? Thanks

---------- Post updated at 03:22 PM ---------- Previous update was at 03:17 PM ----------

Ah got it, just deletes the first line. Thanks :slight_smile: