Creating and exporting/importing variable with awk?

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











up vote
0
down vote

favorite












I have a file where every line is a new customers information, such as



Pop corn, pop@gmail.com, 50, 200


I am currently trying to see if it is possible to use a awk/gawk script to assign each column to a variable and then pipe those variables to sed to be substituted into a template? I would be running both awk/gawk and sed from within a bash script. What is a good implementation I could follow to get this done? I'm trying not to use grep, because I'm trying to learn how to cohesively use awk/gawk, sed, and bash scripts together or perl since I haven't learned it yet.










share|improve this question























  • Do you want to assign each column to a shell variable or to an awk variable?
    – Satō Katsura
    Oct 2 '17 at 3:39











  • I thought I would go ahead at first at use awk variables if it was possible (which I now know is), if it were not then I would have tried using variables declared with in the bash script and exporting them to awk.
    – below_avg_st
    Oct 2 '17 at 3:43










  • Depending on the specifics, it may be that you don't need sed, or that you don't need the shell variable to do your templating, or even awk. Since you only give minimal info about what it is you actually want to do ("substitute (something) into a template"), it's difficult to give a "good implementation" for anything other than the parsing of the file that you talk about at the start.
    – Kusalananda
    Oct 2 '17 at 5:25














up vote
0
down vote

favorite












I have a file where every line is a new customers information, such as



Pop corn, pop@gmail.com, 50, 200


I am currently trying to see if it is possible to use a awk/gawk script to assign each column to a variable and then pipe those variables to sed to be substituted into a template? I would be running both awk/gawk and sed from within a bash script. What is a good implementation I could follow to get this done? I'm trying not to use grep, because I'm trying to learn how to cohesively use awk/gawk, sed, and bash scripts together or perl since I haven't learned it yet.










share|improve this question























  • Do you want to assign each column to a shell variable or to an awk variable?
    – Satō Katsura
    Oct 2 '17 at 3:39











  • I thought I would go ahead at first at use awk variables if it was possible (which I now know is), if it were not then I would have tried using variables declared with in the bash script and exporting them to awk.
    – below_avg_st
    Oct 2 '17 at 3:43










  • Depending on the specifics, it may be that you don't need sed, or that you don't need the shell variable to do your templating, or even awk. Since you only give minimal info about what it is you actually want to do ("substitute (something) into a template"), it's difficult to give a "good implementation" for anything other than the parsing of the file that you talk about at the start.
    – Kusalananda
    Oct 2 '17 at 5:25












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a file where every line is a new customers information, such as



Pop corn, pop@gmail.com, 50, 200


I am currently trying to see if it is possible to use a awk/gawk script to assign each column to a variable and then pipe those variables to sed to be substituted into a template? I would be running both awk/gawk and sed from within a bash script. What is a good implementation I could follow to get this done? I'm trying not to use grep, because I'm trying to learn how to cohesively use awk/gawk, sed, and bash scripts together or perl since I haven't learned it yet.










share|improve this question















I have a file where every line is a new customers information, such as



Pop corn, pop@gmail.com, 50, 200


I am currently trying to see if it is possible to use a awk/gawk script to assign each column to a variable and then pipe those variables to sed to be substituted into a template? I would be running both awk/gawk and sed from within a bash script. What is a good implementation I could follow to get this done? I'm trying not to use grep, because I'm trying to learn how to cohesively use awk/gawk, sed, and bash scripts together or perl since I haven't learned it yet.







shell-script awk sed gawk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 2 '17 at 5:17









αғsнιη

15.7k92563




15.7k92563










asked Oct 2 '17 at 3:31









below_avg_st

84




84











  • Do you want to assign each column to a shell variable or to an awk variable?
    – Satō Katsura
    Oct 2 '17 at 3:39











  • I thought I would go ahead at first at use awk variables if it was possible (which I now know is), if it were not then I would have tried using variables declared with in the bash script and exporting them to awk.
    – below_avg_st
    Oct 2 '17 at 3:43










  • Depending on the specifics, it may be that you don't need sed, or that you don't need the shell variable to do your templating, or even awk. Since you only give minimal info about what it is you actually want to do ("substitute (something) into a template"), it's difficult to give a "good implementation" for anything other than the parsing of the file that you talk about at the start.
    – Kusalananda
    Oct 2 '17 at 5:25
















  • Do you want to assign each column to a shell variable or to an awk variable?
    – Satō Katsura
    Oct 2 '17 at 3:39











  • I thought I would go ahead at first at use awk variables if it was possible (which I now know is), if it were not then I would have tried using variables declared with in the bash script and exporting them to awk.
    – below_avg_st
    Oct 2 '17 at 3:43










  • Depending on the specifics, it may be that you don't need sed, or that you don't need the shell variable to do your templating, or even awk. Since you only give minimal info about what it is you actually want to do ("substitute (something) into a template"), it's difficult to give a "good implementation" for anything other than the parsing of the file that you talk about at the start.
    – Kusalananda
    Oct 2 '17 at 5:25















Do you want to assign each column to a shell variable or to an awk variable?
– Satō Katsura
Oct 2 '17 at 3:39





