Integrate awk one-liner into awk script?











up vote
0
down vote

favorite












If I have this one-liner



$ echo 0123456789ab | awk '{gsub(/..B/,"&:")}1'
01:23:45:67:89:ab


which I would like to use in an awk script, and therefore tries



cat $hosts | awk '{
print "host "$5" {"
print " option host-name ""$5"";"
print " hardware ethernet "$3";"
x = {gsub(/..B/,"&:")}1
print " fixed-address "print x";"
print "}"
print ""
}' > /etc/dhcp/reservations.conf


but it fails with



awk: cmd. line:5:     x = {gsub(/..B/,"&:")}1
awk: cmd. line:5: ^ syntax error
awk: cmd. line:6: print " fixed-address "print x";"
awk: cmd. line:6: ^ syntax error


Question



Does anyone know how to integrate such one-liners into an awk script?










share|improve this question
























  • What is your input file?
    – Inian
    Nov 12 at 10:48










  • You should remove the { around the gsub command as this is not a one-liner anymore (and maybe remove the 1 entirely)
    – Aserre
    Nov 12 at 10:49












  • @Sandra Schlichting, you need NOT to use cat with awk, awk could read files by itself too.
    – RavinderSingh13
    Nov 12 at 11:00










  • Show us the input file you are working with? What is the value that you want to gsub() on? What is the expected output
    – Inian
    Nov 12 at 11:11















up vote
0
down vote

favorite












If I have this one-liner



$ echo 0123456789ab | awk '{gsub(/..B/,"&:")}1'
01:23:45:67:89:ab


which I would like to use in an awk script, and therefore tries



cat $hosts | awk '{
print "host "$5" {"
print " option host-name ""$5"";"
print " hardware ethernet "$3";"
x = {gsub(/..B/,"&:")}1
print " fixed-address "print x";"
print "}"
print ""
}' > /etc/dhcp/reservations.conf


but it fails with



awk: cmd. line:5:     x = {gsub(/..B/,"&:")}1
awk: cmd. line:5: ^ syntax error
awk: cmd. line:6: print " fixed-address "print x";"
awk: cmd. line:6: ^ syntax error


Question



Does anyone know how to integrate such one-liners into an awk script?










share|improve this question
























  • What is your input file?
    – Inian
    Nov 12 at 10:48










  • You should remove the { around the gsub command as this is not a one-liner anymore (and maybe remove the 1 entirely)
    – Aserre
    Nov 12 at 10:49












  • @Sandra Schlichting, you need NOT to use cat with awk, awk could read files by itself too.
    – RavinderSingh13
    Nov 12 at 11:00










  • Show us the input file you are working with? What is the value that you want to gsub() on? What is the expected output
    – Inian
    Nov 12 at 11:11













up vote
0
down vote

favorite









up vote
0
down vote

favorite











If I have this one-liner



$ echo 0123456789ab | awk '{gsub(/..B/,"&:")}1'
01:23:45:67:89:ab


which I would like to use in an awk script, and therefore tries



cat $hosts | awk '{
print "host "$5" {"
print " option host-name ""$5"";"
print " hardware ethernet "$3";"
x = {gsub(/..B/,"&:")}1
print " fixed-address "print x";"
print "}"
print ""
}' > /etc/dhcp/reservations.conf


but it fails with



awk: cmd. line:5:     x = {gsub(/..B/,"&:")}1
awk: cmd. line:5: ^ syntax error
awk: cmd. line:6: print " fixed-address "print x";"
awk: cmd. line:6: ^ syntax error


Question



Does anyone know how to integrate such one-liners into an awk script?










share|improve this question















If I have this one-liner



$ echo 0123456789ab | awk '{gsub(/..B/,"&:")}1'
01:23:45:67:89:ab


which I would like to use in an awk script, and therefore tries



cat $hosts | awk '{
print "host "$5" {"
print " option host-name ""$5"";"
print " hardware ethernet "$3";"
x = {gsub(/..B/,"&:")}1
print " fixed-address "print x";"
print "}"
print ""
}' > /etc/dhcp/reservations.conf


but it fails with



awk: cmd. line:5:     x = {gsub(/..B/,"&:")}1
awk: cmd. line:5: ^ syntax error
awk: cmd. line:6: print " fixed-address "print x";"
awk: cmd. line:6: ^ syntax error


Question



Does anyone know how to integrate such one-liners into an awk script?







linux awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 11:10









Inian

38.4k63669




38.4k63669










asked Nov 12 at 10:43









Sandra Schlichting

9,0652677125




