Использование awk для поиска совпадений и извлечения символов ДО каждого совпадения - помогите!

Pregunta 1:agregar includepkgs=kernel*a yum.conf. Esto hará que nunca se actualice ningún paquete, excepto los paquetes de su núcleo. También evitará la instalación de paquetes que no comiencen con el kernel, así que asegúrese de haber instalado todo lo necesario antes de agregar esta línea. Alternativamente, puede agregar esta línea a archivos de repositorio individuales, y solo afectará eso repo.

Pregunta 2:agregue exclude=postgresql*de la misma manera que includepkg. Esto hará que no se puedan instalar o actualizar paquetes cuyos nombres comiencen con postgresql.

2
11.06.2019, 23:20
3 ответа

Вот пример сценария awk.

 awk '/..WAP../{print substr($0, index($0,"WAP") - 2, 7);}' input.csv

образец ввода:

junk
line 1 12WAP34 678
another line  abWAPcdefg
WAP123
junk WAP

выход:

12WAP34
abWAPcd

объяснение:

/..WAP../{                          # for line containt WAP with 2 chars wrap
    wapPosition = index($0,"WAP") - 2;  # find the position of WAP - 2 chars
    output = substr($0, wapPosition, 7);# output is 7 chars length from wapPostion
    print output;                   # print output
}
0
27.01.2020, 22:17

В GNU Awk вы можете использовать группу захвата в функции matchи получать доступ к ее содержимому через необязательный параметр массива:

$ echo ',x,x,x,x,x,xx,Yes,"1 WAP, other stuff, other stuff",no,x' | 
    awk 'match($0,/([0-9]).WAP/,a) {print a[1]}'
1

Более портативно, вы можете использовать match+ substrкак

awk 'match($0,/[0-9].WAP/) {print substr($0,RSTART,1)}'
0
27.01.2020, 22:17

Предполагая, что WAPможет встречаться только один раз в строке, я думаю, что это, вероятно, то, что вам действительно нужно. Учитывая этот входной файл:

$ cat file
,x,x,x,x,x,xx,Yes,7,WAP,no,x
,x,x,x,x,x,xx,Yes,3 WAP,no,x
,x,x,x,x,x,xx,Yes,"1 WAP",no,x

С GNU awk:

$ awk 'match($0,/([0-9])[^,]WAP/,a){print a[1]}' file
3
1

С любым awk:

$ awk 'match($0,/[0-9][^,]WAP/){print substr($0,RSTART,1)}' file
3
1
1
27.01.2020, 22:17

Теги

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