質問

I am using sed command in Ubuntu for making shell script.

I have a problem because the string I am inserting has both single and double quotes. Dashes also. This is the expample:

sed -i "16i$('#myTable td:contains("Trunk do SW-BG-26, GigabitEthernet0/22")').parents("tr").remove();" proba.txt

It should insert

$('#myTable td:contains("Trunk do SW-BG-26, GigabitEthernet0/22")').parents("tr").remove(); 

in line 16 of the file proba.txt

but instead it inserts

$('#myTable td:contains( 

because it exits prematurely . How can resolve this, I cannot find solution here on site bcause I have both quotation signs and there are explanations only for one kind.

2nd try

I set \ in front every double quote except the outermost ones but I still didn't get what I want. Result is:

.parents("tr").remove();

Then I put \ in front of every ' too but the result was an error in script. This is the 4th row:

sed -i "16i$(\'#myTable td:contains(\"QinQ tunnel - SCnet wireless\")\').parents(\"tr\").remove();" proba.txt

This is the error:

4: skripta.sh: Syntax error: "(" unexpected (expecting ")")

Maybe there is easier way to insert line into the file at the exact line if that line has ", ', /?

3rd time is a charm

Inserting many lines last day I came across another problem using sed. I want to insert this text:

$(document).ready( function() {

with command:

sed -i "16i$(document).ready( function() {" proba.txt

and I get as result this text inserted as document is something special or because of the $:

.ready( function() {

Any thoughts about that?

役に立ちましたか?

解決

There are two ways around this. The easy way out is to put the script into a file and use that on the command line. For example, sed.script contains:

16i\
$('#myTable td:contains("Trunk do SW-BG-26, GigabitEthernet0/22")').parents("tr").remove();

and you run:

sed -f sed.script ...

If you want to do it without the file, then you have to decide whether to use single quotes or double quotes around your sed -e expression. Using single quotes is usually easier; there are no other special characters to worry about. Each embedded single quote is replaced by '\'':

sed -e '16i\
$('\''#myTable td:contains("Trunk do SW-BG-26, GigabitEthernet0/22")'\'').parents("tr").remove();' ...

If you want to use double quotes, then each embedded double quote needs to be replaced by \", but you also have to escape embedded back quotes `, dollar signs $ and backslashes \:

sed -e "16i\\
\$('#myTable td:contains(\"Trunk do SW-BG-26, GigabitEthernet0/22\")').parents(\"tr\").remove();" ...

(To the point: I forgot to escape the $ before I checked the script with double quotes; I got the script with single quotes right first time.)

Because of all the extra checking, I almost invariably use single quotes, unless I need to get shell variables substituted into the script.

他のヒント

sed -i "6 i\\
\$('#myTable td:contains(\"Trunk do SW-BG-26, GigabitEthernet0/22\")').parents(\"tr\").remove();" proba.txt

escape the double quote, the slash and new line needed after the i instruction and the $ due to double quote shell interpretation

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top