0

私は多くの異なるディレクトリに何百もの[ほとんど異なる]ファイルを持っており、それらはすべて同じ5行のテキストを含んでおり、頻繁に編集する必要があります。例:

/home/blah.txt
/home/hello/superman.txt
/home/hello/dreams.txt
/home/55/instruct.txt
and so on...

5行のテキストは順番に並んでいますが、すべての.txtファイルの異なる場所から始まります。例:

/home/blah.txt内:

line 400 this is line 1
line 401 this is line 2
line 402 this is line 3
line 403 this is line 4
line 404 this is line 5

/home/hello/superman.txt:

line 23 this is line 1
line 24 this is line 2
line 25 this is line 3
line 26 this is line 4
line 27 this is line 5

すべての.txtファイルでこれらの5行のテキストを見つけて置き換えるにはどうすればよいですか?

4

2 に答える 2

5

ステップ1:問題のすべてのファイルでvimを開きます。たとえば、zshellを使用すると、次のことができます。

vim **/*.txt

必要なファイルが現在のツリーの下の任意の場所にある.txtファイルであると想定します。または、1行のスクリプトを作成して、必要なすべてのファイルを開きます(「vimdir1 / file1 dir2 / file2 ...」のようになります)。

ステップ2:vimで、次のことを行います。

:bufdo %s/this is line 1/this is the replacement for line 1/g | w 
:bufdo %s/this is line 2/this is the replacement for line 2/g | w 
...

bufdoコマンドは、開いているすべてのバッファーでコマンドを繰り返します。ここで、検索と置換を実行してから書き込みを実行します。:詳細については、bufdoを参照してください。

于 2012-06-24T01:18:06.587 に答える
0

スクリプトを作成する場合、特に番号が変更されても新しい行に保持する必要がある場合:

for i in */*txt
do
    DIR=`dirname $i` # keep directory name somewhere
    FILE=`basename $i .txt` # remove .txt
    cat $i | sed 's/line \(.*\) this is line \(.*\)/NEW LINE with number \1 this is NEW LINE \2/' > $DIR/$FILE.new # replace line XX this is line YYY => NEW LINE XX this is NEW LINE YY, keeping the values XX and YY
    #mv -f $DIR/$FILE.new $i # uncomment this when you're sure you want to replace orig file
done

よろしく、

于 2012-06-24T16:15:43.007 に答える