1

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

2019-02-15T10:25:19+0100|ttt|werewrw|erewr|fddsfsdf|dfsdf|07|2ee7|1b65d04|"20|19|-02-|15t10:03|45435435|765767||SIP;rr=200;text="hyt"|
  • 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
0

Let's take this as a test file:

$ cat text
start|10|"20|end
"10"|"20|"30"|end
"20|"10|"30|end
abc|def|"20"|30|"end

To close all open ", try:

$ sed -E 's/^("[^"|]*)\|/\1"|/; :a; s/(\|"[^"|]*)\|/\1"|/g; ta; s/\|"[^"|]*$/&"/' text
start|10|"20"|end
"10"|"20"|"30"|end
"20"|"10"|"30"|end
abc|def|"20"|30|"end"

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.