44

I wrote the following awk to print lines from the match line until EOF

awk '/match_line/,/*/' file

How can I do the same in sed?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
lidia
  • 3,043
  • 14
  • 42
  • 47

2 Answers2

69
sed -n '/matched/,$p' file
awk '/matched/,0' file
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
30

This is for a really old version of GNU sed on Windows

GNU sed version 2.05

http://www.gnu.org/software/sed/manual/sed.html

-n only display if Printed
-e expression to evaluate
d stands for Delete
p stands for Print
$ end of file
line1,line2 is the range
! is NOT

haystack.txt

abc
def
ghi
needle
want 1
want 2

Print matching line and following lines to end of file

>sed.exe -n -e "/needle/,$p" haystack.txt
needle
want 1
want 2

Print start of file up to BUT NOT including matching line

>sed.exe -n -e "/needle/,$!p" haystack.txt
abc
def
ghi

Print start of file up to AND including matching line

>sed.exe -n -e "1,/needle/p" haystack.txt
abc
def
ghi
needle

Print everything after matching line

>sed.exe -n -e "1,/needle/!p" haystack.txt
want 1
want 2

Print everything between two matches - inclusive

>sed.exe -n -e "/def/,/want 1/p" haystack.txt
def
ghi
needle
want 1

Delete everything between two matches, no -n with this usage

>sed.exe -e "/ghi/,/want 1/d" haystack.txt
abc
def
want 2
englebart
  • 563
  • 4
  • 9
  • 1
    Thanks: better explanation than the checked one. FWIW, another example (that unfortunately won't format multiline :-( `# show global/file attributes of some compressed netCDF files` `DIR='/asm/ROMO/cdc/2008cdc/smoke_out/2008ab_08c/12US1/cmaq_cb05_soa/'` `FN='emis_mole_all_2008*'` `for GZ in $(find "${DIR}" -maxdepth 1 -type f -name "${FN}" | sort) ; do` `echo -e "Processing ${GZ}, start=$(date)"` `GZ_FN="$(basename ${GZ})"` `NETCDF_FN="${GZ_FN%.gz}"` `cp ${GZ} ./` `gunzip ./${GZ_FN}` `ncdump -h ./${NETCDF_FN} | sed -ne '/global attributes/,$p'` `echo # newline between files` `done` – TomRoche Jun 23 '13 at 19:41
  • What is if you want to print between 'def' and 'want 1'? – ikwyl6 Apr 03 '20 at 22:06
  • @ikwyl6 - I added another sample at the end that answers your question. – englebart Apr 20 '20 at 13:15