2007. 12. 11. 02:39

여러파일을 sorting 하기

갑자기 몇개 파일을 소트해야 할 일이 생겨서

하나의 파일을 sort 하는 방법

perl -i.bak -ne'push@L,$_;END{print sort @L;}' filename

여러 파일을 sort하려면

perl -i.bak -ne'push@L,$_;if(eof){print sort@L;@L=();}' file1 file2 file3

또는

for file in file1 file2 file3;do perl -i.bak -ne'push@L,$_;END{print sort@L;}' $file;done

더 좋은 방법은 당장 생각이 안난다.

find를 이용해서 만든 리스트들이 소트되어 있지 않아서 만든 것인데 실제로는 필요없는 디렉토리가 몇개 들어가 있었다. 필요없는 디렉토리들의 공통점은 숫자로 끝나지 않는다는 것이 었기 때문에 간단히

perl -i.bak -ne'/\d$/ or next;push@L,$_;eof and print sort@L and @L=();' input*10k.txt

다시 생각해 보니 펄을 안쓰는게 더 간단하다
for x in input*10k;do grep -v '[0-9]$' $x | sort > $x.tmp;mv $x.tmp $x;done;

이런....
뭐.. 더 빠르겠지 ㅎㅎ