在Shell的第一个null字符之后,如何提取文件中的所有内容



我有一个看起来像这样的文件: some ascii stuffsome more ascii stuffand a little more ascii stuff

我想在第一个之后提取所有内容。因此,此过程之后我的输出是some more ascii stuffand a little more ascii stuff

我该怎么做?这是在Initramfs中完成的,因此我对命令的访问受到限制。我确实有cutgrepawk,我一直在尝试上班,但我只是没有运气。

此utils主要是Busybox,shell

sh

很容易完成,只有外壳内置(嗯,cat不是内置的),但是您可以用流的实际预期消费者代替它):

{ IFS= read -r -d '' _; cat; } <yourfile

read -d ''一次读取所有内容,一次是一个字节,直到stdin上的第一个nul。因此,该流的剩下的是之后的所有内容

之后

您可以按以下方式进行测试:

printf '%s' one two three | { IFS= read -r -d '' _; hexdump -C; }

...适当排放:

00000000  74 77 6f 00 74 68 72 65  65 00                    |two.three.|
0000000a

如果您有GREP,则很可能还具有SED。这对我有用:

echo -e "one00two00three" | sed 's/[^o000]*o000//'

使用gnu awk您可以做到这一点:

awk -F '\0' 'NR == 1{sub($1 FS, "")} 1' file

some more ascii stuffand a little more ascii stuff

od -c验证输出:

awk -F '\0' 'NR == 1{sub($1 FS, "")} 1' file | od -c

0000000   s   o   m   e       m   o   r   e       a   s   c   i   i
0000020   s   t   u   f   f     a   n   d       a       l   i   t   t
0000040   l   e       m   o   r   e       a   s   c   i   i       s   t
0000060   u   f   f    n
0000065

我将使用 perl

perl -n0e 'print unless $.==1'

-0将记录分隔符设置为空字节,而打印则打印除了第一个记录以外的所有内容。

是否适合您,这取决于您当时有可用的尴尬版本...这对我有用,w/gnu awk 4.1.3

echo -e 'some ascii stuffsome more ascii stuffand a little more ascii stuff'| awk 'BEGIN{RS="";ORS="t"} NR>1{print $0}'
some more ascii stuff   and a little more ascii stuff

最新更新