阅读背景:

Shell Linux: grep精确句子,空字符

来源:互联网 

I have a file like

我有一个类似的文件

key

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"

但是grep似乎不理解\x0(\0相同的结果)。此外,我还要检查行以“$key\0”开头

I only can use sed grep and tr and other no maching commands

我只能使用sed grep和tr以及其他无maching命令

2 个解决方案

#1


1  

To have the \0 taken into account try :

考虑到\0请尝试:

  grep -Pa "^key\x0"

it works for me.

它适合我。

#2


1  

Using sed

sed will work:

sed将工作:

$ 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.

使用\x00来表示十六进制字符是sed的GNU扩展。由于这个问题被标记为linux,所以这不是问题。

Since the null character does not display well, one might (or might not) want to improve the display with something like this:

由于null字符不能很好地显示,因此有人可能(或者可能不会)希望通过以下方式改进显示:

$ 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:

如果键本身可以包含sed或shell活动字符,那么我们必须先转义它们,然后针对输入文件运行sed:

#!/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.

要使用此脚本,只需将键作为命令行上的第一个参数,当然,要用单引号括起来,以防止shell处理。

To see how it works, let's look at the pieces in turn:

为了了解它是如何工作的,让我们依次来看看这些片段:

  • sed 's:[]\[^$.*/]:\\&:g' <<<"$1"

    sed的:[]\[^ $。* /):\ \ & g ' < < < " $ 1 "

    This puts a backslash escape in front of all sed-active characters.

    这将在所有的sed活动字符前面加上反斜杠转义。

  • printf -v script '/^%s\\x00/p' "$(sed 's:[]\[^$.*/]:\\&:g' <<<"$1")"

    printf - v脚本' / ^ % s \ \ x00 / p”“$(sed’s:[]\[^ $。* /):\ \ & g”< < < " $ 1 ")

    This creates a sed command using the escaped key and stores it in the shell variable script.

    这将使用转义键创建一个sed命令,并将其存储在shell变量脚本中。

  • sed -n "$script" file

    sed - n " $脚本”文件

    This runs sed using the shell variable script as the sed command.

    这将使用shell变量脚本作为sed命令运行。

Using awk

The question states that awk is not an acceptable tool. For completeness, though, here is an awk solution:

问题指出awk不是一个可接受的工具。为了完整起见,这里有一个awk解决方案:

$ awk -F'\x00' -v k=key1 '$1 == k' file
key1value

Explanation:

解释:

  • -F'\x00'

    - 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.

    awk将输入分为记录(行),并将记录分为字段。在这里,我们将字段分隔符设置为空字符。因此,第一个字段(表示$1)是键。

  • -v k=key1

    - v k = key1

    This creates an awk variable, called k, and sets it to the key that we are looking for.

    这将创建一个awk变量,称为k,并将其设置为我们要查找的键。

  • $1 == k

    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.

    该语句查找第一个字段与指定键匹配的记录(行)。如果找到匹配项,则打印行。


value\n akey

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"

但是grep似乎不理解\x0(\0相同的结果)。此外,我还要检查行以“$key\0”开头

I only can use sed grep and tr and other no maching commands

我只能使用sed grep和tr以及其他无maching命令

2 个解决方案

#1


1  

To have the \0 taken into account try :

考虑到\0请尝试:

  grep -Pa "^key\x0"

it works for me.

它适合我。

#2


1  

Using sed

sed will work:

sed将工作:

$ 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.

使用\x00来表示十六进制字符是sed的GNU扩展。由于这个问题被标记为linux,所以这不是问题。

Since the null character does not display well, one might (or might not) want to improve the display with something like this:

由于null字符不能很好地显示,因此有人可能(或者可能不会)希望通过以下方式改进显示:

$ 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:

如果键本身可以包含sed或shell活动字符,那么我们必须先转义它们,然后针对输入文件运行sed:

#!/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.

要使用此脚本,只需将键作为命令行上的第一个参数,当然,要用单引号括起来,以防止shell处理。

To see how it works, let's look at the pieces in turn:

为了了解它是如何工作的,让我们依次来看看这些片段:

  • sed 's:[]\[^$.*/]:\\&:g' <<<"$1"

    sed的:[]\[^ $。* /):\ \ & g ' < < < " $ 1 "

    This puts a backslash escape in front of all sed-active characters.

    这将在所有的sed活动字符前面加上反斜杠转义。

  • printf -v script '/^%s\\x00/p' "$(sed 's:[]\[^$.*/]:\\&:g' <<<"$1")"

    printf - v脚本' / ^ % s \ \ x00 / p”“$(sed’s:[]\[^ $。* /):\ \ & g”< < < " $ 1 ")

    This creates a sed command using the escaped key and stores it in the shell variable script.

    这将使用转义键创建一个sed命令,并将其存储在shell变量脚本中。

  • sed -n "$script" file

    sed - n " $脚本”文件

    This runs sed using the shell variable script as the sed command.

    这将使用shell变量脚本作为sed命令运行。

Using awk

The question states that awk is not an acceptable tool. For completeness, though, here is an awk solution:

问题指出awk不是一个可接受的工具。为了完整起见,这里有一个awk解决方案:

$ awk -F'\x00' -v k=key1 '$1 == k' file
key1value

Explanation:

解释:

  • -F'\x00'

    - 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.

    awk将输入分为记录(行),并将记录分为字段。在这里,我们将字段分隔符设置为空字符。因此,第一个字段(表示$1)是键。

  • -v k=key1

    - v k = key1

    This creates an awk variable, called k, and sets it to the key that we are looking for.

    这将创建一个awk变量,称为k,并将其设置为我们要查找的键。

  • $1 == k

    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.

    该语句查找第一个字段与指定键匹配的记录(行)。如果找到匹配项,则打印行。


