1

I have the following text:

Variables = X, Y, Z
Zone T="Run 0020 Probe Points", I=19, J=1, F=POINT
DT=(double double double)
AUXDATA Zone_1_X = 28.180958

I want to add double quotes (") around the 28.180958 to get:

Variables = X, Y, Z
Zone T="Run 0020 Probe Points", I=19, J=1, F=POINT
DT=(double double double)
AUXDATA Zone_1_X = "28.180958"

I can successfully use the sed -i 4s/=\ /=\ \"/ ... to insert the double quote ahead of the number, but appending the double quote is elusive. I've tried various methods with sed to append the double quote on the AUXDATA line, but the double quote appears below the AUXDATA line, not at the end of the actual line. For example, sed 4a'"' data.txt produced:

Variables = X, Y, Z
Zone T="Run 0020 Probe Points", I=19, J=1, F=POINT
DT=(double double double)
AUXDATA Zone_1_X = 28.180958
"

Everything I've tried either puts the double quote (") above or below the AUXDATA line. How do I get the double quote at the end of the AUXDATA line itself?

1

The trick is to match something at the end of the line, such as the last space-delimited token on that line, and then re-write that line with double-quotes around that token.

This command will do that:

sed -E 's/^(AUXDATA.*) ([^ ]*)$/\1 "\2"/' < foo

The -E option tells sed we will use extended regex syntax.

The regex itself breaks the line into two pieces:

  • The part that has AUXDATA at the beginning of the line (^ indicates beginning of line), followed by arbitrary text ending with a space

  • the part of the line that contains no spaces and ends at the end of the line

Specifically in this case, the first piece matches AUXDATA Zone_1_X = and the second piece matches 28.180958. sed then replaces that line with:

  • the first part, \1 followed by a space
  • a double-quote, then the second part \2 and another double-quote.

All other lines of the input file are passed through unaltered by sed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.