use “$n” as a string bash variable in an awk script?












0














I have an awk script in a bash script that I am starting to generalise.



I would like to put the string "$3" into a bash variable and use that variable in the awk script. This would allow me to easily update the script as needed.



For example:



NR > 1 && $3 != p { 
#blah blah blah
printf("%s_%s%s", $3, header[i], OFS)
}


would become something like



foo="$3"
NR > 1 && $foo != p {
#blah blah blah
printf("%s_%s%s", $foo, header[i], OFS)
}


I've tried various combinations of foo="$3", ='$3', "$foo", '$foo', and ${foo} and can't get it to work.



What am I doing wrong?



.



I want to replace each instance of "$3" with "$foo" so that I only need to update $foo if I want to change the script.



The complete awk script inside the bash script:



#!/bin/bash

#these are bash variables
file=$1
header=$(head -n1 $file)


############################
# awk script #
############################
read -d '' awkscript << 'EOF'
BEGIN { OFS = "\t" }

/^@/ {
for (i = 1; i <= NF; ++i)
header[i] = $i
next
}

NR > 1 && $3 != p {
#output two blank lines if needed
if (print_blank) {
print "\n"
}
print_blank = 1

for (i = 1; i <= 3; ++i)
printf("%s%s", header[i], OFS)

for (i = 4; i < NF; ++i)
printf("%s_%s%s", $3, header[i], OFS)
printf("%s_%s%s", $3, header[NF], ORS)
}

{ p=$3; print }
EOF

############################
# end awk script #
############################

#blah
#blah
#blah

awk "$awkscript" ${tmp} > ${output}









share|improve this question
























  • Is $3 coming from a positional parameter in your bash script?
    – Jesse_b
    16 mins ago










  • @Jesse_b No. I want to put "$3" into the awk script so that it will match the third token in the string fed to the awk script.
    – masher
    14 mins ago










  • Yes but where is $3 coming from?
    – Jesse_b
    14 mins ago










  • Are you trying to select a field based on the input variable?
    – Jesse_b
    12 mins ago










  • @Jesse_b I'm telling awk to match the third token. This happens in a bunch of places. I want to pull that out and replace it with a single variable so I can change that single variable to match the 4th or 5th or whateverth token, without having to find every single instance of it each time.
    – masher
    8 mins ago
















0














I have an awk script in a bash script that I am starting to generalise.



I would like to put the string "$3" into a bash variable and use that variable in the awk script. This would allow me to easily update the script as needed.



For example:



NR > 1 && $3 != p { 
#blah blah blah
printf("%s_%s%s", $3, header[i], OFS)
}


would become something like



foo="$3"
NR > 1 && $foo != p {
#blah blah blah
printf("%s_%s%s", $foo, header[i], OFS)
}


I've tried various combinations of foo="$3", ='$3', "$foo", '$foo', and ${foo} and can't get it to work.



What am I doing wrong?



.



I want to replace each instance of "$3" with "$foo" so that I only need to update $foo if I want to change the script.



The complete awk script inside the bash script:



#!/bin/bash

#these are bash variables
file=$1
header=$(head -n1 $file)


############################
# awk script #
############################
read -d '' awkscript << 'EOF'
BEGIN { OFS = "\t" }

/^@/ {
for (i = 1; i <= NF; ++i)
header[i] = $i
next
}

NR > 1 && $3 != p {
#output two blank lines if needed
if (print_blank) {
print "\n"
}
print_blank = 1

for (i = 1; i <= 3; ++i)
printf("%s%s", header[i], OFS)

for (i = 4; i < NF; ++i)
printf("%s_%s%s", $3, header[i], OFS)
printf("%s_%s%s", $3, header[NF], ORS)
}

{ p=$3; print }
EOF

############################
# end awk script #
############################

#blah
#blah
#blah

awk "$awkscript" ${tmp} > ${output}









