添加到每个文件的行尾
add to the end of a line in every file
我有 25,000 个 .txt 文件,它们都统一遵循此模式:
String found only at the start of line 1 in every file :Variable text with no pattern
String found only at the start of line 2 in every file :(Variable text with no pattern
String found only at the start of line 3 in every file :(Variable text with no pattern
String found only at the start of line 4 in every file :(Variable text with no pattern
String found only at the start of line 5 in every file :[Variable text with no pattern
谁能告诉我如何在第 1 行的末尾添加一个空的 space,在第 2、3 和 4 行的末尾添加一个右括号,以及一个关闭副本使用 sed 将当前目录和所有子目录中的每个文件的第 5 行末尾括起来?截至今天,我是通过终端批量编辑文本文件的新手,尽管花了几个小时寻找解决方案,但似乎无法弄清楚如何完成修改这些文件内容的最后一步。
感谢您提供任何想法、解决方案或有用的链接
彼得伍德
(使用 Debian 7)
下面将请求的字符添加到请求的行的末尾。 (我不确定 "copy bracket" 是什么,所以我使用了 ]
):
$ sed '1s/$/ /; 2,4s/$/)/; 5s/$/\]/' file.txt
String found only at the start of line 1 in every file :Variable text with no pattern
String found only at the start of line 2 in every file :(Variable text with no pattern)
String found only at the start of line 3 in every file :(Variable text with no pattern)
String found only at the start of line 4 in every file :(Variable text with no pattern)
String found only at the start of line 5 in every file :[Variable text with no pattern]
工作原理
1s/$/ /
1
告诉 sed
仅将此替换应用于行 .
$matches at the end of a line. The
s` 命令在行的末尾添加一个 space线.
替换命令的格式为 s/old/new/
。它们匹配 old
文本并将其替换为 new
。在我们的例子中,我们希望在一行的末尾匹配,表示为 $
,并在那里放置一个 space。
2,4s/$/)/
2,4
告诉 sed
仅将此替换应用于 2 到 4 范围内的行。它会在行尾添加一个 )
。
5s/$/\]/
5
告诉 sed
仅将此替换应用于第 5 行。它在行尾添加了一个 ]
。因为]
是sed-active,所以必须转义。
将其应用于您的所有文件
要就地更改所有文件,制作扩展名为 .bak
的备份副本,请使用:
sed -i.bak '1s/$/ /; 2,4s/$/)/; 5s/$/\]/' *.txt
这是一个 awk
版本:
awk 'NR==1 {[=10=]=[=10=]" "} NR>1 && NR<5 {[=10=]=[=10=]")"} NR==5 {[=10=]=[=10=]"]"} 1' files*
String found only at the start of line 1 in every file :Variable text with no pattern
String found only at the start of line 2 in every file :(Variable text with no pattern)
String found only at the start of line 3 in every file :(Variable text with no pattern)
String found only at the start of line 4 in every file :(Variable text with no pattern)
String found only at the start of line 5 in every file :[Variable text with no pattern]
它只是使用 NR
(行号)逐行测试并添加所需内容。
最后的1
用于打印修改后的行。
我有 25,000 个 .txt 文件,它们都统一遵循此模式:
String found only at the start of line 1 in every file :Variable text with no pattern
String found only at the start of line 2 in every file :(Variable text with no pattern
String found only at the start of line 3 in every file :(Variable text with no pattern
String found only at the start of line 4 in every file :(Variable text with no pattern
String found only at the start of line 5 in every file :[Variable text with no pattern
谁能告诉我如何在第 1 行的末尾添加一个空的 space,在第 2、3 和 4 行的末尾添加一个右括号,以及一个关闭副本使用 sed 将当前目录和所有子目录中的每个文件的第 5 行末尾括起来?截至今天,我是通过终端批量编辑文本文件的新手,尽管花了几个小时寻找解决方案,但似乎无法弄清楚如何完成修改这些文件内容的最后一步。
感谢您提供任何想法、解决方案或有用的链接 彼得伍德 (使用 Debian 7)
下面将请求的字符添加到请求的行的末尾。 (我不确定 "copy bracket" 是什么,所以我使用了 ]
):
$ sed '1s/$/ /; 2,4s/$/)/; 5s/$/\]/' file.txt
String found only at the start of line 1 in every file :Variable text with no pattern
String found only at the start of line 2 in every file :(Variable text with no pattern)
String found only at the start of line 3 in every file :(Variable text with no pattern)
String found only at the start of line 4 in every file :(Variable text with no pattern)
String found only at the start of line 5 in every file :[Variable text with no pattern]
工作原理
1s/$/ /
1
告诉sed
仅将此替换应用于行.
$matches at the end of a line. The
s` 命令在行的末尾添加一个 space线.替换命令的格式为
s/old/new/
。它们匹配old
文本并将其替换为new
。在我们的例子中,我们希望在一行的末尾匹配,表示为$
,并在那里放置一个 space。2,4s/$/)/
2,4
告诉sed
仅将此替换应用于 2 到 4 范围内的行。它会在行尾添加一个)
。5s/$/\]/
5
告诉sed
仅将此替换应用于第 5 行。它在行尾添加了一个]
。因为]
是sed-active,所以必须转义。
将其应用于您的所有文件
要就地更改所有文件,制作扩展名为 .bak
的备份副本,请使用:
sed -i.bak '1s/$/ /; 2,4s/$/)/; 5s/$/\]/' *.txt
这是一个 awk
版本:
awk 'NR==1 {[=10=]=[=10=]" "} NR>1 && NR<5 {[=10=]=[=10=]")"} NR==5 {[=10=]=[=10=]"]"} 1' files*
String found only at the start of line 1 in every file :Variable text with no pattern
String found only at the start of line 2 in every file :(Variable text with no pattern)
String found only at the start of line 3 in every file :(Variable text with no pattern)
String found only at the start of line 4 in every file :(Variable text with no pattern)
String found only at the start of line 5 in every file :[Variable text with no pattern]
它只是使用 NR
(行号)逐行测试并添加所需内容。
最后的1
用于打印修改后的行。