-2

i need to extract path from string

for e.g

title="set key invert ; set bmargin 0 ; set multiplot ; set size 1.0 , 0.33 ; set origin 0.0 , 0.67 ; set format x "" ; set xtics offset 15.75 "1970-01-01 00:00:00" , 31556736 ; plot "/usr/local/lucid/www/tmp/20171003101438149255.dat" using 1:5 notitle with linespoints ls 2'"

Then expected output should be

/usr/local/lucid/www/tmp/20171003101438149255.dat  

using awk or grep

1
  • Are you asking for help to parse a file containing that text, or a variable named title set as you show (which then would be wrong since you're trying to contain " within a "-delimited string) or something else? Commented Oct 3, 2017 at 13:36

3 Answers 3

0

sed approach:

title='set key invert ; set bmargin 0 ; set multiplot ; set size 1.0 , 0.33 ; set origin 0.0 , 0.67 ; set format x "" ; set xtics offset 15.75 "1970-01-01 00:00:00" , 31556736 ; plot "/usr/local/lucid/www/tmp/20171003101438149255.dat" using 1:5 notitle with linespoints ls 2'

sed 's/.* plot "\([^"]\+\).*/\1/' <<<$title
/usr/local/lucid/www/tmp/20171003101438149255.dat
Sign up to request clarification or add additional context in comments.

Comments

0

With grep solution,

grep -oP '"\K/[^"]*(?=")' <<< $title

With awk solution,

awk '{match($0,/\/[^"]*/,a);print a[0]}' <<< $title

Comments

0

Shorter regex with grep:

grep -oP 'plot "\K[^"]+' <<< $title
/usr/local/lucid/www/tmp/20171003101438149255.dat

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.