share|improve this question
























  • Is $3 coming from a positional parameter in your bash script?
    – Jesse_b
    16 mins ago










  • @Jesse_b No. I want to put "$3" into the awk script so that it will match the third token in the string fed to the awk script.
    – masher
    14 mins ago










  • Yes but where is $3 coming from?
    – Jesse_b
    14 mins ago










  • Are you trying to select a field based on the input variable?
    – Jesse_b
    12 mins ago










  • @Jesse_b I'm telling awk to match the third token. This happens in a bunch of places. I want to pull that out and replace it with a single variable so I can change that single variable to match the 4th or 5th or whateverth token, without having to find every single instance of it each time.
    – masher
    8 mins ago














0












0








0







I have an awk script in a bash script that I am starting to generalise.



I would like to put the string "$3" into a bash variable and use that variable in the awk script. This would allow me to easily update the script as needed.



For example:



NR > 1 && $3 != p { 
#blah blah blah
printf("%s_%s%s", $3, header[i], OFS)
}


would become something like



foo="$3"
NR > 1 && $foo != p {
#blah blah blah
printf("%s_%s%s", $foo, header[i], OFS)
}


I've tried various combinations of foo="$3", ='$3', "$foo", '$foo', and ${foo} and can't get it to work.



What am I doing wrong?



.



I want to replace each instance of "$3" with "$foo" so that I only need to update $foo if I want to change the script.



The complete awk script inside the bash script:



#!/bin/bash

#these are bash variables
file=$1
header=$(head -n1 $file)


############################
# awk script #
############################
read -d '' awkscript << 'EOF'
BEGIN { OFS = "\t" }

/^@/ {
for (i = 1; i <= NF; ++i)
header[i] = $i
next
}

NR > 1 && $3 != p {
#output two blank lines if needed
if (print_blank) {
print "\n"
}
print_blank = 1

for (i = 1; i <= 3; ++i)
printf("%s%s", header[i], OFS)

for (i = 4; i < NF; ++i)
printf("%s_%s%s", $3, header[i], OFS)
printf("%s_%s%s", $3, header[NF], ORS)
}

{ p=$3; print }
EOF

############################
# end awk script #
############################

#blah
#blah
#blah

awk "$awkscript" ${tmp} > ${output}









share|improve this question















I have an awk script in a bash script that I am starting to generalise.



I would like to put the string "$3" into a bash variable and use that variable in the awk script. This would allow me to easily update the script as needed.



For example:



NR > 1 && $3 != p { 
#blah blah blah
printf("%s_%s%s", $3, header[i], OFS)
}


would become something like



foo="$3"
NR > 1 && $foo != p {
#blah blah blah
printf("%s_%s%s", $foo, header[i], OFS)
}


I've tried various combinations of foo="$3", ='$3', "$foo", '$foo', and ${foo} and can't get it to work.



What am I doing wrong?



.



I want to replace each instance of "$3" with "$foo" so that I only need to update $foo if I want to change the script.



The complete awk script inside the bash script:



#!/bin/bash

#these are bash variables
file=$1
header=$(head -n1 $file)


############################
# awk script #
############################
read -d '' awkscript << 'EOF'
BEGIN { OFS = "\t" }

/^@/ {
for (i = 1; i <= NF; ++i)
header[i] = $i
next
}

NR > 1 && $3 != p {
#output two blank lines if needed
if (print_blank) {
print "\n"
}
print_blank = 1

for (i = 1; i <= 3; ++i)
printf("%s%s", header[i], OFS)

for (i = 4; i < NF; ++i)
printf("%s_%s%s", $3, header[i], OFS)
printf("%s_%s%s", $3, header[NF], ORS)
}

{ p=$3; print }
EOF

############################
# end awk script #
############################

#blah
#blah
#blah

awk "$awkscript" ${tmp} > ${output}






bash awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 9 mins ago







masher

















asked 18 mins ago









mashermasher

1084




