0

I have several files with this line:

<2-10 digits> ; word

I want to replace all of the digits that come before that word with something else. How can I do that?

4

2 回答 2

2
 sed -i -e 's/.*word/something;word/g' <filename>

循环遍历目录中的多个文件。我假设 .txt 文件作为文件扩展名:

for i in `\ls -1 *.txt`
  do
    sed -i -e 's/.*word/something;word/g' $i
done

注意:sed -i将以交互方式修改文件。因此,在没有-i选项的情况下测试命令以检查这是您想要的,然后继续执行...

于 2013-08-06T22:04:56.027 回答
1

更新: sed 示例:

s="999 abc 1234 ; word 567"
echo $s | sed 's/^\(.* \)[0-9][0-9]*\( ; word.*\)$/\1something\2/g'

输出:

999 abc something ; word 567
于 2013-08-06T21:29:13.563 回答