value\n key2

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"

但是grep似乎不理解\x0(\0相同的结果)。此外,我还要检查行以“$key\0”开头

I only can use sed grep and tr and other no maching commands

我只能使用sed grep和tr以及其他无maching命令

2 个解决方案

#1


1  

To have the \0 taken into account try :

考虑到\0请尝试:

  grep -Pa "^key\x0"

it works for me.

它适合我。

#2


1  

Using sed

sed will work:

sed将工作:

$ 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.

使用\x00来表示十六进制字符是sed的GNU扩展。由于这个问题被标记为linux,所以这不是问题。

Since the null character does not display well, one might (or might not) want to improve the display with something like this:

由于null字符不能很好地显示,因此有人可能(或者可能不会)希望通过以下方式改进显示:

$ 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:

如果键本身可以包含sed或shell活动字符,那么我们必须先转义它们,然后针对输入文件运行sed:

#!/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.

要使用此脚本,只需将键作为命令行上的第一个参数,当然,要用单引号括起来,以防止shell处理。

To see how it works, let's look at the pieces in turn:

为了了解它是如何工作的,让我们依次来看看这些片段:

  • sed 's:[]\[^$.*/]:\\&:g' <<<"$1"

    sed的:[]\[^ $。* /):\ \ & g ' < < < " $ 1 "

    This puts a backslash escape in front of all sed-active characters.

    这将在所有的sed活动字符前面加上反斜杠转义。

  • printf -v script '/^%s\\x00/p' "$(sed 's:[]\[^$.*/]:\\&:g' <<<"$1")"

    printf - v脚本' / ^ % s \ \ x00 / p”“$(sed’s:[]\[^ $。* /):\ \ & g”< < < " $ 1 ")

    This creates a sed command using the escaped key and stores it in the shell variable script.

    这将使用转义键创建一个sed命令,并将其存储在shell变量脚本中。

  • sed -n "$script" file

    sed - n " $脚本”文件

    This runs sed using the shell variable script as the sed command.

    这将使用shell变量脚本作为sed命令运行。

Using awk

The question states that awk is not an acceptable tool. For completeness, though, here is an awk solution:

问题指出awk不是一个可接受的工具。为了完整起见,这里有一个awk解决方案:

$ awk -F'\x00' -v k=key1 '$1 == k' file
key1value

Explanation:

解释:

  • -F'\x00'

    - 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.

    awk将输入分为记录(行),并将记录分为字段。在这里,我们将字段分隔符设置为空字符。因此,第一个字段(表示$1)是键。

  • -v k=key1

    - v k = key1

    This creates an awk variable, called k, and sets it to the key that we are looking for.

    这将创建一个awk变量,称为k,并将其设置为我们要查找的键。

  • $1 == k

    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.

    该语句查找第一个字段与指定键匹配的记录(行)。如果找到匹配项,则打印行。


value\n key

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"

但是grep似乎不理解\x0(\0相同的结果)。此外,我还要检查行以“$key\0”开头

I only can use sed grep and tr and other no maching commands

我只能使用sed grep和tr以及其他无maching命令

2 个解决方案

#1


1  

To have the \0 taken into account try :

考虑到\0请尝试:

  grep -Pa "^key\x0"

it works for me.

它适合我。

#2


1  

Using sed

sed will work:

sed将工作:

$ 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.

使用\x00来表示十六进制字符是sed的GNU扩展。由于这个问题被标记为linux,所以这不是问题。

Since the null character does not display well, one might (or might not) want to improve the display with something like this:

由于null字符不能很好地显示,因此有人可能(或者可能不会)希望通过以下方式改进显示:

$ 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:

如果键本身可以包含sed或shell活动字符,那么我们必须先转义它们,然后针对输入文件运行sed:

#!/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.

要使用此脚本,只需将键作为命令行上的第一个参数,当然,要用单引号括起来,以防止shell处理。

To see how it works, let's look at the pieces in turn:

为了了解它是如何工作的,让我们依次来看看这些片段:

  • sed 's:[]\[^$.*/]:\\&:g' <<<"$1"

    sed的:[]\[^ $。* /):\ \ & g ' < < < " $ 1 "

    This puts a backslash escape in front of all sed-active characters.

    这将在所有的sed活动字符前面加上反斜杠转义。

  • printf -v script '/^%s\\x00/p' "$(sed 's:[]\[^$.*/]:\\&:g' <<<"$1")"

    printf - v脚本' / ^ % s \ \ x00 / p”“$(sed’s:[]\[^ $。* /):\ \ & g”< < < " $ 1 ")

    This creates a sed command using the escaped key and stores it in the shell variable script.

    这将使用转义键创建一个sed命令,并将其存储在shell变量脚本中。

  • sed -n "$script" file

    sed - n " $脚本”文件

    This runs sed using the shell variable script as the sed command.

    这将使用shell变量脚本作为sed命令运行。

Using awk

The question states that awk is not an acceptable tool. For completeness, though, here is an awk solution:

问题指出awk不是一个可接受的工具。为了完整起见,这里有一个awk解决方案:

$ awk -F'\x00' -v k=key1 '$1 == k' file
key1value

Explanation:

解释:

  • -F'\x00'

    - 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.

    awk将输入分为记录(行),并将记录分为字段。在这里,我们将字段分隔符设置为空字符。因此,第一个字段(表示$1)是键。

  • -v k=key1

    - v k = key1

    This creates an awk variable, called k, and sets it to the key that we are looking for.

    这将创建一个awk变量,称为k,并将其设置为我们要查找的键。

  • $1 == k

    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.

    该语句查找第一个字段与指定键匹配的记录(行)。如果找到匹配项,则打印行。


value\n a



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: