how to awk user input [duplicate]

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












1
















This question already has an answer here:



  • Pass shell variable as a /pattern/ to awk

    5 answers



I'm trying to match a pattern and then print out two lines after it. I'm successfully able to do it with the following code.



awk '/'202404'/c=3c&&c--' consolelog.log > testing.txt


I'm trying to replace the 202404 with something I can enter in the terminal. So far I have tried



echo "Enter peak value"
read peak
awk '/$peak/c=3c&&c--' consolelog.log > testing.txt


But it does not work.



I don't necessarily need to use awk, grep or sed or anything will do. I just need to match a user inputted pattern and print 2 lines after it.



***UPDATE*****
I was playing around with it and I got it to work. It was something I totally missed. Here's what I did.



awk 'c&&!--c;/'$peak'/c=2' consolelog.log > testing.txt










share|improve this question















marked as duplicate by steeldriver, Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jan 2 at 19:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • What does your data look like? Are you interested in substring matches of the peak value (as 2404 in the string 202404) or only exact matches? To match exactly, it would be helpful to see the data.

    – Kusalananda
    Jan 2 at 19:41












  • @Kusalananda, I want exact matches because, the max value i need is "202404" and in the raw data, the 2 lines after the value shows me the location i need. For example, the raw data looks similar to this peak value =202404 <br> ***********************<br> at location J512<br>

    – Vineeth Thomas
    Jan 2 at 19:52











  • With all due respect, your answer is bad to the point of potentially being dangerous.   I encourage you to look at DopeGhoti's answer below and  glenn jackman's answer to the duplicate question.

    – G-Man
    Jan 2 at 23:36
















1
















This question already has an answer here:



  • Pass shell variable as a /pattern/ to awk

    5 answers



I'm trying to match a pattern and then print out two lines after it. I'm successfully able to do it with the following code.



awk '/'202404'/c=3c&&c--' consolelog.log > testing.txt


I'm trying to replace the 202404 with something I can enter in the terminal. So far I have tried



echo "Enter peak value"
read peak
awk '/$peak/c=3c&&c--' consolelog.log > testing.txt


But it does not work.



I don't necessarily need to use awk, grep or sed or anything will do. I just need to match a user inputted pattern and print 2 lines after it.



***UPDATE*****
I was playing around with it and I got it to work. It was something I totally missed. Here's what I did.



awk 'c&&!--c;/'$peak'/c=2' consolelog.log > testing.txt










share|improve this question















marked as duplicate by steeldriver, Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jan 2 at 19:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • What does your data look like? Are you interested in substring matches of the peak value (as 2404 in the string 202404) or only exact matches? To match exactly, it would be helpful to see the data.

    – Kusalananda
    Jan 2 at 19:41












  • @Kusalananda, I want exact matches because, the max value i need is "202404" and in the raw data, the 2 lines after the value shows me the location i need. For example, the raw data looks similar to this peak value =202404 <br> ***********************<br> at location J512<br>

    – Vineeth Thomas
    Jan 2 at 19:52











  • With all due respect, your answer is bad to the point of potentially being dangerous.   I encourage you to look at DopeGhoti's answer below and  glenn jackman's answer to the duplicate question.

    – G-Man
    Jan 2 at 23:36














1












1








1









This question already has an answer here:



  • Pass shell variable as a /pattern/ to awk

    5 answers



I'm trying to match a pattern and then print out two lines after it. I'm successfully able to do it with the following code.



awk '/'202404'/c=3c&&c--' consolelog.log > testing.txt


I'm trying to replace the 202404 with something I can enter in the terminal. So far I have tried



echo "Enter peak value"
read peak
awk '/$peak/c=3c&&c--' consolelog.log > testing.txt


But it does not work.



I don't necessarily need to use awk, grep or sed or anything will do. I just need to match a user inputted pattern and print 2 lines after it.



***UPDATE*****
I was playing around with it and I got it to work. It was something I totally missed. Here's what I did.



awk 'c&&!--c;/'$peak'/c=2' consolelog.log > testing.txt










share|improve this question

















This question already has an answer here:



  • Pass shell variable as a /pattern/ to awk

    5 answers



I'm trying to match a pattern and then print out two lines after it. I'm successfully able to do it with the following code.



awk '/'202404'/c=3c&&c--' consolelog.log > testing.txt


I'm trying to replace the 202404 with something I can enter in the terminal. So far I have tried



echo "Enter peak value"
read peak
awk '/$peak/c=3c&&c--' consolelog.log > testing.txt


But it does not work.



I don't necessarily need to use awk, grep or sed or anything will do. I just need to match a user inputted pattern and print 2 lines after it.



***UPDATE*****
I was playing around with it and I got it to work. It was something I totally missed. Here's what I did.



