linux extract string from a file


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


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

Expected result:



You may use this gnu-grep command:

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



  • -P enabled Perl regex mode
  • -o prints only matched text
  • \K resets matched info
  • \S+ matches 1+ non-whitespaces

Answered By – anubhava

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published