0

Ubuntuでは、ツリー内に「output.txt」というファイルがたくさんあります。ファイルごとに、「sort」を使用して行をアルファベット順に並べ替えます。私は考慮した

探す 。-name output.txt -exec sort {} \;

しかし、これは私が望む元のファイルを更新するのではなく、ソートされた行をコンソールに出力します。

4

4 に答える 4

3
find . -name output.txt -exec sort{} -o{} \;
于 2009-06-03T13:20:15.363 に答える
1

Fortran、

私はそれを少し違ったやり方で行います、私はファイルをそれ自体に分類しません。並べ替え後に新しいものがファイルに追加された場合に備えて。これが私がすることです:

  • 'output.txt'ファイルを持つdirツリーをmirriorし、そこにソートされたファイルを置きます

そのようです:

#!/bin/bash 

LIST=`find $1 -name output.txt`

for i in $LIST ; do
        X=`dirname $i`
        mkdir -p "sorted/$X"
        sort "$i" -o "sorted/$i"
done

上記には少し磨きをかける必要があります。mkdirの周囲には「if」が必要であり、$1の健全性を確認する必要があります。

あなたにこれを与えます:

オリジナル:

mytest
mytest/ccc
mytest/ccc/f
mytest/ccc/f/output.txt
mytest/ccc/d
mytest/ccc/d/output.txt
mytest/ccc/e
mytest/ccc/e/output.txt
mytest/bbb
mytest/bbb/f
mytest/bbb/d
mytest/bbb/e
mytest/aaa
mytest/aaa/f
mytest/aaa/f/output.txt
mytest/aaa/d
mytest/aaa/d/output.txt
mytest/aaa/e
mytest/aaa/e/output.txt

出力:

sorted
sorted/mytest
sorted/mytest/ccc
sorted/mytest/ccc/f
sorted/mytest/ccc/f/output.txt
sorted/mytest/ccc/d
sorted/mytest/ccc/d/output.txt
sorted/mytest/ccc/e
sorted/mytest/ccc/e/output.txt
sorted/mytest/aaa
sorted/mytest/aaa/f
sorted/mytest/aaa/f/output.txt
sorted/mytest/aaa/d
sorted/mytest/aaa/d/output.txt
sorted/mytest/aaa/e
sorted/mytest/aaa/e/output.txt

最後に、ソート出力ファイルがtmpファイルに書き込まれ、ソートが完了すると名前が変更されると思います。そうしないと、すべてが読み取られる前に、出力ファイルが入力ファイルを切り捨てる可能性があります。sedのインプレースオプションのように。

于 2009-06-04T03:58:39.153 に答える
0

これを試して

csh <enter>

foreach i ( `find . -type f -name output.txt -print`) <enter>
   cat $i | sort > $.sorted <enter>
end <enter>

ファイルのソートされたコピーを生成します。

于 2009-06-03T13:18:14.433 に答える
-1

私はLinuxをあまり使ったことがないので、誤解されているかもしれませんが、それを制御できるのではないでしょうStdOutか。

find . -name output.txt -exec sort{} \ > output.txt
于 2009-06-03T13:14:10.757 に答える