Below is a row and i need to close "20 with "20"

  • 1
    Is it always "20 or could it be "any-number or "any-text? Is it only one on a row, or can there be many of these instances? – Kusalananda Feb 22 at 21:02
  • it is always like this |"20| in only one row – Mohini Singh Feb 22 at 21:07

Let's take this as a test file:

$ cat text

To close all open ", try:

$ sed -E 's/^("[^"|]*)\|/\1"|/; :a; s/(\|"[^"|]*)\|/\1"|/g; ta; s/\|"[^"|]*$/&"/' text

How it works

If the unclosed quote can occur in the first field, a middle field or the end field, then there are three cases to consider. Thus, we have three substitute commands:

  • s/^("[^"|]*)\|/\1"|/

    If the line begins with a quote but no quote appears before the first |, this adds the quote.

  • :a; s/(\|"[^"|]*)\|/\1"|/g; ta;

    If a quote is missing in a middle field, this adds it. To make sure that all such missing quotes are added, the substitute command is repeated until no more changes are made.

  • s/\|"[^"|]*$/&"/

    If the final field is missing a closing quote, then it is added.

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.