I need a command that grep command to find a text after the matching pattern until the space.

I have a line in a file


and I need the output only as LINUX.

The below command give the output delta/LINUX,

grep -o 'delta/[^"]*'  file.txt

What I need is only LINUX. Please let me know what to change in this to get the expected output.

  • Welcome to the site. How do you identify the matching line. Is it the only one containing shell= or the only one containing delta/, or is there any other criterion?
    – AdminBee
    Aug 23, 2021 at 13:53

4 Answers 4


With grep that supports PCRE with -P flag:

grep -Po 'delta/\K[^"]*' file.txt

\K = Keep out

Alternative, pipe the result to another command, e.g. cut or a second grep, e.g.

grep -o 'delta/[^"]*'  file.txt | cut -d/ -f2
  • Thanks a lot. It is working.! Aug 24, 2021 at 10:11

You can use sed:

sed '/delta\//!d;s_.*/__;s/"//' file
  • /delta\//!d does delete all lines that don't (the !) contain your pattern delta/, just in case there could be other lines in your file
  • s_.*/__ substitutes everything upto the slash with nothing, so it gets removed
  • s/"// finally removes the trailing double quote
sed -E 's|(.*=./.*/.*/)(.*).|\2|'

Match and print the second group in this case LINUX


You can use awk and tell it to use both / and " as field separators. e.g.

$ echo 'shell="/mix/delta/LINUX"' | awk -F'[/"]' '/^shell=/ {print $5}'

LINUX is field 5 because awk counts even empty fields (i.e. the empty field between the first " and the first /, and the empty field after the final ")

If you don't know how long the pathname is going to be but know you're always going to be the last path element before the closing double-quote, you can use $(NF-1) to output the second-last field:

$ echo 'shell="/mix/delta/LINUX"' | awk -F'[/"]' '/^shell=/ {print $(NF-1)}'

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.