1

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=3}c&&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=3}c&&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

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • 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 02 '19 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

    ***********************

    at location J512

    – Vineeth Thomas Jan 02 '19 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 Says 'Reinstate Monica' Jan 02 '19 at 23:36

2 Answers2

0

Your code:

echo "Enter peak value"
read peak
awk '/${peak}/{c=3}c&&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
Kusalananda
  • 333,661
DopeGhoti
  • 76,081
-1
echo "Enter peak value"
read peak
awk '/'"${peak}"'/{c=3}c&&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.

  • 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 02 '19 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 02 '19 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 02 '19 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 02 '19 at 19:53