Regular expression (sed)

Hi

I need to get text that are within ""

For example

File:
asdasd "test test2" sadasds asdda asdasd "demo demo2"

Output:
test test2 demo demo2

Any help is good

Thank you

$ sed 's/\(.*\)\(\".*\"\)\(.*\)\(\".*\"\)/\2 \4/g;s/\"//g' file
test test2 demo demo2
1 Like
sed 's/[^"]*"\([^"]*\)"[^"]*/\1 /g' infile
1 Like

I works

echo '--yyy "test test2" sadasds --xxx "demo demo2"' | sed 's/\(.*\)\(\".*\"\)\(.*\)\(\".*\"\)/\2 \4/g;s/\"//g'

Output: test test2 demo demo2

But, now i need obtain only text that start with --xxx

Output:
demo demo2

Thanks for your quick help

$ ruby -ne 'puts $_.scan(/\".[^"]*?\"/)' file
"test test2"
"demo demo2"
$ echo '--yyy "test test2" sadasds --xxx "demo demo2"' | ruby -e 'print gets.split("--xxx")[-1]'
 "demo demo2"

1 Like

Assuming one --xxx per line, and that there might be text following the quoted string, this will work:

 sed -r 's/.*--xxx[^"]*"//; s/".*//'
1 Like
# echo '--yyy "test test2" sadasds --xxx "demo demo2"' | sed 's/.*-xxx \(.*\)/\1/'
"demo demo2"
# echo '--yyy "test test2" sadasds --xxx "demo demo2"' | sed 's/.*-xxx "\(.*\)"/\1/'
demo demo2
1 Like

I works

Thank all for comments

using Perl:-

perl -wlne 'printf "$1 " while (s/\"(.*?)\"//);' infile.txt

;);):wink:

1 Like

Could you please explain your code?:slight_smile:

It looks for 0 or more characters-that-are-not-a-double-quote in front of a double quote. The zero or more characters-that-are-not-a-double-quote that follow are in parentheses, so they are group 1. All this has to be followed by a double quote and 0 or more characters-that-are-not-a-double-quote. If there is a match It replaces all this with what was stored in group 1 followed by a single space. It repeats for every such pattern on the line (g flag).

1 Like

Thank you :slight_smile:

Perl version of the same thing:

perl -pe 's/.*?"(.*?)".*?/\1 /g'
1 Like