Do you want to assign each column to a shell variable or to an awk variable?
– Satō Katsura
Oct 2 '17 at 3:39













I thought I would go ahead at first at use awk variables if it was possible (which I now know is), if it were not then I would have tried using variables declared with in the bash script and exporting them to awk.
– below_avg_st
Oct 2 '17 at 3:43




I thought I would go ahead at first at use awk variables if it was possible (which I now know is), if it were not then I would have tried using variables declared with in the bash script and exporting them to awk.
– below_avg_st
Oct 2 '17 at 3:43












Depending on the specifics, it may be that you don't need sed, or that you don't need the shell variable to do your templating, or even awk. Since you only give minimal info about what it is you actually want to do ("substitute (something) into a template"), it's difficult to give a "good implementation" for anything other than the parsing of the file that you talk about at the start.
– Kusalananda
Oct 2 '17 at 5:25




Depending on the specifics, it may be that you don't need sed, or that you don't need the shell variable to do your templating, or even awk. Since you only give minimal info about what it is you actually want to do ("substitute (something) into a template"), it's difficult to give a "good implementation" for anything other than the parsing of the file that you talk about at the start.
– Kusalananda
Oct 2 '17 at 5:25










2 Answers
2






active

oldest

votes

















up vote
2
down vote













It seems to me you should be better off doing something like:



while IFS=, read -r name email x y _ignored_; do
email=$email# x=$x# y=$y#
sendmail -oem -oi -t << EOF
To: $name <$email>
Subject: Whatever

x=$x y=$y
EOF
done < file.csv


That, is since you're going to run at least one command per line anyway, use the shell to read the lines into variables. And use shell variable expansion in the here-document instead of sed to do the template expansion.



awk wouldn't help much here as while it can run commands with its system() or getline() (but via running a shell to parse a command line), it cannot set environment variables for those, other than building the shell code that sets those variables. So, it would become quite convoluted and less efficient. Something like:



CODE='sendmail -oem -oi -t << EOF
to: $name <$email>
Subject: Whatever

