Печатать все строки до шаблона2, начиная с ближайшего совпадения с шаблоном1

Вместо того, чтобы писать имя, просто выберите идентификатор. Легче, быстрее.

Отсюда:

xinput --list-props 'TRACKPAD NAME'

К этому:

xinput --list-props 8 
2
29.09.2021, 22:22
2 ответа

Что касается keeping a pattern space comprising all text since the last match "Compiling", but there can be thousands of lines without an error. Would it be very inefficient?-, это, вероятно, будет не менее эффективным, чем любой альтернативный подход, такой как выполнение 2 проходов входного файла для определения совпадающих пар разделителей перед началом печати, и его преимущество в том, что он будет работать независимо от того, ввод хранится в файле или поступает из канала.

Вероятно, наиболее эффективно использовать 2 вызова tacс awkмежду ними, если вы работаете в системе сtac:

$ tac file |
    awk '/^error:/{f=1; print "---separator---"} f; /^Compiling/{f=0}' |
        tac
Compiling File3
... commands...
In file included from...
In file included from...
In file included from...
error: could not find A
---separator---
Compiling File5
... commands...
In file included from...
In file included from...
In file included from...
error: could not find B
---separator---

В противном случае просто используйте любой awk в любой оболочке на каждом компьютере Unix:

$ awk '
    /^Compiling/ { buf="" }
    { buf = buf $0 "\n" }
    /^error:/ { print buf "---separator---" }
' file
Compiling File3
... commands...
In file included from...
In file included from...
In file included from...
error: could not find A
---separator---
Compiling File5
... commands...
In file included from...
In file included from...
In file included from...
error: could not find B
---separator---

В качестве альтернативы, использование GNU awk для мульти -символов RS и RT:

$ awk -v RS='\nerror:[^\n]+' -v ORS='\n---separator---\n' '
    sub(/(^|.*\n)Compiling/,"Compiling") { print $0 RT }
' file
Compiling File3
... commands...
In file included from...
In file included from...
In file included from...
error: could not find A
---separator---
Compiling File5
... commands...
In file included from...
In file included from...
In file included from...
error: could not find B
---separator---
2
30.09.2021, 02:11

Использовать perlочень просто, так как он имеет режим абзаца с-00:

perl -00 -ne 'print if /\nerror:/' file

Выход:

Compiling File3
... commands...
In file included from...
In file included from...
In file included from...
error: could not find A

Compiling File5
... commands...
In file included from...
In file included from...
In file included from...
error: could not find B

Если вы добавите | sed 's/^$/----separator----/', вы также можете добавить свой собственный разделитель вместо пустых строк, если вам это нужно.

2
30.09.2021, 06:54

Теги

Похожие вопросы