9,0652677125












  • What is your input file?
    – Inian
    Nov 12 at 10:48










  • You should remove the { around the gsub command as this is not a one-liner anymore (and maybe remove the 1 entirely)
    – Aserre
    Nov 12 at 10:49












  • @Sandra Schlichting, you need NOT to use cat with awk, awk could read files by itself too.
    – RavinderSingh13
    Nov 12 at 11:00










  • Show us the input file you are working with? What is the value that you want to gsub() on? What is the expected output
    – Inian
    Nov 12 at 11:11


















  • What is your input file?
    – Inian
    Nov 12 at 10:48










  • You should remove the { around the gsub command as this is not a one-liner anymore (and maybe remove the 1 entirely)
    – Aserre
    Nov 12 at 10:49












  • @Sandra Schlichting, you need NOT to use cat with awk, awk could read files by itself too.
    – RavinderSingh13
    Nov 12 at 11:00










  • Show us the input file you are working with? What is the value that you want to gsub() on? What is the expected output
    – Inian
    Nov 12 at 11:11
















What is your input file?
– Inian
Nov 12 at 10:48




What is your input file?
– Inian
Nov 12 at 10:48












You should remove the { around the gsub command as this is not a one-liner anymore (and maybe remove the 1 entirely)
– Aserre
Nov 12 at 10:49






You should remove the { around the gsub command as this is not a one-liner anymore (and maybe remove the 1 entirely)
– Aserre
Nov 12 at 10:49














@Sandra Schlichting, you need NOT to use cat with awk, awk could read files by itself too.
– RavinderSingh13
Nov 12 at 11:00




@Sandra Schlichting, you need NOT to use cat with awk, awk could read files by itself too.
– RavinderSingh13
Nov 12 at 11:00












Show us the input file you are working with? What is the value that you want to gsub() on? What is the expected output
– Inian
Nov 12 at 11:11




Show us the input file you are working with? What is the value that you want to gsub() on? What is the expected output
– Inian
Nov 12 at 11:11












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You can't save { in to a variable since it denotes some action. Moreover in case you save output of sub or gsub it always gives you the count of substitutes it made to line/variable. so better try to change that code line to following.



Change your 2 lines:



x = {gsub(/..B/,"&:")}1
print " fixed-address "print x";"


To:



new_line=$0
gsub(/..B/,"&:",new_line)
print " fixed-address "new_line";"


By doing above your actual line's value will NEVER change and you could use it doing other stuff later point of time in your code too. I haven't tested above since samples were not given but it should work.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53260470%2fintegrate-awk-one-liner-into-awk-script%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
    1
    down vote



    accepted










    You can't save { in to a variable since it denotes some action. Moreover in case you save output of sub or gsub it always gives you the count of substitutes it made to line/variable. so better try to change that code line to following.



    Change your 2 lines:



    x = {gsub(/..B/,"&:")}1
    print " fixed-address "print x";"


    To:



    new_line=$0
    gsub(/..B/,"&:",new_line)
    print " fixed-address "new_line";"


    By doing above your actual line's value will NEVER change and you could use it doing other stuff later point of time in your code too. I haven't tested above since samples were not given but it should work.






    share|improve this answer



























      up vote
      1
      down vote



      accepted










      You can't save { in to a variable since it denotes some action. Moreover in case you save output of sub or gsub it always gives you the count of substitutes it made to line/variable. so better try to change that code line to following.



      Change your 2 lines:



      x = {gsub(/..B/,"&:")}1
      print " fixed-address "print x";"


      To:



      new_line=$0
      gsub(/..B/,"&:",new_line)
      print " fixed-address "new_line";"


      By doing above your actual line's value will NEVER change and you could use it doing other stuff later point of time in your code too. I haven't tested above since samples were not given but it should work.






      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        You can't save { in to a variable since it denotes some action. Moreover in case you save output of sub or gsub it always gives you the count of substitutes it made to line/variable. so better try to change that code line to following.



        Change your 2 lines:



        x = {gsub(/..B/,"&:")}1
        print " fixed-address "print x";"


        To:



        new_line=$0
        gsub(/..B/,"&:",new_line)
        print " fixed-address "new_line";"


        By doing above your actual line's value will NEVER change and you could use it doing other stuff later point of time in your code too. I haven't tested above since samples were not given but it should work.






        share|improve this answer














        You can't save { in to a variable since it denotes some action. Moreover in case you save output of sub or gsub it always gives you the count of substitutes it made to line/variable. so better try to change that code line to following.



        Change your 2 lines:



        x = {gsub(/..B/,"&:")}1
        print " fixed-address "print x";"


        To:



        new_line=$0
        gsub(/..B/,"&:",new_line)
        print " fixed-address "new_line";"


        By doing above your actual line's value will NEVER change and you could use it doing other stuff later point of time in your code too. I haven't tested above since samples were not given but it should work.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 10:56

























        answered Nov 12 at 10:48









        RavinderSingh13

        25.4k41438




        25.4k41438






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2fstackoverflow.com%2fquestions%2f53260470%2fintegrate-awk-one-liner-into-awk-script%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

            Bressuire

            Vorschmack

            Quarantine