3

I normally use grep to search for a pattern in a string. However in this particular instance I have to identify a YAML header, also, it ends with a triple dash.

My test.info file has the following content

---
title: dont't know
draft: true
---
this is a test to add some extra content

I want the following output, i.e. everything from after the last YAML delimiter until the end of the file:

this is a test to add some extra content

When I enter the dashes, bash return the following error:

$ cat test.info | grep '---' -A1
grep: unrecognized option `---'

I tried to “escape” the dashes unsuccessfully. Any idea? This is for BSD grep. The things that get me confused is that I can get what I want if I do something like the following.

$ cat test.info | grep 'this' -A1

Problem is that I don't know what's the first word.

I can grep the file as recommended, but the tool returns the pattern and not everything immediately after:

$ grep -m 1 -e '---' test.info 
---
$ grep -- --- test.info | tail -1
---
2

How about this command?

tac file| awk '/---/ {exit} {print}'|tac

On OSX just replace both tac commands with tail -r

From man tac:

 tac - concatenate and print files in reverse - reverse of cat command ;)

Output of tac file:

next line
this is a test to add some extra content
---
draft: true
title: dont't know
---

The awk command awk '/---/ {exit} {print}' prints all lines until first matched pattern found.

Output:

next line
this is a test to add some extra content

then run tac command again to reverse to default.

Output:

this is a test to add some extra content
next line
| improve this answer | |
1
$ line=$(grep -n -- --- test.info | tail -n 1 | cut -d: -f1);tail -n +$(( $line + 1 )) test.info 
this is a test to add some extra content

Appropriate error checking needs to be added, as in if $line 'not numeric' ...

The original problem comes from the fact that you need to escape - or tell the program that it is not an option:

$ grep -n -- --- test.info
1:---
4:---

Most(?) gnu software has "--" as an option; telling to stop parsing for more options after that point.

Note:
$ grep --version
should tell if it is a GNU grep utility or not.

$ grep -h
or
$ grep --help
usually tells the options it understands.

| improve this answer | |
  • This works, upvoted. – slhck Nov 22 '14 at 12:21
  • That is team work ;-) – Hannu Nov 22 '14 at 12:47
  • just in case he has an issue with the dash (he's using bsd grep so who knows) then for escaping dash it seems like using hex for dash worked for him, this worked of gnu grep and from what he said seems to have worked in his bsd grep too $ echo - | grep -P '\x2d' matches his dash. – barlop Nov 23 '14 at 14:24
1

Using awk:

awk 'END{print}' RS='---' file

RS defines --- as record separator and with END{print} we only prints the last record.

Using sed:

sed -r ':a;$!{N;ba};s:^(.*\n?)---::' file
| improve this answer | |
0

I see at least two different ways to do it. In fact you need to escape the first "-"

Using option -e:

grep -e "---" test.info

Escaping the first "-" with backslash:

grep "\---" test.info
| improve this answer | |
  • None of the works. – Andrea Moro Nov 19 '14 at 20:43
  • I've added more details so it is easier to reproduce. – Andrea Moro Nov 19 '14 at 20:46
  • ??? Sorry, then is a mystery for me. I've tested before posting: it works here (GNU grep 2.12) – Zimmi Nov 19 '14 at 20:51
  • I've added few details above in my answer with the file content, just in case. – Andrea Moro Nov 19 '14 at 20:57
0

So, without taking any credit as I don't want, I just post the solution that worked on my mac thanks to KasiyA

tail -r file| awk '/---/ {exit} {print}'| tail -r
| improve this answer | |
-1

Addressing one aspect of your (now original) question.

(And your new questions stills talks about grep and -A1 and pretends that you can't find a way to specify 3 hiphens and that's just wrong because in your comments you've shown that you can).

[your original question asked how to show results FROM a point. when actually you want FROM AFTER. Your new question still stated 'from' I updated it to 'from after' ]

If does look like grep and -A1 aren't what you want though it's still mentioned even in your updated question

I don't see any funny results. Perhaps you can paste the results you think are funny. [you now have]

If you do grep pattern -A1 then it dumps the pattern followed by the next line. And it dumps -- after each match.

e.g.

$ cat t3.info
,,,
qwerty
uiop
,,,




werwer
werwer
,,,,,
werwerwer
werwerwer
werwerwer
,,,,



$ cat t3.info | grep -P ',,,' -A1
,,,
qwerty
--
,,,

--
,,,,,
werwerwer
--
,,,,



$

You could do grep -P '(?=---)...' -A1 Another thing you can try is this grep -P '\x2d{3}' -A1

And if your file has ---

$ cat t3.info
---
qwerty
uiop
---




werwer
werwer
---
werwerwer
werwerwer
werwerwer
---

Sure this doesn't work

$ cat t3.info | grep -P '---' -A1 grep: unrecognized option '---' Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information.

But this works

$ cat t3.info | grep -P '\---' -A1
---
qwerty
--
---

--
---
werwerwer
--
---

and this works

$ cat t3.info | grep -P '(?=\x2d{3})...' -A1
---
qwerty
--
---

--
---
werwerwer
--
---

grep version 2.16

$ grep --version
grep (GNU grep) 2.16

If you don't want to be including your pattern then whatever option you use it wouldn't be -A1 and I don't see why you are having trouble with matching ---

It may be that to do what you want with grep or something grep like, you need to match new line e.g. e.g. regex of positive lookbehind.. for ---\n but apparently grep can't match new lines, in which case you might have more luck with pcregrep https://stackoverflow.com/questions/2686147/how-to-find-patterns-across-multiple-lines-using-grep or some other way. S oyour question is more, how can you match what follows on the line after a pattern.

| improve this answer | |
  • My bad. I didn't mention I'm on OS X and the grep version I have is grep (BSD grep) 2.5.1-FreeBSD – Andrea Moro Nov 19 '14 at 23:04
  • @AndreaMoro can you paste the command and results you are getting? and try the alternative grep lines I suggeested eg cat t3.info | grep -P '(?=\x2d{3})...' -A1 or cat t3.info | grep -P '\x2d\x2d\x2d' -A1 – barlop Nov 19 '14 at 23:25
  • the -P parameter corresponds to -e in the BSD version. As for the output the one with regular expression doesn't produce anything. the second simply don't remove everything before the last --- as expected. That's the output --- title: dont't know -- --- this is a test to add some extra content – Andrea Moro Nov 19 '14 at 23:30
  • @AndreaMoro it's not so clear in comment as new lines aren't visible and one cannot really format much in a comment. Can you paste the examples (converted to bsd as you have) into your question, with the results. – barlop Nov 19 '14 at 23:40
  • 1
    @AndreaMoro That is what you intended your question to be but that's not what your question says. Your question says grep and your question says a specific delimiter/pattern being a problem, and your question says -A1(which would actually include the pattern), and your question says it's not working,and you can't escape the dash/hiphen.I suggest u post another question,this time without saying anything of grep and nothing about --- being a special problem.And ur question may be more about either a)matching text outside a pair of tags.or b)matching text after a second occurrence of a pattern. – barlop Nov 20 '14 at 10:53

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.