小编use*_*338的帖子

用顺序索引替换字符串

有人可以提出一种优雅的方法来实现这一目标吗?

输入:

test  instant  ()

test  instant  ()

...
test  instant  ()    //total 1000 lines
Run Code Online (Sandbox Code Playgroud)

输出应该是:

test      instant1  ()

test      instant2  ()

test      instant1000()
Run Code Online (Sandbox Code Playgroud)

空行在我的输入文件中,同一目录下有许多文件需要同时处理。

我试图用这个来替换同一个目录中的许多文件,但没有用。

test  instant  ()

test  instant  ()

...
test  instant  ()    //total 1000 lines
Run Code Online (Sandbox Code Playgroud)

错误:

Substitution replacement not terminated at -e line 1.
Substitution replacement not terminated at -e line 1.
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

test      instant1  ()

test      instant2  ()

test      instant1000()
Run Code Online (Sandbox Code Playgroud)

它起作用了,但索引只是从一个文件增加到另一个文件。我想在更改为新文件时将其重置为 1。有什么好的建议吗?

for file in ./*; do perl -i -000pe 's/instance$& . ++$n/ge' "$file"; done …
Run Code Online (Sandbox Code Playgroud)

shell sed awk perl text-processing

10
推荐指数
2
解决办法
9863
查看次数

模式匹配时如何将文本添加到行尾?

输入:

line1 with the PATTERN that contains ( ) 
line2 with the PATTERN that contains ( ) 
lineN with the PATTERN that contains ( ) 
Run Code Online (Sandbox Code Playgroud)

输出:

line1 with the PATTERN that contains ( ) ;
line2 with the PATTERN that contains ( ) ;
...
lineN with the PATTERN that contains ( ) ;
Run Code Online (Sandbox Code Playgroud)

我试过这个:

find . -name "test.txt" -print | xargs sed -i "/PATTERN/ s/$)/); /g"
Run Code Online (Sandbox Code Playgroud)

但它没有用。

shell sed line-editor

8
推荐指数
1
解决办法
2万
查看次数

标签 统计

sed ×2

shell ×2

awk ×1

line-editor ×1

perl ×1

text-processing ×1