0

I have a file example.txt which contains following text:

[one]: bla bla bla onebla twobla
[two]: hey heya noheya
[onemore]: i got mad and etc

I need to grep and show only text that after [myword]: Tried to test it this way:

grep [myword] /tmp/example.txt | cut -d ':' -f 2

On each [myword] it prints all after brackets, but how can I get only the one I need only and not all?

I need to print only the test that after certain brackets. For example I want to print only text that in line [one]: but not after [two]: or [onemore]:lines, so that the output will be bla bla bla onebla twobla. If I want to print all after [onemore]: - the correct output should be i got mad and etc

0

If you wish the solution which is independent on a delimiter, the use awk

$ cat 304162 
[one]: bla bla bla onebla twobla
[two]: hey heya noheya
[onemore]: i got mad and etc
[fourth:one]: some more bla, well ! worst case
$ awk '/\[onemore\]/{sub(/^[^]]*\]:[[:blank:]]*/,"");print}' 304162
i got mad and etc
$ awk '/\[fourth:one\]/{sub(/^[^]]*\]:[[:blank:]]*/,"");print}' 304162
some more bla, well ! worst case
3

Better with sed:

sed -n 's/^\[one]: //p' < example.txt

With GNU grep with support for recent PCRE, you can also do:

grep -Po '^\[one]: \K.*' < example.txt

Or

grep -xPo '\[one]: \K.*' < example.txt

In any case, note that in most shells, [...] are glob operators. In grep [myword], [myword] is expanded to the list of files that match that, that is any file in the current directory whose name is m, y, w, o, r or d (and if there's none, depending on the shell, the pattern is passed as-is to grep, or you get an error). So they must be quoted for the shell (with single quotes for instance as in the solutions here). For instance, if there's a file called r in the current directory, and one called d, grep [myword] would become grep d r in all shells but fish.

[...] is also a special operator in regular expressions (very similar to the [...] glob operator), grep '[myword]' would match on lines that contain m, y, w, o, r or d. So you need to escape the opening [ for grep (for regular expressions) as well. That can be done with grep '\[myword]' or grep '[[]myword]'.

^ is another regular expression operator that means: match at the beginning of the line only. So grep '^\[myword]: ' matches on lines that start with [myword]:.

While grep is just meant to print matching lines (is not otherwise a stream editor like sed is), GNU grep added the non-standard -o option for it to print the matching portion(s) of the line (if non-empty). It also added the -P options to use perl compatible regular expressions (in PCRE) instead of basic ones without -P.

In recent PCREs, \K is an operator that resets the start of the matching portion. So in grep -Po '^\[one]: \K.*', we do print the matching portion because of -o, but because of \K, that matching portion becomes the sequence of characters (.*) that is found after [one]:.

  • Thanks, but it doesnt give me any output (also where I should point to files to search in?) – alya Aug 18 '16 at 7:58
  • @alya, see edit. It works for me on your sample. As stated, the grep one will not work on all systems, as it uses GNU extensions (so will only work on systems where grep is the GNU one (see grep --version) or have copied those extensions from GNU grep), and also the PCREs are relatively recent (\K was added only a few years ago). – Stéphane Chazelas Aug 18 '16 at 8:20
0

[one] regular expression means "a character matching o or n or e.
grep [one] /tmp/example.txt will match all the lines of your example

To match the [] as characters you need to escape them :

grep \\[one\\] /tmp/example.txt   | cut -d ':' -f 2

Or you can use grep -F which ignore the regular expression syntax :

grep -F [one] /tmp/example.txt | cut -d ':' -f 2

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.