How to add quotes to line break field values in a file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












2















Input file having and two records and three fields, second attribute contains new line characters.I want enclose every field value with double quotes.



Input File:



100|Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce|2019-01-24
200|Minimum amount not avialable in your account.
Last time deposited amonut month ago|2019-01-24


Required output should be like below



"100"|"Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce"|"2019-01-24"
"200"|"Minimum amount not avialable in your account.
Last time deposited amonut month ago"|"2019-01-24"









share|improve this question
























  • Are you able to modify the code or output format of whatever produces this data?

    – Kusalananda
    Jan 24 at 10:04











  • Wait, so there are linebreaks in the delimited strings?!

    – Panki
    Jan 24 at 10:16











  • Yes, OP says: second attribute contains new line characters.

    – RoVo
    Jan 24 at 10:19












  • @RoVo what is "second attribute" anyway?

    – Niko Gambt
    Jan 24 at 11:45






  • 1





    You should ask OP ... but I guess it means second field of his |sv.

    – RoVo
    Jan 24 at 11:58















2















Input file having and two records and three fields, second attribute contains new line characters.I want enclose every field value with double quotes.



Input File:



100|Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce|2019-01-24
200|Minimum amount not avialable in your account.
Last time deposited amonut month ago|2019-01-24


Required output should be like below



"100"|"Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce"|"2019-01-24"
"200"|"Minimum amount not avialable in your account.
Last time deposited amonut month ago"|"2019-01-24"









share|improve this question
























  • Are you able to modify the code or output format of whatever produces this data?

    – Kusalananda
    Jan 24 at 10:04











  • Wait, so there are linebreaks in the delimited strings?!

    – Panki
    Jan 24 at 10:16











  • Yes, OP says: second attribute contains new line characters.

    – RoVo
    Jan 24 at 10:19












  • @RoVo what is "second attribute" anyway?

    – Niko Gambt
    Jan 24 at 11:45






  • 1





    You should ask OP ... but I guess it means second field of his |sv.

    – RoVo
    Jan 24 at 11:58













2












2








2








Input file having and two records and three fields, second attribute contains new line characters.I want enclose every field value with double quotes.



Input File:



100|Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce|2019-01-24
200|Minimum amount not avialable in your account.
Last time deposited amonut month ago|2019-01-24


Required output should be like below



"100"|"Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce"|"2019-01-24"
"200"|"Minimum amount not avialable in your account.
Last time deposited amonut month ago"|"2019-01-24"









share|improve this question
















Input file having and two records and three fields, second attribute contains new line characters.I want enclose every field value with double quotes.



Input File:



100|Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce|2019-01-24
200|Minimum amount not avialable in your account.
Last time deposited amonut month ago|2019-01-24


Required output should be like below



"100"|"Alert created becuase of high volume.
Money withdrawan more 5000.
Try to access acccount from unathorised devíce"|"2019-01-24"
"200"|"Minimum amount not avialable in your account.
Last time deposited amonut month ago"|"2019-01-24"






text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 24 at 10:23









Jeff Schaller

41.3k1056131




41.3k1056131










asked Jan 24 at 9:59









chava kumarchava kumar

183




183












  • Are you able to modify the code or output format of whatever produces this data?

    – Kusalananda
    Jan 24 at 10:04











  • Wait, so there are linebreaks in the delimited strings?!

    – Panki
    Jan 24 at 10:16











  • Yes, OP says: second attribute contains new line characters.

    – RoVo
    Jan 24 at 10:19












  • @RoVo what is "second attribute" anyway?

    – Niko Gambt
    Jan 24 at 11:45






  • 1





    You should ask OP ... but I guess it means second field of his |sv.

    – RoVo
    Jan 24 at 11:58

















  • Are you able to modify the code or output format of whatever produces this data?

    – Kusalananda
    Jan 24 at 10:04











  • Wait, so there are linebreaks in the delimited strings?!

    – Panki
    Jan 24 at 10:16











  • Yes, OP says: second attribute contains new line characters.

    – RoVo
    Jan 24 at 10:19












  • @RoVo what is "second attribute" anyway?

    – Niko Gambt
    Jan 24 at 11:45






  • 1





    You should ask OP ... but I guess it means second field of his |sv.

    – RoVo
    Jan 24 at 11:58
















