Reputation: 21
File1:
Z "A B 'c'" A
D "A B 'c'" B
GH H
Desired output:
Z "A B 'c'" A
D B
GH H
How can I remove the pattern " A B 'c' "
, but only in lines which do not contain pattern Z
, using bash
, awk
, sed
, or grep
?
Upvotes: 1
Views: 83
Reputation: 21965
Was about [ answer ] the same way [ @ed-morton ] did but now I feel tempted to post a variant of the answer which uses hexadecimal representation of '
ie \x27
awk '!/Z/{sub(/"A B \x27\c\x27"/,"")}1' 38258963
Output
Z "A B 'c'" A
D B
GH H
Note
The only difference here is that you need to escape the c
which also is a hexadecimal digit coz it is close knit to single quote
in the pattern
Upvotes: 0
Reputation: 203358
You can't use single quotes inside a single quote delimited script. That's a shell thing, and applies whether it's an awk or sed or any other script. There's various workarounds but the simplest and most robust is to use the octal number that represents a '
, i.e. \047
, instead:
$ awk '!/Z/{sub(/"A B \047c\047"/,"")} 1' file
Z "A B 'c'" A
D B
GH H
Note that when you remove the string you're interested in it leaves 2 blanks between D and B. Add a blank to whichever end of the RE is correct to resolve that if you care.
Upvotes: 1
Reputation: 8406
Use sed
, but before that get around bash
's quoting restrictions so that sed
has the correct target string. First single quote the pattern's double quotes as far as possible, and then double quote the single quotes, and repeat as needed:
sed '/Z/!s/"A B '"'c'"'" //g' File1
Upvotes: 2