Я хочу вырезать некоторые части двоичного файла, начиная с определенного шаблона и заканчивая определенным шаблоном. Как мне сделать это в Linux с помощью bash. Можно ли это сделать с помощью sed или awk?
У меня есть файл, подобный приведенному ниже.
bd = 0x422e90e0ff4abc00 pad = 0x82 смещение = 0x05 dst = 00: 0d: bc: 03: 6d: 80 src = 00: 50: a2: df: e8: 1c etype = 0x0800 ip: version = 4 headerwords = 5 tos = 32 длина = 142 ip: id = 201 flags = 0x2 fragmentoffset = 0 ip: ttl = 117 протокол = 6 контрольная сумма = 0x0000 ip: sourceaddress = 36.190.253.236 ip: destinaddress = 125.182.162.162 bd = 0x422e90e0ff61f000 pad = 0x92 offset = 0x19 dst = 00 : 50: a2: df: e8: 1c src = 00: 0d: bc: 03: 6d: 80 etype = 0x0800 ip: version = 4 headerwords = 5 tos = 0 length = 40 ip: id = 11084 flags = 0x2 fragmentoffset = 0 ip: ttl = 62 протокол = 6 контрольная сумма = 0x0000 ip: sourceaddress = 125.182.162.162 ip: destinaddress = 36.190.253.236 bd = 0x422e90e0ff6bb900 pad = 0xb8 смещение = 0x06 dst = 00: 50: a2: df: e8: 1c src = 00: 0d: bc: 03: 6d: 80 etype = 0x0800 ip: version = 4 headerwords = 5 tos = 0 length = 40 ip: id = 15720 flags = 0x2 fragmentoffset = 0 ip: ttl = 62 protocol = 6 контрольная сумма = 0x0000 ip: sourceaddress = 125.182.162.162 ip: destinaddress = 36.190.253.236 bd = 0x422e90e0ffbe9a00 pad = 0xbb смещение = 0xc5 dst = 00: 50: a2: df: e8: 1c src = 00: 0d: bc: 03: 6d: 80 etype = 0x0800 ip: версия = 4 слова заголовка = 5 tos = 0 length = 186 ip: id = 15722 flags = 0x2 fragmentoffset = 0 ip: ttl = 62 protocol = 6 контрольная сумма = 0x0000 ip: sourceaddress = 125.182.162.162 ip: destinaddress = 36.190.253.236
Мне нужно вырезать этот файл с bd = 0x422e90e0ff61f000 до bd = 0x422e90e0ffbe9a00. Это можно сделать с помощью sed или awk.
Что-то вроде этого? (У меня есть ваш блок текста в файле foo.txt
)
$ cat foo.awk
BEGIN {
RS = "bd=[a-fx0-9]* ";
FS = "";
}
{
print $0;
}
$ awk -f foo.awk foo.txt
pad=0x82 offset=0x05 dst=00:0d:bc:03:6d:80 src=00:50:a2:df:e8:1c etype=0x0800 ip: version=4 headerwords=5 tos=32 length=142 ip: id=201 flags=0x2 fragmentoffset=0 ip: ttl=117 protocol=6 checksum=0x0000 ip: sourceaddress=36.190.253.236 ip: destinaddress=125.182.162.162
pad=0x92 offset=0x19 dst=00:50:a2:df:e8:1c src=00:0d:bc:03:6d:80 etype=0x0800 ip: version=4 headerwords=5 tos=0 length=40 ip: id=11084 flags=0x2 fragmentoffset=0 ip: ttl=62 protocol=6 checksum=0x0000 ip: sourceaddress=125.182.162.162 ip: destinaddress=36.190.253.236
pad=0xb8 offset=0x06 dst=00:50:a2:df:e8:1c src=00:0d:bc:03:6d:80 etype=0x0800 ip: version=4 headerwords=5 tos=0 length=40 ip: id=15720 flags=0x2 fragmentoffset=0 ip: ttl=62 protocol=6 checksum=0x0000 ip: sourceaddress=125.182.162.162 ip: destinaddress=36.190.253.236
pad=0xbb offset=0xc5 dst=00:50:a2:df:e8:1c src=00:0d:bc:03:6d:80 etype=0x0800 ip: version=4 headerwords=5 tos=0 length=186 ip: id=15722 flags=0x2 fragmentoffset=0 ip: ttl=62 protocol=6 checksum=0x0000 ip: sourceaddress=125.182.162.162 ip: destinaddress=36.190.253.236
Итак, в основном: измените разделитель полей на null и измените разделитель записей на этот шаблон «bd =» и распечатайте все в записи.