0

I have a file looking like:

---
some metadata
...
---
Some real content.
...
Important content.

Expected output:

Some real content.
...
Important content.

To keep things simple, let's consider that strictly /^---$/ lines are not happening anywhere else than as beginning and end delimiters of the heading metadata block. As shown above.

Note: there could be any amounts of new lines between the end --- delimeter and the first line of content. In that case, I don't care in keeping the new lines or not.

I would like to preferably use awk because I feel this command is versatile and powerful enough to help me do more manipulations on the file if I need later.

Based on this answer to another similar question, I've tried the following unsuccessfully:

awk '/---/ {s=1} /---/ && s==1 {s=0} !s' $file

Might it be because the second rule is actually matching the first line of the file, again?

Is there another way to efficiently do this with awk?

2
  • Have a look at unix.stackexchange.com/q/10374, does it answer your question?
    – Quasímodo
    Feb 26, 2021 at 18:38
  • @Quasímodo some of the answers there are providing actual solutions to the problem, indeed. But things are framed differently, and the accepted answer below is much closer to what I had tried before, as mentioned in the question.
    – lajarre
    Feb 26, 2021 at 18:50

5 Answers 5

3

How about the following:

awk 'BEGIN { metadata = 0 } /---/ { metadata = !metadata; next } !metadata' FILE

With the following input FILE (I added more lines):

---
some metadata
...
---
Some real content.
...
Important content.
---
some metadata
...
---
1
2
3

it prints:

Some real content.
...
Important content.
1
2
3

Your script:

awk '/---/ {s=1} /---/ && s==1 {s=0} !s' $file

doesn't work because you first set s to 1:

/---/ {s=1}

you immediately set it to 0:

/---/ && s==1 {s=0}

and print record if !s, IOW !0 so at the end of the day you print each line.

2
  • 2
    You can completely delete the BEGIN block since ordinary Awk variables are unset by default.
    – Quasímodo
    Feb 26, 2021 at 18:39
  • Yes but I want to avoid awk: ./script.awk:8: (FILENAME=FILE FNR=1) warning: reference to uninitialized variable metadata'` when running with --lint Feb 26, 2021 at 18:40
2

GNU sed employing extended regexes To be read as delete from dash-dash to dash-dash.

sed -Ee '
  /^-+$/,/^-+$/d
' file
1
awk '/^---$/ && s++<2{ next } s>1 && NF' infile
1

It is not an awk solution, however try sed for best result:

sed '/^---$/,/^---$/d' $file
0
sed '/metadata/,+2d' filename| sed '/---/d'

input

---
some metadata
...
---
Some real content.
...
Important content.
---
some metadata
...
---
1
2
3

output

Some real content.
...
Important content.
1
2
3
1
  • 1
    Your answer assumes that the metadata consists of exactly two lines starting with the keyword metadata. It would be worthwile to add this explanation to your post, since the OP will have to modify any answer to adapt it to the actual file format, and also because in case the number of lines is not fixed (which is probable since otherwise no "block-start" and "block-end" markers would be necessary), this answer wouldn't work.
    – AdminBee
    Mar 1, 2021 at 11:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .