How to Grep For Contents After Pattern

How to grep for contents after pattern?

grep 'potato:' file.txt | sed 's/^.*: //'

grep looks for any line that contains the string potato:, then, for each of these lines, sed replaces (s/// - substitute) any character (.*) from the beginning of the line (^) until the last occurrence of the sequence : (colon followed by space) with the empty string (s/...// - substitute the first part with the second part, which is empty).

or

grep 'potato:' file.txt | cut -d\   -f2

For each line that contains potato:, cut will split the line into multiple fields delimited by space (-d\ - d = delimiter, \ = escaped space character, something like -d" " would have also worked) and print the second field of each such line (-f2).

or

grep 'potato:' file.txt | awk '{print $2}'

For each line that contains potato:, awk will print the second field (print $2) which is delimited by default by spaces.

or

grep 'potato:' file.txt | perl -e 'for(<>){s/^.*: //;print}'

All lines that contain potato: are sent to an inline (-e) Perl script that takes all lines from stdin, then, for each of these lines, does the same substitution as in the first example above, then prints it.

or

awk '{if(/potato:/) print $2}' < file.txt

The file is sent via stdin (< file.txt sends the contents of the file via stdin to the command on the left) to an awk script that, for each line that contains potato: (if(/potato:/) returns true if the regular expression /potato:/ matches the current line), prints the second field, as described above.

or

perl -e 'for(<>){/potato:/ && s/^.*: // && print}' < file.txt

The file is sent via stdin (< file.txt, see above) to a Perl script that works similarly to the one above, but this time it also makes sure each line contains the string potato: (/potato:/ is a regular expression that matches if the current line contains potato:, and, if it does (&&), then proceeds to apply the regular expression described above and prints the result).

How to grep one string after pattern?

With your shown samples, please try following grep command. Written and tested in GNU grep.

grep -oP '^yellow_y\s+\K\S+'  Input_file

Online demo for above regex

Explanation: Simple explanation would be, using -oP options of GNU grep which is for printing matched words and enabling PCRE regex respectively. In main program using regex to match condition. Checking if line starts from yellow_y followed by 1 or more spaces then using \K capability of GNU grep to forget this match and matching 1 or more non-spaces characters then which will provide required values in output.

grep for contents after pattern for word character and comma

You can use a single awk for this:

echo "this is a test:foo,bar,baz']" | awk -F 'test:' '{sub(/[^,[:alnum:]].*/, "", $2); print $2}'

foo,bar,baz

Or, you can use a single sed:

echo  "this is a test:foo,bar,baz']" | sed 's/.*test://; s/[^,[:alnum:]].*//'

foo,bar,baz

How to grep line after pattern match only for one pattern out of many

You can do as you ask with:

awk '/PANIC:/{print; getline; print} /Node [0-7]/ || /^ *$/' input

Although you don't mention it in the question, your sample output demonstrates that you also want to combine multiple blank lines in a single blank line. That can be done with:

awk '/./{a=0} /PANIC:/{print; getline; print} /^ *$/ && !a++{print} /Node [0-7]/' input

To print the line before the PANIC match, it's probably easiest to simply record the previous line. Something like:

 awk '/PANIC:/{print prev; print; getline; print} { prev = $0 } /Node [0-7]/ || /^ *$/' input | perl -00pe1

use grep command to get a word after a match [ Linux ]

You can use \K, which tells the engine to pretend that the match attempt started at this position. You can have something like:

grep -oP '.*:\K(.*)'

Example:

$ echo "hello:world" | grep -oP ":\K.*"
world

Grep characters before and after match?

3 characters before and 4 characters after

$> echo "some123_string_and_another" | grep -o -P '.{0,3}string.{0,4}'
23_string_and

How to extract string following a pattern with grep, regex or perl

Since you need to match content without including it in the result (must
match name=" but it's not part of the desired result) some form of
zero-width matching or group capturing is required. This can be done
easily with the following tools:

Perl

With Perl you could use the n option to loop line by line and print
the content of a capturing group if it matches:

perl -ne 'print "$1\n" if /name="(.*?)"/' filename

GNU grep

If you have an improved version of grep, such as GNU grep, you may have
the -P option available. This option will enable Perl-like regex,
allowing you to use \K which is a shorthand lookbehind. It will reset
the match position, so anything before it is zero-width.

grep -Po 'name="\K.*?(?=")' filename

The o option makes grep print only the matched text, instead of the
whole line.

Vim - Text Editor

Another way is to use a text editor directly. With Vim, one of the
various ways of accomplishing this would be to delete lines without
name= and then extract the content from the resulting lines:

:v/.*name="\v([^"]+).*/d|%s//\1

Standard grep

If you don't have access to these tools, for some reason, something
similar could be achieved with standard grep. However, without the look
around it will require some cleanup later:

grep -o 'name="[^"]*"' filename

A note about saving results

In all of the commands above the results will be sent to stdout. It's
important to remember that you can always save them by piping it to a
file by appending:

> result

to the end of the command.

Grep for string if exist after pattern

 grep -rE --include=\*.log -o "PARTICULAR*=\w+"

Based on the output you included, you should now get:

$> grep -rE --include=\*.log -o "PARTICULAR*=\w+"
foo/bar.log:PARTICULAR=gfgfgfgkl5dfgfgf
foo/bar.log:PARTICULAR=56945674454rgfgfg
foo/bar.log:PARTICULAR=gkjdghfidfgh4545454


Related Topics



Leave a reply



Submit