I have a pipe separated file,read file row wise and search for unclosed quotes(") in each row and close that












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"|









share|improve this question




















  • 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
















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"|









share|improve this question




















  • 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














1












1








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"|









share|improve this question
















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"|






text-processing quoting csv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 22 at 21:43









Jeff Schaller

43.4k1160140




43.4k1160140










asked Feb 22 at 21:01









Mohini SinghMohini Singh

82




82








  • 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














  • 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








1




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







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





it is always like this |"20| in only one row

– Mohini Singh
Feb 22 at 21:07










1 Answer
1






active

oldest

votes


















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.








share|improve this answer



















  • 1





    Thanks alot,it worked :) (Y)

    – Mohini Singh
    Feb 22 at 21:44











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502398%2fi-have-a-pipe-separated-file-read-file-row-wise-and-search-for-unclosed-quotes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.








share|improve this answer



















  • 1





    Thanks alot,it worked :) (Y)

    – Mohini Singh
    Feb 22 at 21:44
















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.








share|improve this answer



















  • 1





    Thanks alot,it worked :) (Y)

    – Mohini Singh
    Feb 22 at 21:44














0












0








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.








share|improve this answer













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.









share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 22 at 21:33









John1024John1024

47.6k5110126




47.6k5110126








  • 1





    Thanks alot,it worked :) (Y)

    – Mohini Singh
    Feb 22 at 21:44














  • 1





    Thanks alot,it worked :) (Y)

    – Mohini Singh
    Feb 22 at 21:44








1




1





Thanks alot,it worked :) (Y)

– Mohini Singh
Feb 22 at 21:44





Thanks alot,it worked :) (Y)

– Mohini Singh
Feb 22 at 21:44


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502398%2fi-have-a-pipe-separated-file-read-file-row-wise-and-search-for-unclosed-quotes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to make a Squid Proxy server?

第一次世界大戦

Touch on Surface Book