Operating System - HP-UX
1820263 Members
2968 Online
109622 Solutions
New Discussion юеВ

Extract text between single quotes

 
SOLVED
Go to solution
Brian K. Arnholt
Frequent Advisor

Extract text between single quotes

I have a file with various lines of text. There is a section of text between single-quotes that I need to extract (without the single quotes) the text (its a file name). I looked through the sed help on the forums and I must have a brain-block because I can't figure it out. Any ideas?

A sample line of text is below, and I need to extract the path name between the single quotes:

PATH '/nfs.databank/BND/L1/PP3/LSA-rest/la3638g5MR.lb' ;
Some see things as they are and ask why, I dream of things that never were and ask why not?
5 REPLIES 5
Massimo Bianchi
Honored Contributor

Re: Extract text between single quotes

Hi,
try

awk ' /\'/,/\'/ ' YOURFILE

HTH,
Massimo

Ian Dennison_1
Honored Contributor

Re: Extract text between single quotes

Assume that LINE1 contains the line above

export NEWPATH=`echo $LINE1 |awk '{print $2}' |sed "s/\'//g"`

Share and Enjoy! Ian
Building a dumber user
James R. Ferguson
Acclaimed Contributor
Solution

Re: Extract text between single quotes

Hi Brian:

Set 'awk's field delimiter:

echo "PATH '/nfs.databank/BND/L1/PP3/LSA-rest/la3638g5MR.lb'"|awk -F"'" '{print $2}'

Regards!

...JRF...
Ian Lochray
Respected Contributor

Re: Extract text between single quotes

How about ....
grep "'.*'" filename | sed -e "s/\(.*'\)\(.*\)\('.*\)/\\2/"
Brian K. Arnholt
Frequent Advisor

Re: Extract text between single quotes

Thanks, I new it was simple, I just am having one of those days and the forumers came through...
Both Ian's solutions worked, but JRF's was the cleanest and works great for what I need.

Thanks again.
Brian
Some see things as they are and ask why, I dream of things that never were and ask why not?