'G' 对 sed 命令有什么作用?
What does 'G' do with sed command?
我针对名为 "file1.txt" 的文本文件尝试了两种不同的 sed 命令。
我收到了相同的结果或输出。我不明白的是 "G" 选项的含义或用途!我在网上搜索过,但找不到好的答案!
这里有两个命令:
sed 'G' file1.txt
sed '/^$/d;G' file.txt
两个命令给出相同的输出!他们在文本文件中的每一行之间创建了一个行间距!
我知道'^$' 指的是什么都不包含的行,但是 'G' 是做什么的。例如,如果我输入 'd' 而不是 'G',所有空行都将被删除!
Instead of exchanging the hold space with the pattern space, you can copy the hold space to the pattern space with the "g" command. This deletes the pattern space. If you want to append to the pattern space, use the "G" command. This adds a new line to the pattern space, and copies the hold space after the new line.
也就是说,不是交换保留和模式 space(当前行等)的当前内容,而是可以采用模式 space 并将其复制到保留 space(替换或附加到进程中的保留 space)。
这两个 sed 命令属于您可以找到的惯用 sed 单行代码 online,它们只对某些文件(没有空行的文件)做同样的事情。
# double space a file
sed G
# double space a file which already has blank lines in it. Output file
# should contain no more than one blank line between lines of text.
sed '/^$/d;G'
根据man sed
:
g
将模式 space 的内容替换为保持 space 的内容。
G
在模式 space 的内容后附加一个换行符,然后
将 hold space 的内容附加到模式 space.
的内容
我针对名为 "file1.txt" 的文本文件尝试了两种不同的 sed 命令。 我收到了相同的结果或输出。我不明白的是 "G" 选项的含义或用途!我在网上搜索过,但找不到好的答案!
这里有两个命令:
sed 'G' file1.txt
sed '/^$/d;G' file.txt
两个命令给出相同的输出!他们在文本文件中的每一行之间创建了一个行间距! 我知道'^$' 指的是什么都不包含的行,但是 'G' 是做什么的。例如,如果我输入 'd' 而不是 'G',所有空行都将被删除!
Instead of exchanging the hold space with the pattern space, you can copy the hold space to the pattern space with the "g" command. This deletes the pattern space. If you want to append to the pattern space, use the "G" command. This adds a new line to the pattern space, and copies the hold space after the new line.
也就是说,不是交换保留和模式 space(当前行等)的当前内容,而是可以采用模式 space 并将其复制到保留 space(替换或附加到进程中的保留 space)。
这两个 sed 命令属于您可以找到的惯用 sed 单行代码 online,它们只对某些文件(没有空行的文件)做同样的事情。
# double space a file
sed G
# double space a file which already has blank lines in it. Output file
# should contain no more than one blank line between lines of text.
sed '/^$/d;G'
根据man sed
:
g
将模式 space 的内容替换为保持 space 的内容。
G
在模式 space 的内容后附加一个换行符,然后 将 hold space 的内容附加到模式 space.
的内容