Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sed 添加到每个文件的行尾_Sed - Fatal编程技术网

Sed 添加到每个文件的行尾

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

我有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 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
    告诉
    sed
    仅对2到4之间的行应用此替换。它在行尾添加一个

  • 5s/$/\]/

    5
    告诉
    sed
    仅对第5行应用此替换。它在行尾添加一个
    ]
    。由于
    ]
    处于激活状态,因此必须对其进行转义

将其应用于所有文件 要更改所有文件的位置,制作扩展名为
.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]