Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 have a pandoc markdown file which has a lot of code. I want to extract all the code bits from file preferably using sed. The syntax of code in pandoc is three or more ` signs to start and finish the code segment.


Code is here


I can extract one line but I have no way of extracting between markers.

share|improve this question
Keep your sanity and use a parser ;-) – Ciro Santilli 巴拿馬文件 六四事件 法轮功 Apr 28 '15 at 13:49
sed -n '/^```/,/^```/ p' < input.file

Will print all lines between ``` but including the ``` lines too. To get rid of that:

sed -n '/^```/,/^```/ p' < input.file | sed '/^```/ d'

You can find an explanation of how to use line ranges by pattern, and the 'p' and 'd' commands starting here:


share|improve this answer

You can also use perl:

perl -0777e '$_=<>;print $1 while /^`{3,}\s*\n(.*?)^`{3,}\s*\n/msg' your_file


  • The 0777 part sets the input record separator ($/) to slurp the whole file
  • The regex looks for a line starting with 3 or more backticks then matches and captures as few lines as possible (guaranteed by the lazy quantifier *?) followed by a line starting with 3 or more ticks.
  • The regex modifier m ensures that ^ would match beginning of lines not just the start of the file. The modifier s makes sure that . will match a newline character so that .*? can match several lines. Finally g makes sure that the regex will be applied iteratively to the file contents so that all matches are found.
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.