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?
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
选项的情况下测试命令以检查这是您想要的,然后继续执行...
更新: sed 示例:
s="999 abc 1234 ; word 567"
echo $s | sed 's/^\(.* \)[0-9][0-9]*\( ; word.*\)$/\1something\2/g'
输出:
999 abc something ; word 567