0

I would like to extract string after x_rundir from a file full with text.

Trying with

grep -w "x_rundir" xfile.txt

but it extracts whole sentence from the file

xfile.txt:

00:39:11-INFO: x_rundir: /i/am/a/path/

Expected result:

/i/am/a/path
anubhava
  • 761,203
  • 64
  • 569
  • 643
peace
  • 149
  • 7

1 Answers1

0

You may use this gnu-grep command:

grep -oP 'x_rundir: \K\S+' xfile.txt

/i/am/a/path/

Here:

  • -P enabled Perl regex mode
  • -o prints only matched text
  • \K resets matched info
  • \S+ matches 1+ non-whitespaces
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    This works. Thanks. May I ask what are those \K \S+ thingy called? I would like to know more. – peace Nov 15 '22 at 03:28
  • `\K` resets all matched information that comes before it. `\S+` matches 1 or more non-whitespace characters – anubhava Nov 15 '22 at 05:02