LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Matching double quotes " (https://www.linuxquestions.org/questions/programming-9/matching-double-quotes-845999/)

hattori.hanzo 11-23-2010 05:52 AM

Matching double quotes "
 
Code:

Dhcp Server 190.21.12.50 add scope 190.21.12.0 255.255.255.0 "New York" ""
I am using this to extract the network and mask.

Code:

grep "add scope" file | cut -d" " -f6,7
What would one use to extract the Description between the double quotes?

Thanks & Regards,

catkin 11-23-2010 05:59 AM

Code:

grep "add scope" file | cut -d'"' -f2

sycamorex 11-23-2010 06:02 AM

awk -F\" '{print $2}' file
or
awk -F\" '{print $2,$4}' file
To match the contents of the second double quotes

hattori.hanzo 11-23-2010 06:43 AM

Thanks for the input but is it possible to accomplish this in one line.

I guess I could run it thru twice then combine the output.

Quote:

grep "add scope" file | cut -d" " --output-delimiter="/" -f6,7
grep "add scope" file | cut -d'"' -f2
Thanks & Regards,

grail 11-23-2010 06:58 AM

How about:
Code:

sed -r '/add scope/s@.*scope ([^ ]+) ([^ ]+) "([^"]+)".*@\1/\2 \3@' file

colucix 11-23-2010 07:06 AM

Using awk (as previously suggested by sycamorex):
Code:

$ awk -F\" '/add scope/{n = split($1,_," "); printf "%s/%s %s\n", _[6], _[7], $2}' file
190.21.12.0/255.255.255.0 New York


colucix 11-23-2010 07:07 AM

Quote:

Originally Posted by grail (Post 4168135)
How about:
Code:

sed -r '/add scope/s@.*scope ([^ ]+) ([^ ]+) "([^"]+)".*@\1/\2 \3@' file

Hi grail! Nice sed one-liner! :)

hattori.hanzo 11-24-2010 12:49 AM

Thanks guys. I will need to study both sed and awk solutions.


All times are GMT -5. The time now is 06:55 PM.