Sed 添加到每个文件的行尾
我有25000个.txt文件,所有这些文件都遵循以下模式:Sed 添加到每个文件的行尾,sed,Sed,我有25000个.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 eve
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
有谁能告诉我如何在当前目录中的每个文件和所有带有sed的子目录的第1行末尾添加一个空格,在第2、3和4行末尾添加一个右括号,在第5行末尾添加一个右括号?从今天起,我还不熟悉通过终端批量编辑文本文件,尽管我花了几个小时寻找解决方案,但似乎不知道如何完成修改这些文件内容的最后一步
感谢您提供任何想法、解决方案或有用的链接
皮特·伍德
(使用Debian 7)以下命令将请求的字符添加到请求行的末尾。(我不确定“复制括号”是什么,所以我使用了
]
):
工作原理
1s/$/
告诉1
仅将此替换应用于行末尾的行sed
$
s`命令在行尾添加一个空格 替换命令的格式为匹配项。
。它们在s/old/new/
文本上匹配,并将其替换为旧的
。在本例中,我们希望在一行末尾匹配,表示为新的
,并在那里放置一个空格$
2,4s/$/)/
告诉2,4
仅对2到4之间的行应用此替换。它在行尾添加一个sed
)
5s/$/\]/
告诉5
仅对第5行应用此替换。它在行尾添加一个sed
。由于]
处于激活状态,因此必须对其进行转义]
.bak
的备份副本,请使用:
sed -i.bak '1s/$/ /; 2,4s/$/)/; 5s/$/\]/' *.txt
这是一个
awk
版本:
awk 'NR==1 {$0=$0" "} NR>1 && NR<5 {$0=$0")"} NR==5 {$0=$0"]"} 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]
awk'NR==1{$0=$0”“}NR>1&&NR感谢John的代码,特别是感谢您花额外的时间带我浏览它-我将剪切并粘贴它以供将来参考。当我把所有的文件放在同一个文件夹中,几乎没有花时间时,这真是一种享受。。。。比我打算在旧XP机器上运行的AHK脚本要好得多,而旧XP机器要花87个小时才能完成同样的任务!感谢您的替代解决方案!
awk 'NR==1 {$0=$0" "} NR>1 && NR<5 {$0=$0")"} NR==5 {$0=$0"]"} 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]