Shell Linux:具有NULL字符的grep精确句子 [英] Shell Linux : grep exact sentence with NULL character
问题描述
我有一个文件,如
key \0value\\\
akey\0value\\\
key2\0value\\\
我必须创建一个脚本作为参数一个字。
我试过了
grep -aF$ key \x0
但grep似乎不明白\x0(\0相同的结果)。此外,我必须检查以$ key \0开头的行
我只能使用sed grep和tr以及其他不需要的加工命令
使用 sed
sed
会工作:
$ sed -n'/ ^ key1 \x00 / p'文件
key1value
使用 \x00
代表一个十六进制字符,它是对 sed
的GNU扩展。由于这个问题被标记为 linux
,所以这不是问题。
由于null字符显示不好,有人可能(或可能不想)用这样的东西改善显示效果:
$ sed -n's / ^ \\ \\(akey \)\x00 / \ 1 - > / p'文件
akey - >值
使用 sed
和包含特殊字符的键
如果键本身可以包含sed或shell活动字符,那么我们必须首先转义它们,然后针对输入文件运行 sed
:
#!/ bin / bash
printf -v script'/ ^%s\\x00 / p'$(sed's:[] \ [^ $。$ / *:\\&:g'<<<$ 1)
sed -n$ scriptfile
要使用此脚本,只需将该键作为命令行中的第一个参数提供,并用单引号括起来,以防止shell处理。 / p>
看看如何它的作品,让我们依次看看这些作品:
-
sed's:[] \ [^ $。* /]:\\&:g'<
-
printf -v脚本'/ ^%s \\x00所有活跃字符前面的反斜杠转义
/ p'$(sed's:[] \ [^ $。* /]:\\&:g'<<lt;$ 1) / p> -
sed -n$ scriptfile
使用shell变量
script
作为<$>运行sed
c $ c> sed 命令。 使用 -
-F'\x00'
awk
将输入分成记录(行)并将记录分成多个字段。在这里,我们将字段分隔符设置为空字符。因此,表示为$ 1
的第一个字段是关键。 这将创建awk
变量,称为k
,并将其设置为我们正在查找的键。 -
$ 1 == k
对于第一个字段与我们指定的键匹配的记录(行)。如果找到匹配,则打印该行。
sed 's:[]\[^$.*/]:\\&:g' <<<"$1"
This puts a backslash escape in front of all sed-active characters.
printf -v script '/^%s\\x00/p' "$(sed 's:[]\[^$.*/]:\\&:g' <<<"$1")"
This creates a
sed
command using the escaped key and stores it in the shell variablescript
.sed -n "$script" file
This runs
sed
using the shell variablescript
as thesed
command.-F'\x00'
awk
divides the input up into records (lines) and divides the records up into fields. Here, we set the field separator to the null character. Consequently, the first field, denoted$1
, is the key.-v k=key1
This creates an
awk
variable, calledk
, and sets it to the key that we are looking for.$1 == k
This statement looks for records (lines) for which the first field matches our specified key. If a match is found, the line is printed.
使用转义键创建 sed
命令并将其存储在shell变量脚本中
。
awk / code>
这个问题表明 awk
不是一个可接受的工具。尽管如此,以下是一个 awk
解决方案:
$ awk -F'\x00'-vk = key1'$ 1 == k'file
key1value
<解释:
I have a file like
key\0value\n
akey\0value\n
key2\0value\n
I have to create a script that take as argument a word. I have to return every lines having a key exactly the same than the argument.
I tried
grep -aF "$key\x0"
but grep seems to do not understand the \x0 (\0 same result). Futhermore, I have to check that the line begins with "$key\0"
I only can use sed grep and tr and other no maching commands
解决方案 Using sed
sed
will work:
$ sed -n '/^key1\x00/p' file
key1value
The use of \x00
to represent a hex character is a GNU extension to sed
. Since this question is tagged linux
, that is not a problem.
Since the null character does not display well, one might (or might not) want to improve the display with something like this:
$ sed -n 's/^\(akey\)\x00/\1-->/p' file
akey-->value
Using sed
with keys that contain special characters
If the key itself can contain sed or shell active characters, then we must escape them first and then run sed
against the input file:
#!/bin/bash
printf -v script '/^%s\\x00/p' "$(sed 's:[]\[^$.*/]:\\&:g' <<<"$1")"
sed -n "$script" file
To use this script, simply supply the key as the first argument on the command line, enclosed in single-quotes, of course, to prevent shell processing.
To see how it works, let's look at the pieces in turn:
Using awk
The question states that awk
is not an acceptable tool. For completeness, though, here is an awk
solution:
$ awk -F'\x00' -v k=key1 '$1 == k' file
key1value
Explanation:
这篇关于Shell Linux:具有NULL字符的grep精确句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!