-2

Searching a pattern using awk and print next 15 lines from a log file

awk '/^DERAT1SB[0-9][0-9]# show registration/{nr[NR]; nr[NR+4]}; NR in nr' slb.log

I am using above command to search a pattern DERAT1SB23# show registration inside a log file, but it is throwing a syntax error.

ERROR:

awk: syntax error near line 1

awk: bailing out near line 1

kvantour
  • 25,269
  • 4
  • 47
  • 72
Amiyanshu
  • 1
  • 2
  • 2
    You're using old, broken awk. Google it. There are other issues too which you can investigate after that. – Ed Morton Aug 02 '18 at 15:47
  • Can you please show us snippets of the log file you're trying to search? That might help you get a better answer. Also, if you're trying to get the next 15 lines, I'd just use grep to search for what you need and then use the -A flag to specify the number of lines you need after the search string (e.g. `grep $str filename -A $lineNum`) – AndreasKralj Aug 02 '18 at 15:48
  • you are using an old version of `awk` where the word old should be replace with antiquated. You will get the same error when you do `awk 1 /dev/null`. Nonetheless, the problem is most likely located in the `#` which you might want to escape. – kvantour Aug 02 '18 at 15:56
  • @kvantour there's nothing special about a # in a regexp. – Ed Morton Aug 02 '18 at 16:02
  • @EdMorton I know, but you never know with old versions of AWK. – kvantour Aug 02 '18 at 16:02
  • 1
    True but as in the example you posted old, broken awk can't handle a condition without an explicit action so now look at the last part of the OPs script :-). – Ed Morton Aug 02 '18 at 16:04
  • How about `grep -A15 pattern file`? – Mark Setchell Aug 02 '18 at 16:10
  • systems that have the [old]awk, as shown in your error message, usually have a `nawk` that will give you much better error messages. Also check if you have `/usr/xpg4/bin/awk` as that an even newer version. Finally, pray that your system has `gawk` installed. It will be in a different directory and it may be named as `awk`, so you'll have to ask around your organization to see where the GNU versions are kept. Maybe that is standardized now, but for the 4 places I worked, there where 5 different dirs for `gawk` ;-). Good luck. – shellter Aug 02 '18 at 16:19
  • @EdMorton, I seem to be unable to fix it. Nor with brackets nor with whatever. `awk '(NR in nr){print $0}' /dev/null` gives exactly the same problem. – kvantour Aug 02 '18 at 16:25
  • Its entirely possible that old awk doesn't have an `in` operator or doesn't allow use of the `in` operator in that context. idk. – Ed Morton Aug 02 '18 at 16:31
  • @EdMorton so far I only found for loops which work. If statements fail. – kvantour Aug 02 '18 at 17:03
  • Yeah I vaguely remember now that that was the case. – Ed Morton Aug 02 '18 at 17:11
  • @Mark Setchell, i can't use grep -A as it's not supported in Solaris. – Amiyanshu Aug 06 '18 at 15:07
  • Guys below is the sample log , – Amiyanshu Aug 06 '18 at 15:30
  • I think there’s a proper `grep` in `/usr/xpg4/bin` or somesuch. – Mark Setchell Aug 06 '18 at 20:20

3 Answers3

0

Search for a pattern and print next 15 lines: using GNU sed

sed -n '/pattern/,+15 p' file

With plain awk:

awk '
    /pattern/ {
        print
        for (i=1; i<=15; i++) {getline; print}
        exit
    }
' file
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • ITYM `awk '/regexp/{c=16}c&&c--' file` or similar. See https://stackoverflow.com/a/17914105/1745001 for more examples of printing lines around a match and see http://awk.freeshell.org/AllAboutGetline for the issues with using getline. – Ed Morton Aug 02 '18 at 15:58
0

awk: bailing out near line 1

Is a known issue and you should change in your code on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 1
    `nawk` is inferior to xpg awk (missing functionality so not as close to POSIX compliant) so there's no reason to suggest using nawk on any system that has either xpg awk available. – Ed Morton Aug 02 '18 at 15:54
  • @EdMorton, sure in some SUN os since I have not seen `nawk` so thought which ever is present on OP's system he/she could use it then. – RavinderSingh13 Aug 02 '18 at 15:55
  • AFAIK every SUN/Solaris has both and the only reason to use nawk is to keep using old nawk scripts which would break on a POSIX awk. – Ed Morton Aug 02 '18 at 17:14
0

From Effective Awk Programming, Edition 4:

A Rose by Any Other Name The awk language has evolved over the years. Full details are provided in Appendix A [The Evolution of the awk Language], page 439. The language described in this book is often referred to as "new awk." By analogy, the original version of awk is referred to as "old awk."

On most current systems, when you run the awk utility you get some version of new awk.4 If your system’s standard awk is the old one, you will see something like this if you try the test program:

$ awk 1 /dev/null
error awk: syntax error near line 1
error awk: bailing out near line 1

In this case, you should find a version of new awk, or just install gawk!

Throughout this book, whenever we refer to a language feature that should be available in any complete implementation of POSIX awk, we simply use the term awk. When referring to a feature that is specific to the GNU implementation, we use the term gawk.

4 Only Solaris systems still use an old awk for > the default awk utility. A more modern awk lives in /usr/xpg6/bin on these systems.

kvantour
  • 25,269
  • 4
  • 47
  • 72