x=$x y=$y
EOF' awk -F ', *' -v q="'" '
function shquote(s)
gsub(q, q "\" q q, s)
return q s q

system("name="shquote($1)" email="shquote($2)" x="shquote($3)
" y="shquote($4)"n"ENVIRON["CODE"])' < file.csv





share|improve this answer





























    up vote
    0
    down vote













    Use split function of awk to read the line into the array with specific delimiter.



    awk 'split($0, array, /,/)' customers_file


    Then you can pass to sed what elements of this array you want to feed it with just printing those elements within awk.






    share|improve this answer






















    • is this pearl? or is split also an implementation in bash/awk?
      – below_avg_st
      Oct 2 '17 at 4:33










    • Hi, this is awk and split is its built in function.
      – Î±Ò“sнιη
      Oct 2 '17 at 4:38











    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: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f395551%2fcreating-and-exporting-importing-variable-with-awk%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    It seems to me you should be better off doing something like:



    while IFS=, read -r name email x y _ignored_; do
    email=$email# x=$x# y=$y#
    sendmail -oem -oi -t << EOF
    To: $name <$email>
    Subject: Whatever

    x=$x y=$y
    EOF
    done < file.csv


    That, is since you're going to run at least one command per line anyway, use the shell to read the lines into variables. And use shell variable expansion in the here-document instead of sed to do the template expansion.



    awk wouldn't help much here as while it can run commands with its system() or getline() (but via running a shell to parse a command line), it cannot set environment variables for those, other than building the shell code that sets those variables. So, it would become quite convoluted and less efficient. Something like:



    CODE='sendmail -oem -oi -t << EOF
    to: $name <$email>
    Subject: Whatever

    x=$x y=$y
    EOF' awk -F ', *' -v q="'" '
    function shquote(s)
    gsub(q, q "\" q q, s)
    return q s q

    system("name="shquote($1)" email="shquote($2)" x="shquote($3)
    " y="shquote($4)"n"ENVIRON["CODE"])' < file.csv





    share|improve this answer


























      up vote
      2
      down vote













      It seems to me you should be better off doing something like:



      while IFS=, read -r name email x y _ignored_; do
      email=$email# x=$x# y=$y#
      sendmail -oem -oi -t << EOF
      To: $name <$email>
      Subject: Whatever

      x=$x y=$y
      EOF
      done < file.csv


      That, is since you're going to run at least one command per line anyway, use the shell to read the lines into variables. And use shell variable expansion in the here-document instead of sed to do the template expansion.



      awk wouldn't help much here as while it can run commands with its system() or getline() (but via running a shell to parse a command line), it cannot set environment variables for those, other than building the shell code that sets those variables. So, it would become quite convoluted and less efficient. Something like:



      CODE='sendmail -oem -oi -t << EOF
      to: $name <$email>
      Subject: Whatever

      x=$x y=$y
      EOF' awk -F ', *' -v q="'" '
      function shquote(s)
      gsub(q, q "\" q q, s)
      return q s q

      system("name="shquote($1)" email="shquote($2)" x="shquote($3)
      " y="shquote($4)"n"ENVIRON["CODE"])' < file.csv





      share|improve this answer
























        up vote
        2
        down vote










        up vote
        2
        down vote









        It seems to me you should be better off doing something like:



        while IFS=, read -r name email x y _ignored_; do
        email=$email# x=$x# y=$y#
        sendmail -oem -oi -t << EOF
        To: $name <$email>
        Subject: Whatever

        x=$x y=$y
        EOF
        done < file.csv


        That, is since you're going to run at least one command per line anyway, use the shell to read the lines into variables. And use shell variable expansion in the here-document instead of sed to do the template expansion.



        awk wouldn't help much here as while it can run commands with its system() or getline() (but via running a shell to parse a command line), it cannot set environment variables for those, other than building the shell code that sets those variables. So, it would become quite convoluted and less efficient. Something like:



        CODE='sendmail -oem -oi -t << EOF
        to: $name <$email>
        Subject: Whatever

        x=$x y=$y
        EOF' awk -F ', *' -v q="'" '
        function shquote(s)
        gsub(q, q "\" q q, s)
        return q s q

        system("name="shquote($1)" email="shquote($2)" x="shquote($3)
        " y="shquote($4)"n"ENVIRON["CODE"])' < file.csv





        share|improve this answer














        It seems to me you should be better off doing something like:



        while IFS=, read -r name email x y _ignored_; do
        email=$email# x=$x# y=$y#
        sendmail -oem -oi -t << EOF
        To: $name <$email>
        Subject: Whatever

        x=$x y=$y
        EOF
        done < file.csv


        That, is since you're going to run at least one command per line anyway, use the shell to read the lines into variables. And use shell variable expansion in the here-document instead of sed to do the template expansion.



        awk wouldn't help much here as while it can run commands with its system() or getline() (but via running a shell to parse a command line), it cannot set environment variables for those, other than building the shell code that sets those variables. So, it would become quite convoluted and less efficient. Something like:



        CODE='sendmail -oem -oi -t << EOF
        to: $name <$email>
        Subject: Whatever

        x=$x y=$y
        EOF' awk -F ', *' -v q="'" '
        function shquote(s)
        gsub(q, q "\" q q, s)
        return q s q

        system("name="shquote($1)" email="shquote($2)" x="shquote($3)
        " y="shquote($4)"n"ENVIRON["CODE"])' < file.csv






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 2 '17 at 9:40

























        answered Oct 2 '17 at 9:02









        Stéphane Chazelas

        283k53522859




        283k53522859






















            up vote
            0
            down vote













            Use split function of awk to read the line into the array with specific delimiter.



            awk 'split($0, array, /,/)' customers_file


            Then you can pass to sed what elements of this array you want to feed it with just printing those elements within awk.






            share|improve this answer






















            • is this pearl? or is split also an implementation in bash/awk?
              – below_avg_st
              Oct 2 '17 at 4:33










            • Hi, this is awk and split is its built in function.
              – Î±Ò“sнιη
              Oct 2 '17 at 4:38















            up vote
            0
            down vote













            Use split function of awk to read the line into the array with specific delimiter.



            awk 'split($0, array, /,/)' customers_file


            Then you can pass to sed what elements of this array you want to feed it with just printing those elements within awk.






            share|improve this answer






















            • is this pearl? or is split also an implementation in bash/awk?
              – below_avg_st
              Oct 2 '17 at 4:33










            • Hi, this is awk and split is its built in function.
              – Î±Ò“sнιη
              Oct 2 '17 at 4:38













            up vote
            0
            down vote










            up vote
            0
            down vote









            Use split function of awk to read the line into the array with specific delimiter.



            awk 'split($0, array, /,/)' customers_file


            Then you can pass to sed what elements of this array you want to feed it with just printing those elements within awk.






            share|improve this answer














            Use split function of awk to read the line into the array with specific delimiter.



            awk 'split($0, array, /,/)' customers_file


            Then you can pass to sed what elements of this array you want to feed it with just printing those elements within awk.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 2 '17 at 7:17

























            answered Oct 2 '17 at 3:36









            αғsнιη

            15.7k92563




            15.7k92563











            • is this pearl? or is split also an implementation in bash/awk?
              – below_avg_st
              Oct 2 '17 at 4:33










            • Hi, this is awk and split is its built in function.
              – Î±Ò“sнιη
              Oct 2 '17 at 4:38

















            • is this pearl? or is split also an implementation in bash/awk?
              – below_avg_st
              Oct 2 '17 at 4:33










            • Hi, this is awk and split is its built in function.
              – Î±Ò“sнιη
              Oct 2 '17 at 4:38
















            is this pearl? or is split also an implementation in bash/awk?
            – below_avg_st
            Oct 2 '17 at 4:33




            is this pearl? or is split also an implementation in bash/awk?
            – below_avg_st
            Oct 2 '17 at 4:33












            Hi, this is awk and split is its built in function.
            – Î±Ò“sнιη
            Oct 2 '17 at 4:38





            Hi, this is awk and split is its built in function.
            – Î±Ò“sнιη
            Oct 2 '17 at 4:38


















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f395551%2fcreating-and-exporting-importing-variable-with-awk%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?