0

I organize all my text notes in AsciiDoc files. AsciiDoc is a file format similar to Markdown but instead of # I use = to denote a first level-heading, like so:

first-file.adoc

= How To Install MySQL 8.0 on Fedora 

second-file.adoc

= Summaries and Notes From Books I've Read 

Knowing that each file include one and "only one" first-level heading = This is a First-Level Heading and it is always on line number 1, my goal is to search through all files using grep and only look in those first-level headings.

This grep command matches the second file:

grep -in '^= .*ooks' *.adoc 

It makes sense and it is what I need. However my terminal highlight with red everything up until the word "Books" and leaves the rest "I've Read" with white. Is that normal?

= Summaries and Notes From Books I've Read

3
  • 1
    ... or, to put it another way, spaces are literal in a grep regex. So .* rst would match fi rst but never first Commented Jul 5, 2020 at 18:32
  • @Archemardo you mean something like this: grep '^= ' *.adoc | grep -in 'rst' it seems to work. Commented Jul 5, 2020 at 18:40
  • @archemer and steeldriver make an answer. Commented Jul 5, 2020 at 19:19

3 Answers 3

1

grep solution

search

grep '^=.*rst.*$' *.adoc 

where

  • ^ begin of line
  • = equal
  • .* anything any number of time
  • rst string rst
  • .* anything any number of time
  • $ end of line

this will list files and found line

first.adoc:= The title of the first file 

print result

grep will highlight what part of line triggered the match, so

grep '^=.*rst' *.adoc 

will highlight

first.adoc:= The title of the first file

because search to end-of-line wasn't requested.

grep '^=.*rst.*$' *.adoc 

will highlight

first.adoc:= The title of the first file

because search to end-of-line was requested.

to only files use -l ( or --file-with-matches)

grep -l '^=.*rst' *.adoc 

to only list lines and not the filename use -h (or --no-filename)

see man grep

3
  • Thanks. I need the file name. Your solution works, but the output highlights only the words up to "first", those are shown with red color and the word "file" remains white, even though the output is what I was looking for. There is a space after the equal sign. grep -in '^= .*rst' *.adoc Commented Jul 6, 2020 at 6:15
  • Thank you. Your answer helped me. What would be the correct way to represent a space in regexp? I have a space after the equal sign like so = Title. I've tried something like this: grep -in '^=\s.*rst.*$' *.adoc where \s represents a space. Or should I just add a space with the spacebar ^= .*rst.*$' Commented Jul 6, 2020 at 7:14
  • just use ^= (begining of line, equal, space (not a tab)) or else ^=[:blank:] Commented Jul 6, 2020 at 7:22
1

If you can use awk, GNU version that supports nextfile, all you need to do is

awk '{ if ( $0 ~ /fifth/ ) print FILENAME ; nextfile }' *.adoc 
1

If I understand your question correctly, you would like the whole title to be highlighted.

If that's the case, you just need to add .* to the end of your expresion. grep shows you in red the past of the line that matches your matching expression.

1
  • 2
    They don't say whether they want the output highlighted or not. They only wonder if it's normal to have the output partially highlighted. As far as I know, it's only GNU grep that does this sort of highligting. Commented Jul 6, 2020 at 7:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.