![일치 후 문자열 검색](https://linux55.com/image/179477/%EC%9D%BC%EC%B9%98%20%ED%9B%84%20%EB%AC%B8%EC%9E%90%EC%97%B4%20%EA%B2%80%EC%83%89.png)
주어진 일치 항목 이후에 문자열을 검색하는 방법이 있습니까?
예를 들어 를 실행하면 dmidecode
많은 정보가 제공됩니다. 예를 들어
BIOS Information
Vendor: ABCD
Version: 123456(V1.01)
Release Date: 01/01/1970
Address: 0xE0000
Runtime Size: 128 kB
ROM Size: 8192 kB
Characteristics:
PCI is supported
BIOS is upgradeable
BIOS shadowing is allowed
Boot from CD is supported
Selectable boot is supported
BIOS ROM is socketed
EDD is supported
Japanese floppy for NEC 9800 1.2 MB is supported (int 13h)
Japanese floppy for Toshiba 1.2 MB is supported (int 13h)
5.25"/360 kB floppy services are supported (int 13h)
5.25"/1.2 MB floppy services are supported (int 13h)
3.5"/720 kB floppy services are supported (int 13h)
3.5"/2.88 MB floppy services are supported (int 13h)
8042 keyboard services are supported (int 9h)
CGA/mono video services are supported (int 10h)
ACPI is supported
USB legacy is supported
Targeted content distribution is supported
UEFI is supported
BIOS Revision: 1.21
Firmware Revision: 1.21
이제 "version"을 grep하면 dmidecode
출력에서 다양한 일치 항목을 얻을 수 있습니다. 대신 ^BIOS 단어 다음에 "version" 줄을 검색하고 첫 번째 일치 항목에서 중지할 수 있는 방법이 있습니까? 따라서 출력은 다음과 같습니다.
Version: 123456(V1.01)
답변1
$ sudo dmidecode |
awk '/^BIOS/ { ++Bios } Bios && /Version/ { print; exit; }'
Version: 02PI.M505.20110824.LEO
$
BIOS가 수행한 작업을 계산하고 버전 라인에서 트리거합니다.
BIOS가 내 시스템의 첫 번째 블록이고 grep에 최대 개수 옵션이 있으므로 이 방법도 작동합니다.
sudo dmidecode | grep -m 1 'Version'
답변2
대부분의 경우 grep -A
첫 번째 일치 후에 일부 줄을 출력한 다음 해당 결과를 다시 grep할 수 있습니다.
dmidecode | grep -A 3 "BIOS Information" | grep "Version"