How can I direct the output of awk to multiple individual files in a specified directory

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











up vote
0
down vote

favorite












I'm having trouble directing the output of awk into individual files in a specified directory. I can successfully do this in the working directory as well as chaining a mv command to the end, but I would like to be able to use only awk to save the files to the specified directory.



For reference, here is a simplified messages.txt that I want to split into individual message files:



messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2

messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2

messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2


I was able to successfully split the messages file into individual messages and output each to a separate file in the working directory using:



awk -v RS= 'print > ("message" NR ".txt")'


Which creates message1.txt, message2.txt, and message3.txt in the working directory. I can also chain a mv command to the end of this basic command like so to move the output into a specified directory:



out = "$(pwd)/messages"
mkdir -p $out
awk -v RS= 'print > ("message" NR ".txt")' && mv message*.txt $out


Which creates message1.txt, message2.txt, and message3.txt in ./messages. However, I haven't been able to figure how to save the separate message files to a specified directory only using awk.



For example, I attempted to split the message file and output individual messages to a specified output directory as follows:



out="$(pwd)/messages"
mkdir -p $out
awk -v RS= 'print > ($out "message" NR ".txt")' messages.txt


Which results in the following error:



awk: cmd. line:1: (FILENAME=- FNR=1) fatal: can't redirect to 'messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2message1.txt' (file name too long)


I found the following related question "Split files using awk and generate the results in another directory" but still wasn't able to get the correct syntax.



out="$(pwd)/messages"
mkdir -p $out
awk -v RS= path=$out 'f=path "message" NR ".txt"; print > f' messages.txt

awk: fatal: cannot open file `f=path "message" NR ".txt"; print > f' for reading (No such file or directory)









share|improve this question









New contributor




cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    You're just missing a -v I think ... awk -v RS= -v path="$out"
    – steeldriver
    Nov 19 at 3:27










  • Did you really type out = "$(pwd)/messages"? Did it give you an error message?
    – Scott
    Nov 19 at 4:09










  • @steeldriver Thanks, that was it, I was missing the -v before path=$out.
    – cneiderer
    Nov 19 at 4:52










  • @Scott Sorry, that was error in writing the question. I didn't initially have the out defined in that code block and made and error when I was trying to add that in for completeness.
    – cneiderer
    Nov 19 at 4:55














up vote
0
down vote

favorite












I'm having trouble directing the output of awk into individual files in a specified directory. I can successfully do this in the working directory as well as chaining a mv command to the end, but I would like to be able to use only awk to save the files to the specified directory.



For reference, here is a simplified messages.txt that I want to split into individual message files:



messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2

messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2

messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2


I was able to successfully split the messages file into individual messages and output each to a separate file in the working directory using:



awk -v RS= 'print > ("message" NR ".txt")'


Which creates message1.txt, message2.txt, and message3.txt in the working directory. I can also chain a mv command to the end of this basic command like so to move the output into a specified directory:



out = "$(pwd)/messages"
mkdir -p $out
awk -v RS= 'print > ("message" NR ".txt")' && mv message*.txt $out


Which creates message1.txt, message2.txt, and message3.txt in ./messages. However, I haven't been able to figure how to save the separate message files to a specified directory only using awk.



For example, I attempted to split the message file and output individual messages to a specified output directory as follows:



out="$(pwd)/messages"
mkdir -p $out
awk -v RS= 'print > ($out "message" NR ".txt")' messages.txt


Which results in the following error:



awk: cmd. line:1: (FILENAME=- FNR=1) fatal: can't redirect to 'messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2message1.txt' (file name too long)


I found the following related question "Split files using awk and generate the results in another directory" but still wasn't able to get the correct syntax.



out="$(pwd)/messages"
mkdir -p $out
awk -v RS= path=$out 'f=path "message" NR ".txt"; print > f' messages.txt

awk: fatal: cannot open file `f=path "message" NR ".txt"; print > f' for reading (No such file or directory)









share|improve this question









New contributor




cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    You're just missing a -v I think ... awk -v RS= -v path="$out"
    – steeldriver
    Nov 19 at 3:27










  • Did you really type out = "$(pwd)/messages"? Did it give you an error message?
    – Scott
    Nov 19 at 4:09










  • @steeldriver Thanks, that was it, I was missing the -v before path=$out.
    – cneiderer
    Nov 19 at 4:52










  • @Scott Sorry, that was error in writing the question. I didn't initially have the out defined in that code block and made and error when I was trying to add that in for completeness.
    – cneiderer
    Nov 19 at 4:55












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm having trouble directing the output of awk into individual files in a specified directory. I can successfully do this in the working directory as well as chaining a mv command to the end, but I would like to be able to use only awk to save the files to the specified directory.



For reference, here is a simplified messages.txt that I want to split into individual message files:



messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2

messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2

messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2


I was able to successfully split the messages file into individual messages and output each to a separate file in the working directory using:



awk -v RS= 'print > ("message" NR ".txt")'


Which creates message1.txt, message2.txt, and message3.txt in the working directory. I can also chain a mv command to the end of this basic command like so to move the output into a specified directory:



out = "$(pwd)/messages"
mkdir -p $out
awk -v RS= 'print > ("message" NR ".txt")' && mv message*.txt $out


Which creates message1.txt, message2.txt, and message3.txt in ./messages. However, I haven't been able to figure how to save the separate message files to a specified directory only using awk.



For example, I attempted to split the message file and output individual messages to a specified output directory as follows:



out="$(pwd)/messages"
mkdir -p $out
awk -v RS= 'print > ($out "message" NR ".txt")' messages.txt


Which results in the following error:



awk: cmd. line:1: (FILENAME=- FNR=1) fatal: can't redirect to 'messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2message1.txt' (file name too long)


I found the following related question "Split files using awk and generate the results in another directory" but still wasn't able to get the correct syntax.



out="$(pwd)/messages"
mkdir -p $out
awk -v RS= path=$out 'f=path "message" NR ".txt"; print > f' messages.txt

awk: fatal: cannot open file `f=path "message" NR ".txt"; print > f' for reading (No such file or directory)









share|improve this question









New contributor




cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm having trouble directing the output of awk into individual files in a specified directory. I can successfully do this in the working directory as well as chaining a mv command to the end, but I would like to be able to use only awk to save the files to the specified directory.



For reference, here is a simplified messages.txt that I want to split into individual message files:



messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2

messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2

messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2


I was able to successfully split the messages file into individual messages and output each to a separate file in the working directory using:



awk -v RS= 'print > ("message" NR ".txt")'


Which creates message1.txt, message2.txt, and message3.txt in the working directory. I can also chain a mv command to the end of this basic command like so to move the output into a specified directory:



out = "$(pwd)/messages"
mkdir -p $out
awk -v RS= 'print > ("message" NR ".txt")' && mv message*.txt $out


Which creates message1.txt, message2.txt, and message3.txt in ./messages. However, I haven't been able to figure how to save the separate message files to a specified directory only using awk.



For example, I attempted to split the message file and output individual messages to a specified output directory as follows:



out="$(pwd)/messages"
mkdir -p $out
awk -v RS= 'print > ($out "message" NR ".txt")' messages.txt


Which results in the following error:



awk: cmd. line:1: (FILENAME=- FNR=1) fatal: can't redirect to 'messageHeader:
mh_field1: mh_val1
mh_field2: mh_val2
messageData:
md_field1: md_val1
md_field2: md_val2message1.txt' (file name too long)


I found the following related question "Split files using awk and generate the results in another directory" but still wasn't able to get the correct syntax.



out="$(pwd)/messages"
mkdir -p $out
awk -v RS= path=$out 'f=path "message" NR ".txt"; print > f' messages.txt

awk: fatal: cannot open file `f=path "message" NR ".txt"; print > f' for reading (No such file or directory)






shell awk






share|improve this question









New contributor




cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









Rui F Ribeiro

38.2k1475123




38.2k1475123






New contributor




cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 19 at 3:08









cneiderer

11




11




New contributor




cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1




    You're just missing a -v I think ... awk -v RS= -v path="$out"
    – steeldriver
    Nov 19 at 3:27










  • Did you really type out = "$(pwd)/messages"? Did it give you an error message?
    – Scott
    Nov 19 at 4:09










  • @steeldriver Thanks, that was it, I was missing the -v before path=$out.
    – cneiderer
    Nov 19 at 4:52










  • @Scott Sorry, that was error in writing the question. I didn't initially have the out defined in that code block and made and error when I was trying to add that in for completeness.
    – cneiderer
    Nov 19 at 4:55












  • 1




    You're just missing a -v I think ... awk -v RS= -v path="$out"
    – steeldriver
    Nov 19 at 3:27










  • Did you really type out = "$(pwd)/messages"? Did it give you an error message?
    – Scott
    Nov 19 at 4:09










  • @steeldriver Thanks, that was it, I was missing the -v before path=$out.
    – cneiderer
    Nov 19 at 4:52










  • @Scott Sorry, that was error in writing the question. I didn't initially have the out defined in that code block and made and error when I was trying to add that in for completeness.
    – cneiderer
    Nov 19 at 4:55







1




1




You're just missing a -v I think ... awk -v RS= -v path="$out"
– steeldriver
Nov 19 at 3:27




You're just missing a -v I think ... awk -v RS= -v path="$out"
– steeldriver
Nov 19 at 3:27












Did you really type out = "$(pwd)/messages"? Did it give you an error message?
– Scott
Nov 19 at 4:09




Did you really type out = "$(pwd)/messages"? Did it give you an error message?
– Scott
Nov 19 at 4:09












@steeldriver Thanks, that was it, I was missing the -v before path=$out.
– cneiderer
Nov 19 at 4:52




@steeldriver Thanks, that was it, I was missing the -v before path=$out.
– cneiderer
Nov 19 at 4:52












@Scott Sorry, that was error in writing the question. I didn't initially have the out defined in that code block and made and error when I was trying to add that in for completeness.
– cneiderer
Nov 19 at 4:55




@Scott Sorry, that was error in writing the question. I didn't initially have the out defined in that code block and made and error when I was trying to add that in for completeness.
– cneiderer
Nov 19 at 4:55










1 Answer
1






active

oldest

votes

















up vote
0
down vote













Per @steeldriver's comment, there was a missing -v, making the correct syntax:



out="$(pwd)/messages"
mkdir -p $out
awk -v RS= -v path="$out" 'f=path "message" NR ".txt"; print > f' messages.txt





share|improve this answer








New contributor




cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

















    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',
    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
    );



    );






    cneiderer is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482655%2fhow-can-i-direct-the-output-of-awk-to-multiple-individual-files-in-a-specified-d%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








    up vote
    0
    down vote













    Per @steeldriver's comment, there was a missing -v, making the correct syntax:



    out="$(pwd)/messages"
    mkdir -p $out
    awk -v RS= -v path="$out" 'f=path "message" NR ".txt"; print > f' messages.txt





    share|improve this answer








    New contributor




    cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      0
      down vote













      Per @steeldriver's comment, there was a missing -v, making the correct syntax:



      out="$(pwd)/messages"
      mkdir -p $out
      awk -v RS= -v path="$out" 'f=path "message" NR ".txt"; print > f' messages.txt





      share|improve this answer








      New contributor




      cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



















        up vote
        0
        down vote










        up vote
        0
        down vote









        Per @steeldriver's comment, there was a missing -v, making the correct syntax:



        out="$(pwd)/messages"
        mkdir -p $out
        awk -v RS= -v path="$out" 'f=path "message" NR ".txt"; print > f' messages.txt





        share|improve this answer








        New contributor




        cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        Per @steeldriver's comment, there was a missing -v, making the correct syntax:



        out="$(pwd)/messages"
        mkdir -p $out
        awk -v RS= -v path="$out" 'f=path "message" NR ".txt"; print > f' messages.txt






        share|improve this answer








        New contributor




        cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered Nov 19 at 12:28









        cneiderer

        11




        11




        New contributor




        cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        cneiderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.




















            cneiderer is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            cneiderer is a new contributor. Be nice, and check out our Code of Conduct.












            cneiderer is a new contributor. Be nice, and check out our Code of Conduct.











            cneiderer is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482655%2fhow-can-i-direct-the-output-of-awk-to-multiple-individual-files-in-a-specified-d%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