Searching a pattern using awk and print next 15 li

2019-09-21 14:01发布

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

3条回答
再贱就再见
2楼-- · 2019-09-21 14:46

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
查看更多
唯我独甜
3楼-- · 2019-09-21 14:53

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.

查看更多
Ridiculous、
4楼-- · 2019-09-21 14:56

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

查看更多
登录 后发表回答