Are you able to modify the code or output format of whatever produces this data?

– Kusalananda
Jan 24 at 10:04





Are you able to modify the code or output format of whatever produces this data?

– Kusalananda
Jan 24 at 10:04













Wait, so there are linebreaks in the delimited strings?!

– Panki
Jan 24 at 10:16





Wait, so there are linebreaks in the delimited strings?!

– Panki
Jan 24 at 10:16













Yes, OP says: second attribute contains new line characters.

– RoVo
Jan 24 at 10:19






Yes, OP says: second attribute contains new line characters.

– RoVo
Jan 24 at 10:19














@RoVo what is "second attribute" anyway?

– Niko Gambt
Jan 24 at 11:45





@RoVo what is "second attribute" anyway?

– Niko Gambt
Jan 24 at 11:45




1




1





You should ask OP ... but I guess it means second field of his |sv.

– RoVo
Jan 24 at 11:58





You should ask OP ... but I guess it means second field of his |sv.

– RoVo
Jan 24 at 11:58










3 Answers
3






active

oldest

votes


















1














You can try with this awk :



awk -F'n' '!f$1="""$1;f=1"")f==3$0=$0""";f=01' infile





share|improve this answer























  • Thanks a lot.it is working as expected. But it is not accepting when i tired to parameterized like this awk -F'n' '!f$1="""$1;f=1"")f==$Fieldcnt$0=$0""";f=01'

    – chava kumar
    Jan 26 at 11:46












  • @chavakumar Fieldcnt is not defined. If it's a shell variable : try to pass it to awk with -v.

    – ctac_
    Jan 26 at 12:58











  • I have tried with option -v and it was worked. awk -v NOOFATTRIBUTES="$NOOFATTRIBUTES" -F'n' '!f$1="""$1;f=1"")f==NOOFATTRIBUTES$0=$0""";f=01'

    – chava kumar
    Jan 26 at 14:08











  • Thanks a lot for your quick help.

    – chava kumar
    Jan 26 at 14:09


















1














You can do it with perl:



perl -0pe 's/([^|]*)|([^|]*)|([^|n]*)(n|$)/"1"|"2"|"3"4/g' input_file



  • -0 read file at once, not line by line.


  • -p print the line in the end


  • -e the expression


  • s/pattern/replacement/g Replace pattern with replacement





