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;

이런....
뭐.. 더 빠르겠지 ㅎㅎ
크리에이티브 커먼즈 라이선스
Creative Commons License

'Perl Recipe' 카테고리의 다른 글

원라인 펄 놀이 - 첫줄 빼고 sort  (0) 2008/04/17
여러파일을 sorting 하기  (0) 2007/12/11
리스트 비교  (0) 2007/11/06
한꺼번에 많은 파일 지우는 세가지 방법  (0) 2007/11/05
Trackback 0 Comment 0