[英]“tailing” a binary file based on string location using bash?
I've got a bunch of binary files, each containing an embedded string near the end of the file but at different places (only occurs once in each file). 我有一堆二进制文件,每个文件都包含一个嵌入字符串靠近文件末尾但在不同的地方(每个文件只出现一次)。 I need to extract the part of the file starting at the location of the string till the end of the file and dump it into a new file.
我需要从字符串的位置开始提取文件的一部分,直到文件的末尾并将其转储到新文件中。
eg. 例如。 If the file's contents is "AWREDEDEDEXXXERESSDSDS" and the string of interest is "XXX", then the part of the file I need is "XXXERESSDSDS".
如果文件的内容是“AWREDEDEDEXXXERESSDSDS”并且感兴趣的字符串是“XXX”,那么我需要的文件部分是“XXXERESSDSDS”。
What's the easiest way to do this in bash? 在bash中最简单的方法是什么?
In PERL, there is a variable built in that specifically refers to the part of the string after the matched regular expression. 在PERL中,有一个内置的变量,专门引用匹配的正则表达式后的字符串部分。 That would be the method I would use.
这将是我将使用的方法。 It is not just Bash and utilities, but PERL is so commonly installed that you should be OK.
它不只是Bash和实用程序,但PERL是如此常见的安装,你应该没问题。
Following is a small hack shell solution that is not very performant. 以下是一个不太高效的小型hack shell解决方案。 But it works.
但它的确有效。
Write the script file tail.sh
as follows: 编写脚本文件
tail.sh
如下:
#!/bin/sh
dd bs=1 if=$1 of=$2 skip=`grep --binary-files=text -m1 -b -o $3 $1 | cut -d ':' -f 1 | head -1`
Call tail.sh INPUTNAME OUTPUTNAME PATTERN 调用tail.sh INPUTNAME OUTPUTNAME PATTERN
ps: sorry forgot one option to grep in first post ps:抱歉忘记了第一篇文章中grep的一个选项
Would strings
and grep
do you want? 你想要
strings
和grep
吗?
eg 例如
strings -n 3 myfilename | grep XXX
strings -n3 file_binary | awk '/XXX/{gsub(/.*XXX/,"");print}'
I came up with this solution: 我想出了这个解决方案:
ls -1 *.bin | xargs strings -n4 --radix=d -f | grep "string" | awk '{sub(/:/, ""); print $2 " " $1 " " $1".";}' | xargs -l1 split -b && rm *.aa
ls -1 *.bin Print only the filenames with the extension "bin" in a list format ls -1 * .bin 仅以列表格式打印扩展名为“bin”的文件名
xargs strings -n4 --radix=d -f List all the strings in the file and their positions and include the filename in the output xargs strings -n4 --radix = d -f 列出文件中的所有字符串及其位置,并在输出中包含文件名
grep "string" Print lines containing "string" (it only occurs once in each file) grep“string” 打印包含“string”的行(每个文件只出现一次)
awk '{sub(/:/, ""); awk'{sub(/:/,“”); print $2 " " $1 " " $1".";}' Remove the colon after the filename added by strings, and print the position of the string, the filename, and the filename with a period (this line is used as the arguments for the split command
print $ 2“”$ 1“”$ 1“。”;}' 在字符串添加文件名后删除冒号,并用句点打印字符串的位置,文件名和文件名(此行用作参数split命令
xargs -l1 split -b Execute the split command for each line using the output of awk as the rest of the arguments xargs -l1 split -b 使用awk的输出作为其余参数,为每一行执行split命令
rm *.aa Delete the first parts of the split files. rm * .aa 删除拆分文件的第一部分。 "aa" is the default suffix for the part of the split files.
“aa”是拆分文件部分的默认后缀。
There are probably better/faster/safer ways of doing this but it's fine for my purposes. 可能有更好/更快/更安全的方法,但这对我的目的来说很好。
Try this: 尝试这个:
grep -ao string.* filename
Since you have binary data, you might want to redirect the output to a file. 由于您有二进制数据,因此您可能希望将输出重定向到文件。
grep -ao string.* filename > binary.out
Or pipe it through hexdump
or similar for testing: 或者通过
hexdump
或类似方法将其管道进行测试:
grep -ao string.* filename | hd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.