Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i want to grab the the text between tok98 and 5678 and dump that out to a file.

if the tok98 and the 5678 was on the same line i can use this

sed -n 's/.*\(tok98\)\(.*\)\(5678\).*/\2/p' testfile.txt > text.dmp

but when the tokens are vertically spaced its not working, how can i rewrite this sed to work here

$ cat testfile.txt
do with > 9
  fdf  df s df sd f sd v
  dvsdv  dvf sd vs vs dv  sdfsd

    2323 2323  232 {}   
   sfdf sd  f s df s df  sf 
no way = true + 50

share|improve this question
up vote 6 down vote accepted

You can use /START/,/STOP/ pattern ranges in sed

sed -n '/tok98/,/5678/p' file

/START/ and /STOP/ can be REs too

sed -n '/^tok98/,/^5678/p' file

would print between the lines starting tok98 and 5678 and would not start or stop unless the lines begin with those strings.

If you want to exclude the /START/ and /STOP/ then this should work

sed -n '/^tok98/,/^5678/ {/^tok98/b;/^5678/b;p}' file
share|improve this answer

sed by default does not process multiple lines, but this can be solved using a trick like:

$ cat testfile.txt | tr '\n' ',' | sed -n 's/.*\(tok98\)\(.*\)\(5678\).*/\2/p' | tr ',' '\n' > text.dmp

You just need to convert the newlines \n to some other character like , and then convert them back to \n when done with sed. You need to make sure that you input file does not have any comma.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.