0

I am in need to extract all characters after a pattern match.

For example ,

NAME=John

Age=16

I need to extract all characters after "=". Output should be like

John

16

I cant go with perl or Jython for this purpose because of some restrictions. I tried with grep , but to my knowledge I came as shown below only

echo "NAME=John" |grep -o -P '=.{0,}'

skanagasabap
  • 910
  • 3
  • 12
  • 24

2 Answers2

1

You were pretty close:

grep -oP '(?<=\w=)\w+' file

makes it.

Explanation

it looks for any word after word= and prints it.

  • -o stands for "Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line".
  • -P stands for "Interpret PATTERN as a Perl regular expression".
  • (?<=\w=)\w+ means: match only \w+ following word=. More info in [Regex tutorial - Lookahead][1] and in [this nice explanation by sudo_O][2].

Test

$ cat file
NAME=John
Age=16
$ grep -oP '(?<=\w=)\w+' file
John
16
fedorqui
  • 275,237
  • 103
  • 548
  • 598
0

One sed solution

sed -ne 's/.*=//gp' <filename>

another awk solution

awk -F= '$0=$2' <filename>

Explanation:

in sed we remove anything from the beginning of a line till a = and print the rest. in awk we break the string in 2 parts, separated by =, now after that $0=$2 is making replacing the whole string with the second portion

abasu
  • 2,454
  • 19
  • 22