I want to delete some specific lines in the YAML front matter:

Author: aaaa
Title: bbbb
Time: cccc
Place: dddd

Lets say I wanted to delete only the lines in the front matter that contained "title:" and "place:"

To make sure I am not deleting any other lines that contain these words, it should only search the front matter. Also any solution would preferably not use the line number but rather the actual word as its criteria for selection.

Any help would be much appreciated thanks.

My own research consists of:

This - which deletes multiple words in one go

and This - which selects a specific area of the file

  • 1
    What delimits the "front matter"? can you show more of your YAML file to see what should be kept? – glenn jackman Mar 20 '15 at 21:30
  • 4
    sed '/^Title: \|^Place: /d' file.txt ? – heemayl Mar 20 '15 at 21:51
  • HI I have edited the Front matter in my question to show what delimits it. How do i use this command to only apply within the front matter? – st john smith Mar 25 '15 at 15:57

Not familiar with the YFM (YAML front matter) but as per this page it must be the first thing in the file and must take the form of valid YAML set between triple-dashed lines. That means your file will always start with --- if YFM is present, so you could run:

sed -n '1{/^---$/!q};1,/^---$/{/^Title: \|^Place: /d};p' infile

i.e. if the first line doesn't match --- quit, otherwise delete all lines matching those patterns if they occur between the 1st line and the next line matching ---
If the second marker is ... just alter the second sed expression but note that to match a literal dot you'll have to escape it e.g. to match ... you would use \.\.\. or \.\{3\}):

sed -n '1{/^---$/!q};1,/^\.\.\.$/{/^Title: \|^Place: /d};p' infile
  • What if the front matter ends in ..., would the command be sed -n '1{/^---$/!q};1,/^...$/{/^Title: \|^Place: /d};p' infile ?? – st john smith Mar 30 '15 at 19:23
  • @stjohnsmith - almost there, you just have to escape the dots if they are to be matched literally. Answer updated. – don_crissti Mar 30 '15 at 20:40

In addition to heemayl's sed solution, also a grep option:

grep -Ev '^Title:|^Place:' file.txt


  • grep -v to ignore lines that include Title: or Place:
  • grep -E to stack the two terms
  • ^ indicates the patterns should be at the beginning of the line
  • How do you apply this only to the front matter?? Please see my edition of the question which shows what delimits the front matter. – st john smith Mar 25 '15 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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