share|improve this answer
































    0














    $ cat input.txt | tr "n" "t" 
    |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i="""$i""";1'
    |tr "t" "n"



    • tr transform new line to a tab. So the input is linearized

    • We tell awk to handle the | as a field seperator. Now we can iterat over each field and add the quotation marks around. Afterwards the whole line is printed


    • tr converts the tab back to a new line





    share|improve this answer

























    • Thank you for help, but this code didnt work in my case.

      – chava kumar
      Jan 26 at 11:59











    • "didn't work" is never a good error description. Could you please describe what doesn't work?

      – finswimmer
      Jan 26 at 12:07











    • Hi, It is not generated any error but there is no change in output. Output is same as input.command used like this. cat filename.txt | tr "n" "t" |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i=""$i"";1' |tr "t" "n"

      – chava kumar
      Jan 26 at 12:33












    • Could you try my edited version?

      – finswimmer
      Jan 28 at 4:58










    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%2f496417%2fhow-to-add-quotes-to-line-break-field-values-in-a-file%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can try with this awk :



    awk -F'n' '!f$1="""$1;f=1"")f==3$0=$0""";f=01' infile





    share|improve this answer























    • Thanks a lot.it is working as expected. But it is not accepting when i tired to parameterized like this awk -F'n' '!f$1="""$1;f=1"")f==$Fieldcnt$0=$0""";f=01'

      – chava kumar
      Jan 26 at 11:46












    • @chavakumar Fieldcnt is not defined. If it's a shell variable : try to pass it to awk with -v.

      – ctac_
      Jan 26 at 12:58











    • I have tried with option -v and it was worked. awk -v NOOFATTRIBUTES="$NOOFATTRIBUTES" -F'n' '!f$1="""$1;f=1"")f==NOOFATTRIBUTES$0=$0""";f=01'

      – chava kumar
      Jan 26 at 14:08











    • Thanks a lot for your quick help.

      – chava kumar
      Jan 26 at 14:09















    1














    You can try with this awk :



    awk -F'n' '!f$1="""$1;f=1"")f==3$0=$0""";f=01' infile





    share|improve this answer























    • Thanks a lot.it is working as expected. But it is not accepting when i tired to parameterized like this awk -F'n' '!f$1="""$1;f=1"")f==$Fieldcnt$0=$0""";f=01'

      – chava kumar
      Jan 26 at 11:46












    • @chavakumar Fieldcnt is not defined. If it's a shell variable : try to pass it to awk with -v.

      – ctac_
      Jan 26 at 12:58











    • I have tried with option -v and it was worked. awk -v NOOFATTRIBUTES="$NOOFATTRIBUTES" -F'n' '!f$1="""$1;f=1"")f==NOOFATTRIBUTES$0=$0""";f=01'

      – chava kumar
      Jan 26 at 14:08











    • Thanks a lot for your quick help.

      – chava kumar
      Jan 26 at 14:09













    1












    1








    1







    You can try with this awk :



    awk -F'n' '!f$1="""$1;f=1"")f==3$0=$0""";f=01' infile





    share|improve this answer













    You can try with this awk :



    awk -F'n' '!f$1="""$1;f=1"")f==3$0=$0""";f=01' infile






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 24 at 13:49









    ctac_ctac_

    1,4141210




    1,4141210












    • Thanks a lot.it is working as expected. But it is not accepting when i tired to parameterized like this awk -F'n' '!f$1="""$1;f=1"")f==$Fieldcnt$0=$0""";f=01'

      – chava kumar
      Jan 26 at 11:46












    • @chavakumar Fieldcnt is not defined. If it's a shell variable : try to pass it to awk with -v.

      – ctac_
      Jan 26 at 12:58











    • I have tried with option -v and it was worked. awk -v NOOFATTRIBUTES="$NOOFATTRIBUTES" -F'n' '!f$1="""$1;f=1"")f==NOOFATTRIBUTES$0=$0""";f=01'

      – chava kumar
      Jan 26 at 14:08











    • Thanks a lot for your quick help.

      – chava kumar
      Jan 26 at 14:09

















    • Thanks a lot.it is working as expected. But it is not accepting when i tired to parameterized like this awk -F'n' '!f$1="""$1;f=1"")f==$Fieldcnt$0=$0""";f=01'

      – chava kumar
      Jan 26 at 11:46












    • @chavakumar Fieldcnt is not defined. If it's a shell variable : try to pass it to awk with -v.

      – ctac_
      Jan 26 at 12:58











    • I have tried with option -v and it was worked. awk -v NOOFATTRIBUTES="$NOOFATTRIBUTES" -F'n' '!f$1="""$1;f=1"")f==NOOFATTRIBUTES$0=$0""";f=01'

      – chava kumar
      Jan 26 at 14:08











    • Thanks a lot for your quick help.

      – chava kumar
      Jan 26 at 14:09
















    Thanks a lot.it is working as expected. But it is not accepting when i tired to parameterized like this awk -F'n' '!f$1="""$1;f=1"")f==$Fieldcnt$0=$0""";f=01'

    – chava kumar
    Jan 26 at 11:46






    Thanks a lot.it is working as expected. But it is not accepting when i tired to parameterized like this awk -F'n' '!f$1="""$1;f=1"")f==$Fieldcnt$0=$0""";f=01'

    – chava kumar
    Jan 26 at 11:46














    @chavakumar Fieldcnt is not defined. If it's a shell variable : try to pass it to awk with -v.

    – ctac_
    Jan 26 at 12:58





    @chavakumar Fieldcnt is not defined. If it's a shell variable : try to pass it to awk with -v.

    – ctac_
    Jan 26 at 12:58













    I have tried with option -v and it was worked. awk -v NOOFATTRIBUTES="$NOOFATTRIBUTES" -F'n' '!f$1="""$1;f=1"")f==NOOFATTRIBUTES$0=$0""";f=01'

    – chava kumar
    Jan 26 at 14:08





    I have tried with option -v and it was worked. awk -v NOOFATTRIBUTES="$NOOFATTRIBUTES" -F'n' '!f$1="""$1;f=1"")f==NOOFATTRIBUTES$0=$0""";f=01'

    – chava kumar
    Jan 26 at 14:08













    Thanks a lot for your quick help.

    – chava kumar
    Jan 26 at 14:09





    Thanks a lot for your quick help.

    – chava kumar
    Jan 26 at 14:09













    1














    You can do it with perl:



    perl -0pe 's/([^|]*)|([^|]*)|([^|n]*)(n|$)/"1"|"2"|"3"4/g' input_file



    • -0 read file at once, not line by line.


    • -p print the line in the end


    • -e the expression


    • s/pattern/replacement/g Replace pattern with replacement





    share|improve this answer





























      1














      You can do it with perl:



      perl -0pe 's/([^|]*)|([^|]*)|([^|n]*)(n|$)/"1"|"2"|"3"4/g' input_file



      • -0 read file at once, not line by line.


      • -p print the line in the end


      • -e the expression


      • s/pattern/replacement/g Replace pattern with replacement





      share|improve this answer



























        1












        1








        1







        You can do it with perl:



        perl -0pe 's/([^|]*)|([^|]*)|([^|n]*)(n|$)/"1"|"2"|"3"4/g' input_file



        • -0 read file at once, not line by line.


        • -p print the line in the end


        • -e the expression


        • s/pattern/replacement/g Replace pattern with replacement





        share|improve this answer















        You can do it with perl:



        perl -0pe 's/([^|]*)|([^|]*)|([^|n]*)(n|$)/"1"|"2"|"3"4/g' input_file



        • -0 read file at once, not line by line.


        • -p print the line in the end


        • -e the expression


        • s/pattern/replacement/g Replace pattern with replacement






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 24 at 13:06

























        answered Jan 24 at 10:12









        RoVoRoVo

        3,141216




        3,141216





















            0














            $ cat input.txt | tr "n" "t" 
            |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i="""$i""";1'
            |tr "t" "n"



            • tr transform new line to a tab. So the input is linearized

            • We tell awk to handle the | as a field seperator. Now we can iterat over each field and add the quotation marks around. Afterwards the whole line is printed


            • tr converts the tab back to a new line





            share|improve this answer

























            • Thank you for help, but this code didnt work in my case.

              – chava kumar
              Jan 26 at 11:59











            • "didn't work" is never a good error description. Could you please describe what doesn't work?

              – finswimmer
              Jan 26 at 12:07











            • Hi, It is not generated any error but there is no change in output. Output is same as input.command used like this. cat filename.txt | tr "n" "t" |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i=""$i"";1' |tr "t" "n"

              – chava kumar
              Jan 26 at 12:33












            • Could you try my edited version?

              – finswimmer
              Jan 28 at 4:58















            0














            $ cat input.txt | tr "n" "t" 
            |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i="""$i""";1'
            |tr "t" "n"



            • tr transform new line to a tab. So the input is linearized

            • We tell awk to handle the | as a field seperator. Now we can iterat over each field and add the quotation marks around. Afterwards the whole line is printed


            • tr converts the tab back to a new line





            share|improve this answer

























            • Thank you for help, but this code didnt work in my case.

              – chava kumar
              Jan 26 at 11:59











            • "didn't work" is never a good error description. Could you please describe what doesn't work?

              – finswimmer
              Jan 26 at 12:07











            • Hi, It is not generated any error but there is no change in output. Output is same as input.command used like this. cat filename.txt | tr "n" "t" |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i=""$i"";1' |tr "t" "n"

              – chava kumar
              Jan 26 at 12:33












            • Could you try my edited version?

              – finswimmer
              Jan 28 at 4:58













            0












            0








            0







            $ cat input.txt | tr "n" "t" 
            |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i="""$i""";1'
            |tr "t" "n"



            • tr transform new line to a tab. So the input is linearized

            • We tell awk to handle the | as a field seperator. Now we can iterat over each field and add the quotation marks around. Afterwards the whole line is printed


            • tr converts the tab back to a new line





            share|improve this answer















            $ cat input.txt | tr "n" "t" 
            |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i="""$i""";1'
            |tr "t" "n"



            • tr transform new line to a tab. So the input is linearized

            • We tell awk to handle the | as a field seperator. Now we can iterat over each field and add the quotation marks around. Afterwards the whole line is printed


            • tr converts the tab back to a new line






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 28 at 4:56

























            answered Jan 24 at 13:30









            finswimmerfinswimmer

            52416




            52416












            • Thank you for help, but this code didnt work in my case.

              – chava kumar
              Jan 26 at 11:59











            • "didn't work" is never a good error description. Could you please describe what doesn't work?

              – finswimmer
              Jan 26 at 12:07











            • Hi, It is not generated any error but there is no change in output. Output is same as input.command used like this. cat filename.txt | tr "n" "t" |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i=""$i"";1' |tr "t" "n"

              – chava kumar
              Jan 26 at 12:33












            • Could you try my edited version?

              – finswimmer
              Jan 28 at 4:58

















            • Thank you for help, but this code didnt work in my case.

              – chava kumar
              Jan 26 at 11:59











            • "didn't work" is never a good error description. Could you please describe what doesn't work?

              – finswimmer
              Jan 26 at 12:07











            • Hi, It is not generated any error but there is no change in output. Output is same as input.command used like this. cat filename.txt | tr "n" "t" |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i=""$i"";1' |tr "t" "n"

              – chava kumar
              Jan 26 at 12:33












            • Could you try my edited version?

              – finswimmer
              Jan 28 at 4:58
















            Thank you for help, but this code didnt work in my case.

            – chava kumar
            Jan 26 at 11:59





            Thank you for help, but this code didnt work in my case.

            – chava kumar
            Jan 26 at 11:59













            "didn't work" is never a good error description. Could you please describe what doesn't work?

            – finswimmer
            Jan 26 at 12:07





            "didn't work" is never a good error description. Could you please describe what doesn't work?

            – finswimmer
            Jan 26 at 12:07













            Hi, It is not generated any error but there is no change in output. Output is same as input.command used like this. cat filename.txt | tr "n" "t" |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i=""$i"";1' |tr "t" "n"

            – chava kumar
            Jan 26 at 12:33






            Hi, It is not generated any error but there is no change in output. Output is same as input.command used like this. cat filename.txt | tr "n" "t" |awk -v FS="|" -v OFS="|" 'for(i=1;i<=NF;i++) $i=""$i"";1' |tr "t" "n"

            – chava kumar
            Jan 26 at 12:33














            Could you try my edited version?

            – finswimmer
            Jan 28 at 4:58





            Could you try my edited version?

            – finswimmer
            Jan 28 at 4:58

















            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%2f496417%2fhow-to-add-quotes-to-line-break-field-values-in-a-file%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 check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay