47

I have a command output from which I want to remove the double quotes ".

Command:

strings -a libAddressDoctor5.so |\
grep EngineVersion |\
awk '{if(NR==2)print}' |\
awk '{print$2}'

Output:

EngineVersion="5.2.5.624"

I'd like to know how to remove unwanted characters with awk or sed.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
AabinGunz
  • 12,109
  • 54
  • 146
  • 218

3 Answers3

56

Use sed's substitution: sed 's/"//g'

s/X/Y/ replaces X with Y.

g means all occurrences should be replaced, not just the first one.

Piotr Praszmo
  • 17,928
  • 1
  • 57
  • 65
45

Using just awk you could do (I also shortened some of your piping):

strings -a libAddressDoctor5.so | awk '/EngineVersion/ { if(NR==2) { gsub("\"",""); print $2 } }'

I can't verify it for you because I don't know your exact input, but the following works:

echo "Blah EngineVersion=\"123\"" | awk '/EngineVersion/ { gsub("\"",""); print $2 }'

See also this question on removing single quotes.

gregswift
  • 4,568
  • 1
  • 20
  • 8
  • 5
    Awk's `gsub` function is what I needed to remove characters directly from a awk script, not the command line, thank you. – pawamoy Feb 20 '18 at 14:36
19

tr can be more concise for removing characters than sed or awk, especially when you want to remove multiple different characters from a string.

Removing double quotes:

echo '"Hi"' | tr -d \"
# Prints Hi without quotes

Removing different kinds of brackets:

echo '[{Hi}]' | tr -d {}[]
# Prints Hi without brackets

-d stands for "delete".

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171