Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SED replace everything not in between quotes

Tags:

unix

sed

I have a repeating list of this text, and the "testestewst" is unique for each item in the list:

testestetet *testest = [testeeste qtestest:@"I need this text"];
[testesteste:@"I need this text"];
[stesteset atestestest];

How in the world do I do a SED replace to remove everything but the text I need?


2 Answers

sed 's/.*"\(.*\)".*/\1/g' < test > result

works at least if you only one of such item per line... (replace 'test' and 'result' with the names of input and output files.

EDIT :

sed 's/^[^"]*$//g' < test | sed 's/.*"\(.*\)".*/\1/g' > result

Replaces also all lines without quotes with new lines...

like image 113
Matthieu Avatar answered Jan 22 '26 21:01

Matthieu


If I understand what you are asking for, this should work...I went to some trouble to only return quoted text on lines containing an @. If your goal can be accomplished by just pulling out quoted text then it's possibly simpler.

$ cat > t1.sed
/@/{
s/[^"]*@"//
s/".*//
p
}
d
$ sed -f t1.sed t1.dat
I need this text
I need this text
$
like image 40
DigitalRoss Avatar answered Jan 22 '26 22:01

DigitalRoss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!