1084












  • Is $3 coming from a positional parameter in your bash script?
    – Jesse_b
    16 mins ago










  • @Jesse_b No. I want to put "$3" into the awk script so that it will match the third token in the string fed to the awk script.
    – masher
    14 mins ago










  • Yes but where is $3 coming from?
    – Jesse_b
    14 mins ago










  • Are you trying to select a field based on the input variable?
    – Jesse_b
    12 mins ago










  • @Jesse_b I'm telling awk to match the third token. This happens in a bunch of places. I want to pull that out and replace it with a single variable so I can change that single variable to match the 4th or 5th or whateverth token, without having to find every single instance of it each time.
    – masher
    8 mins ago


















  • Is $3 coming from a positional parameter in your bash script?
    – Jesse_b
    16 mins ago










  • @Jesse_b No. I want to put "$3" into the awk script so that it will match the third token in the string fed to the awk script.
    – masher
    14 mins ago










  • Yes but where is $3 coming from?
    – Jesse_b
    14 mins ago










  • Are you trying to select a field based on the input variable?
    – Jesse_b
    12 mins ago










  • @Jesse_b I'm telling awk to match the third token. This happens in a bunch of places. I want to pull that out and replace it with a single variable so I can change that single variable to match the 4th or 5th or whateverth token, without having to find every single instance of it each time.
    – masher
    8 mins ago
















Is $3 coming from a positional parameter in your bash script?
– Jesse_b
16 mins ago




Is $3 coming from a positional parameter in your bash script?
– Jesse_b
16 mins ago












@Jesse_b No. I want to put "$3" into the awk script so that it will match the third token in the string fed to the awk script.
– masher
14 mins ago




@Jesse_b No. I want to put "$3" into the awk script so that it will match the third token in the string fed to the awk script.
– masher
14 mins ago












Yes but where is $3 coming from?
– Jesse_b
14 mins ago




Yes but where is $3 coming from?
– Jesse_b
14 mins ago












Are you trying to select a field based on the input variable?
– Jesse_b
12 mins ago




Are you trying to select a field based on the input variable?
– Jesse_b
12 mins ago












@Jesse_b I'm telling awk to match the third token. This happens in a bunch of places. I want to pull that out and replace it with a single variable so I can change that single variable to match the 4th or 5th or whateverth token, without having to find every single instance of it each time.
– masher
8 mins ago




@Jesse_b I'm telling awk to match the third token. This happens in a bunch of places. I want to pull that out and replace it with a single variable so I can change that single variable to match the 4th or 5th or whateverth token, without having to find every single instance of it each time.
– masher
8 mins ago










1 Answer
1






active

oldest

votes


















0














You can inject a shell variable to your awk script with the -v option:



#!/bin/bash

#these are bash variables
file=$1
header=$(head -n1 $file)
awktoken=$2

############################
# awk script #
############################
read -d '' awkscript << 'EOF'
BEGIN { OFS = "\t" }

/^@/ {
for (i = 1; i <= NF; ++i)
header[i] = $i
next
}

NR > 1 && $variable != p {
#output two blank lines if needed
if (print_blank) {
print "\n"
}
print_blank = 1

for (i = 1; i <= 3; ++i)
printf("%s%s", header[i], OFS)

for (i = 4; i < NF; ++i)
printf("%s_%s%s", $variable, header[i], OFS)
printf("%s_%s%s", $variable, header[NF], ORS)
}

{ p=$variable; print }
EOF

############################
# end awk script #
############################

awk -vvariable="$awktoken" "$awkscript" ${tmp} > ${output}




variable is the name of the variable in this example. Normally you would call it without the $ but that is left in for it to expand to $3 or whatever number you choose





