Get string between 2 patterns

Hi all, I wish to get the string between u' and '

This is the test.txt file:

PLAY [SERVER] *********************************** 
GATHERING FACTS ***************************** 
OK: [20.6.1.1] 
TASK: [Get name] **************************** 
changed: [20.6.1.1] 
TASK: [print name] *************************** 
ok: [20.6.1.1] => {"msg": "[u'/project/cars/version/scripts/', u'/project/cars/version/scripts/y.txt', u'/project/cars/version/scripts/x.txt']"} 
PLAY RECAP *********************** 
20.6.1.1  : ok=3  changed=1 unreachable=0 failed=0 
 

The output that I want is:

/project/cars/version/scripts/
/project/cars/version/scripts/y.txt
/project/cars/version/scripts/x.txt
 

The above strings that I want are between " u' " and " ' ".

I have researched and tried many of the suggested method but fail.
One of those: sed 's/.*u' //' | sed 's/'.*$//'

Can anyone help me?

Thank you!

Done with bash.

grep "'" test.dat | cut -d \' -f2
grep "'" test.dat | cut -d \' -f4
grep "'" test.dat | cut -d \' -f6

You could also do

grep "'" test.dat | cut -s -d \' -f2,4,6

in a single line but you would have to separate the substrings.

Suprisingly the onliner does result in

/project/cars/version/scripts/'/project/cars/version/scripts/y.txt'/project/cars/version/scripts/x.txt

with ticks (') that i would not have expected there....

Using awk

awk 'NR%2==0' RS=\' file
/project/cars/version/scripts/
/project/cars/version/scripts/y.txt
/project/cars/version/scripts/x.txt
1 Like
awk -F\' '{print $2}' RS=, infile

-ahamed

Gives me, on bash :

$ awk -F\' '{print $2}' RS=, test.dat
GATHERING FACTS *****************************
/project/cars/version/scripts/y.txt
/project/cars/version/scripts/x.txt

Hi,
You can try (work with grep that supported -P option (regex engine perl):

grep -P -o "u[']\K[^']*" file

Regards.

Though my solution is not the cleanest, it works.
Check if you have put a comma accidentally in your dat file.

-bash-3.2$ awk -F\' '{print $2}' RS=, infile
/project/cars/version/scripts/
/project/cars/version/scripts/y.txt
/project/cars/version/scripts/x.txt

--ahamed

Yours is neater than mine and should work for me as well.

Neither a comma nor a tick.

$ grep \' test.dat
ok: [20.6.1.1] => {"msg": "[u'/project/cars/version/scripts/', u'/project/cars/version/scripts/y.txt', u'/project/cars/version/scripts/x.txt']"}
$ grep , test.dat
ok: [20.6.1.1] => {"msg": "[u'/project/cars/version/scripts/', u'/project/cars/version/scripts/y.txt', u'/project/cars/version/scripts/x.txt']"}
$ awk -F\' '{print $2}' RS=, test.dat
GATHERING FACTS *****************************
/project/cars/version/scripts/y.txt
/project/cars/version/scripts/x.txt
$ cat test.dat
PLAY [SERVER] ***********************************
GATHERING FACTS *****************************
OK: [20.6.1.1]
TASK: [Get name] ****************************
changed: [20.6.1.1]
TASK: [print name] ***************************
ok: [20.6.1.1] => {"msg": "[u'/project/cars/version/scripts/', u'/project/cars/version/scripts/y.txt', u'/project/cars/version/scripts/x.txt']"}
PLAY RECAP ***********************
20.6.1.1  : ok=3  changed=1 unreachable=0 failed=0

A mystery to me. But one i'm willing to ignore for now. :sunglasses:

Did my solution in post #3 works?
If so, why not use it.

sed:

sed -n ":a s/^[^u]*u'//; T;h; s/'.*$//p; g; ta" file
/project/cars/version/scripts/
/project/cars/version/scripts/y.txt
/project/cars/version/scripts/x.txt

An other sed version:

sed -n "s/[^u]*u'\([^']*\)['][^u]*/\1\n/gp" file

Regards.

Hi all, thanks for your invaluable inputs. Works beautifully.