awk 'c&&!--c;/'$peak'/c=2' consolelog.log > testing.txt





This question already has an answer here:



  • Pass shell variable as a /pattern/ to awk

    5 answers







bash shell-script awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 14:43









Rui F Ribeiro

39.5k1479132




39.5k1479132










asked Jan 2 at 19:30









Vineeth ThomasVineeth Thomas

264




264




marked as duplicate by steeldriver, Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jan 2 at 19:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by steeldriver, Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jan 2 at 19:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • What does your data look like? Are you interested in substring matches of the peak value (as 2404 in the string 202404) or only exact matches? To match exactly, it would be helpful to see the data.

    – Kusalananda
    Jan 2 at 19:41












  • @Kusalananda, I want exact matches because, the max value i need is "202404" and in the raw data, the 2 lines after the value shows me the location i need. For example, the raw data looks similar to this peak value =202404 <br> ***********************<br> at location J512<br>

    – Vineeth Thomas
    Jan 2 at 19:52











  • With all due respect, your answer is bad to the point of potentially being dangerous.   I encourage you to look at DopeGhoti's answer below and  glenn jackman's answer to the duplicate question.

    – G-Man
    Jan 2 at 23:36


















  • What does your data look like? Are you interested in substring matches of the peak value (as 2404 in the string 202404) or only exact matches? To match exactly, it would be helpful to see the data.

    – Kusalananda
    Jan 2 at 19:41












  • @Kusalananda, I want exact matches because, the max value i need is "202404" and in the raw data, the 2 lines after the value shows me the location i need. For example, the raw data looks similar to this peak value =202404 <br> ***********************<br> at location J512<br>

    – Vineeth Thomas
    Jan 2 at 19:52











  • With all due respect, your answer is bad to the point of potentially being dangerous.   I encourage you to look at DopeGhoti's answer below and  glenn jackman's answer to the duplicate question.

    – G-Man
    Jan 2 at 23:36

















What does your data look like? Are you interested in substring matches of the peak value (as 2404 in the string 202404) or only exact matches? To match exactly, it would be helpful to see the data.

– Kusalananda
Jan 2 at 19:41






What does your data look like? Are you interested in substring matches of the peak value (as 2404 in the string 202404) or only exact matches? To match exactly, it would be helpful to see the data.

– Kusalananda
Jan 2 at 19:41














@Kusalananda, I want exact matches because, the max value i need is "202404" and in the raw data, the 2 lines after the value shows me the location i need. For example, the raw data looks similar to this peak value =202404 <br> ***********************<br> at location J512<br>

– Vineeth Thomas
Jan 2 at 19:52





@Kusalananda, I want exact matches because, the max value i need is "202404" and in the raw data, the 2 lines after the value shows me the location i need. For example, the raw data looks similar to this peak value =202404 <br> ***********************<br> at location J512<br>

– Vineeth Thomas
Jan 2 at 19:52













With all due respect, your answer is bad to the point of potentially being dangerous.   I encourage you to look at DopeGhoti's answer below and  glenn jackman's answer to the duplicate question.

– G-Man
Jan 2 at 23:36






With all due respect, your answer is bad to the point of potentially being dangerous.   I encourage you to look at DopeGhoti's answer below and  glenn jackman's answer to the duplicate question.

– G-Man
Jan 2 at 23:36











2 Answers
2






active

oldest

votes


















0














Your code:



echo "Enter peak value"
read peak
awk '/$peak/c=3c&&c--' consolelog.log > testing.txt


This does not work because strong quotes prevent the shell from performing variable expansion. This is good because awk uses a lot of syntax that weak quotes would make very difficult to use.



There are, however, ways to use environment variables within an awk script.



You can declare your own variable:



read peak
awk -vpeak="$peak" '$0 ~ peak c=3 c&&c--' consolelog.log > testing.txt


Or you can just tell awk to look at the one you just used read to fill:



read peak
export peak
awk '$0 ~ ENVIRON["peak"] c=3 c&&c--' consolelog.log > testing.txt





share|improve this answer
































    -1














    echo "Enter peak value"
    read peak
    awk '/'"$peak"'/c=3c&&c--' consolelog.log > testing.txt


    Try this.



    You need to close single quotes, and then you can use your Bash variable. I recommend placing it in a double-quote environment because otherwise it would cause trouble with other arguments to awk.






    share|improve this answer























    • No, you should not inject code into scripts like that. awk can read environment variables and it can take values on the command line with -v.

      – Kusalananda
      Jan 2 at 19:44












    • Is security a big concern? If it is, you might be right, but IMHO it is not important for this script. Needs more input from OP.

      – onur güngör
      Jan 2 at 19:47











    • Dancing around with 's and " and switching quoting modes inline like this does work, but it's poor practice because it's already starting off as hard to read, and future-you six months from now at 4AM trying to parse this when your process just tipped over is going to want to go back in time six months and educate emself with a blunt object.

      – DopeGhoti
      Jan 2 at 19:52






    • 3





      Security may not be a big concern for this user, but remember that by presenting an answer, others may copy and paste your code into systems where security is a concern.

      – Kusalananda
      Jan 2 at 19:53


















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Your code:



    echo "Enter peak value"
    read peak
    awk '/$peak/c=3c&&c--' consolelog.log > testing.txt


    This does not work because strong quotes prevent the shell from performing variable expansion. This is good because awk uses a lot of syntax that weak quotes would make very difficult to use.



    There are, however, ways to use environment variables within an awk script.



    You can declare your own variable:



    read peak
    awk -vpeak="$peak" '$0 ~ peak c=3 c&&c--' consolelog.log > testing.txt


    Or you can just tell awk to look at the one you just used read to fill:



    read peak
    export peak
    awk '$0 ~ ENVIRON["peak"] c=3 c&&c--' consolelog.log > testing.txt





    share|improve this answer





























      0














      Your code:



      echo "Enter peak value"
      read peak
      awk '/$peak/c=3c&&c--' consolelog.log > testing.txt


      This does not work because strong quotes prevent the shell from performing variable expansion. This is good because awk uses a lot of syntax that weak quotes would make very difficult to use.



      There are, however, ways to use environment variables within an awk script.



      You can declare your own variable:



      read peak
      awk -vpeak="$peak" '$0 ~ peak c=3 c&&c--' consolelog.log > testing.txt


      Or you can just tell awk to look at the one you just used read to fill:



      read peak
      export peak
      awk '$0 ~ ENVIRON["peak"] c=3 c&&c--' consolelog.log > testing.txt





      share|improve this answer



























        0












        0








        0







        Your code:



        echo "Enter peak value"
        read peak
        awk '/$peak/c=3c&&c--' consolelog.log > testing.txt


        This does not work because strong quotes prevent the shell from performing variable expansion. This is good because awk uses a lot of syntax that weak quotes would make very difficult to use.



        There are, however, ways to use environment variables within an awk script.



        You can declare your own variable:



        read peak
        awk -vpeak="$peak" '$0 ~ peak c=3 c&&c--' consolelog.log > testing.txt


        Or you can just tell awk to look at the one you just used read to fill:



        read peak
        export peak
        awk '$0 ~ ENVIRON["peak"] c=3 c&&c--' consolelog.log > testing.txt





        share|improve this answer















        Your code:



        echo "Enter peak value"
        read peak
        awk '/$peak/c=3c&&c--' consolelog.log > testing.txt


        This does not work because strong quotes prevent the shell from performing variable expansion. This is good because awk uses a lot of syntax that weak quotes would make very difficult to use.



        There are, however, ways to use environment variables within an awk script.



        You can declare your own variable:



        read peak
        awk -vpeak="$peak" '$0 ~ peak c=3 c&&c--' consolelog.log > testing.txt


        Or you can just tell awk to look at the one you just used read to fill:



        read peak
        export peak
        awk '$0 ~ ENVIRON["peak"] c=3 c&&c--' consolelog.log > testing.txt






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 2 at 20:00









        Kusalananda

        124k16234385




        124k16234385










        answered Jan 2 at 19:47









        DopeGhotiDopeGhoti

        43.9k55382




        43.9k55382























            -1














            echo "Enter peak value"
            read peak
            awk '/'"$peak"'/c=3c&&c--' consolelog.log > testing.txt


            Try this.



            You need to close single quotes, and then you can use your Bash variable. I recommend placing it in a double-quote environment because otherwise it would cause trouble with other arguments to awk.






            share|improve this answer























            • No, you should not inject code into scripts like that. awk can read environment variables and it can take values on the command line with -v.

              – Kusalananda
              Jan 2 at 19:44












            • Is security a big concern? If it is, you might be right, but IMHO it is not important for this script. Needs more input from OP.

              – onur güngör
              Jan 2 at 19:47











            • Dancing around with 's and " and switching quoting modes inline like this does work, but it's poor practice because it's already starting off as hard to read, and future-you six months from now at 4AM trying to parse this when your process just tipped over is going to want to go back in time six months and educate emself with a blunt object.

              – DopeGhoti
              Jan 2 at 19:52






            • 3





              Security may not be a big concern for this user, but remember that by presenting an answer, others may copy and paste your code into systems where security is a concern.

              – Kusalananda
              Jan 2 at 19:53
















            -1














            echo "Enter peak value"
            read peak
            awk '/'"$peak"'/c=3c&&c--' consolelog.log > testing.txt


            Try this.



            You need to close single quotes, and then you can use your Bash variable. I recommend placing it in a double-quote environment because otherwise it would cause trouble with other arguments to awk.






            share|improve this answer























            • No, you should not inject code into scripts like that. awk can read environment variables and it can take values on the command line with -v.

              – Kusalananda
              Jan 2 at 19:44












            • Is security a big concern? If it is, you might be right, but IMHO it is not important for this script. Needs more input from OP.

              – onur güngör
              Jan 2 at 19:47











            • Dancing around with 's and " and switching quoting modes inline like this does work, but it's poor practice because it's already starting off as hard to read, and future-you six months from now at 4AM trying to parse this when your process just tipped over is going to want to go back in time six months and educate emself with a blunt object.

              – DopeGhoti
              Jan 2 at 19:52






            • 3





              Security may not be a big concern for this user, but remember that by presenting an answer, others may copy and paste your code into systems where security is a concern.

              – Kusalananda
              Jan 2 at 19:53














            -1












            -1








            -1







            echo "Enter peak value"
            read peak
            awk '/'"$peak"'/c=3c&&c--' consolelog.log > testing.txt


            Try this.



            You need to close single quotes, and then you can use your Bash variable. I recommend placing it in a double-quote environment because otherwise it would cause trouble with other arguments to awk.






            share|improve this answer













            echo "Enter peak value"
            read peak
            awk '/'"$peak"'/c=3c&&c--' consolelog.log > testing.txt


            Try this.



            You need to close single quotes, and then you can use your Bash variable. I recommend placing it in a double-quote environment because otherwise it would cause trouble with other arguments to awk.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 2 at 19:43









            onur güngöronur güngör

            764513




            764513












            • No, you should not inject code into scripts like that. awk can read environment variables and it can take values on the command line with -v.

              – Kusalananda
              Jan 2 at 19:44












            • Is security a big concern? If it is, you might be right, but IMHO it is not important for this script. Needs more input from OP.

              – onur güngör
              Jan 2 at 19:47











            • Dancing around with 's and " and switching quoting modes inline like this does work, but it's poor practice because it's already starting off as hard to read, and future-you six months from now at 4AM trying to parse this when your process just tipped over is going to want to go back in time six months and educate emself with a blunt object.

              – DopeGhoti
              Jan 2 at 19:52






            • 3





              Security may not be a big concern for this user, but remember that by presenting an answer, others may copy and paste your code into systems where security is a concern.

              – Kusalananda
              Jan 2 at 19:53


















            • No, you should not inject code into scripts like that. awk can read environment variables and it can take values on the command line with -v.

              – Kusalananda
              Jan 2 at 19:44












            • Is security a big concern? If it is, you might be right, but IMHO it is not important for this script. Needs more input from OP.

              – onur güngör
              Jan 2 at 19:47











            • Dancing around with 's and " and switching quoting modes inline like this does work, but it's poor practice because it's already starting off as hard to read, and future-you six months from now at 4AM trying to parse this when your process just tipped over is going to want to go back in time six months and educate emself with a blunt object.

              – DopeGhoti
              Jan 2 at 19:52






            • 3





              Security may not be a big concern for this user, but remember that by presenting an answer, others may copy and paste your code into systems where security is a concern.

              – Kusalananda
              Jan 2 at 19:53

















            No, you should not inject code into scripts like that. awk can read environment variables and it can take values on the command line with -v.

            – Kusalananda
            Jan 2 at 19:44






            No, you should not inject code into scripts like that. awk can read environment variables and it can take values on the command line with -v.

            – Kusalananda
            Jan 2 at 19:44














            Is security a big concern? If it is, you might be right, but IMHO it is not important for this script. Needs more input from OP.

            – onur güngör
            Jan 2 at 19:47





            Is security a big concern? If it is, you might be right, but IMHO it is not important for this script. Needs more input from OP.

            – onur güngör
            Jan 2 at 19:47













            Dancing around with 's and " and switching quoting modes inline like this does work, but it's poor practice because it's already starting off as hard to read, and future-you six months from now at 4AM trying to parse this when your process just tipped over is going to want to go back in time six months and educate emself with a blunt object.

            – DopeGhoti
            Jan 2 at 19:52





            Dancing around with 's and " and switching quoting modes inline like this does work, but it's poor practice because it's already starting off as hard to read, and future-you six months from now at 4AM trying to parse this when your process just tipped over is going to want to go back in time six months and educate emself with a blunt object.

            – DopeGhoti
            Jan 2 at 19:52




            3




            3





            Security may not be a big concern for this user, but remember that by presenting an answer, others may copy and paste your code into systems where security is a concern.

            – Kusalananda
            Jan 2 at 19:53






            Security may not be a big concern for this user, but remember that by presenting an answer, others may copy and paste your code into systems where security is a concern.

            – Kusalananda
            Jan 2 at 19:53



            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?