share





















    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%2f493587%2fuse-n-as-a-string-bash-variable-in-an-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









    0














    You can inject a shell variable to your awk script with the -v option:



    #!/bin/bash

    #these are bash variables
    file=$1
    header=$(head -n1 $file)
    awktoken=$2

    ############################
    # awk script #
    ############################
    read -d '' awkscript << 'EOF'
    BEGIN { OFS = "\t" }

    /^@/ {
    for (i = 1; i <= NF; ++i)
    header[i] = $i
    next
    }

    NR > 1 && $variable != p {
    #output two blank lines if needed
    if (print_blank) {
    print "\n"
    }
    print_blank = 1

    for (i = 1; i <= 3; ++i)
    printf("%s%s", header[i], OFS)

    for (i = 4; i < NF; ++i)
    printf("%s_%s%s", $variable, header[i], OFS)
    printf("%s_%s%s", $variable, header[NF], ORS)
    }

    { p=$variable; print }
    EOF

    ############################
    # end awk script #
    ############################

    awk -vvariable="$awktoken" "$awkscript" ${tmp} > ${output}




    variable is the name of the variable in this example. Normally you would call it without the $ but that is left in for it to expand to $3 or whatever number you choose





    share


























      0














      You can inject a shell variable to your awk script with the -v option:



      #!/bin/bash

      #these are bash variables
      file=$1
      header=$(head -n1 $file)
      awktoken=$2

      ############################
      # awk script #
      ############################
      read -d '' awkscript << 'EOF'
      BEGIN { OFS = "\t" }

      /^@/ {
      for (i = 1; i <= NF; ++i)
      header[i] = $i
      next
      }

      NR > 1 && $variable != p {
      #output two blank lines if needed
      if (print_blank) {
      print "\n"
      }
      print_blank = 1

      for (i = 1; i <= 3; ++i)
      printf("%s%s", header[i], OFS)

      for (i = 4; i < NF; ++i)
      printf("%s_%s%s", $variable, header[i], OFS)
      printf("%s_%s%s", $variable, header[NF], ORS)
      }

      { p=$variable; print }
      EOF

      ############################
      # end awk script #
      ############################

      awk -vvariable="$awktoken" "$awkscript" ${tmp} > ${output}




      variable is the name of the variable in this example. Normally you would call it without the $ but that is left in for it to expand to $3 or whatever number you choose





      share
























        0












        0








        0






        You can inject a shell variable to your awk script with the -v option:



        #!/bin/bash

        #these are bash variables
        file=$1
        header=$(head -n1 $file)
        awktoken=$2

        ############################
        # awk script #
        ############################
        read -d '' awkscript << 'EOF'
        BEGIN { OFS = "\t" }

        /^@/ {
        for (i = 1; i <= NF; ++i)
        header[i] = $i
        next
        }

        NR > 1 && $variable != p {
        #output two blank lines if needed
        if (print_blank) {
        print "\n"
        }
        print_blank = 1

        for (i = 1; i <= 3; ++i)
        printf("%s%s", header[i], OFS)

        for (i = 4; i < NF; ++i)
        printf("%s_%s%s", $variable, header[i], OFS)
        printf("%s_%s%s", $variable, header[NF], ORS)
        }

        { p=$variable; print }
        EOF

        ############################
        # end awk script #
        ############################

        awk -vvariable="$awktoken" "$awkscript" ${tmp} > ${output}




        variable is the name of the variable in this example. Normally you would call it without the $ but that is left in for it to expand to $3 or whatever number you choose





        share












        You can inject a shell variable to your awk script with the -v option:



        #!/bin/bash

        #these are bash variables
        file=$1
        header=$(head -n1 $file)
        awktoken=$2

        ############################
        # awk script #
        ############################
        read -d '' awkscript << 'EOF'
        BEGIN { OFS = "\t" }

        /^@/ {
        for (i = 1; i <= NF; ++i)
        header[i] = $i
        next
        }

        NR > 1 && $variable != p {
        #output two blank lines if needed
        if (print_blank) {
        print "\n"
        }
        print_blank = 1

        for (i = 1; i <= 3; ++i)
        printf("%s%s", header[i], OFS)

        for (i = 4; i < NF; ++i)
        printf("%s_%s%s", $variable, header[i], OFS)
        printf("%s_%s%s", $variable, header[NF], ORS)
        }

        { p=$variable; print }
        EOF

        ############################
        # end awk script #
        ############################

        awk -vvariable="$awktoken" "$awkscript" ${tmp} > ${output}




        variable is the name of the variable in this example. Normally you would call it without the $ but that is left in for it to expand to $3 or whatever number you choose






        share











        share


        share










        answered 1 min ago









        Jesse_bJesse_b

        12k23064




        12k23064






























            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.





            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%2funix.stackexchange.com%2fquestions%2f493587%2fuse-n-as-a-string-bash-variable-in-an-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

            Entries order in /etc/network/interfaces

            新発田市

            Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)