How to extract a piece of text within double quote

2019-08-24 18:58发布

问题:

He is a nice guy. "Rob" doesn't like it though.

How to extract Rob from this line using shell script?

回答1:

This is a way of doing it, the first that came to my mind:

echo "He is a nice guy. \"Rob\" doesn't like it though" | cut -d'"' -f2

Of course it could have been done with sed or awk.

Is this homework?



回答2:

$ echo "He is a nice guy. \"Rob\" doesn't like it though." | gawk -F'"' '{print $2}'
Rob


回答3:

you can just use the shell

$ string="He is a nice guy. \"Rob\" doesn't like it though."
$ string=${string#*\"}
$ echo ${string%%\"*}
Rob


回答4:

Here is another solution, try this

echo "ROB" | tr ""''

The first two "" are double quotes and the last two are single quotes



标签: shell