2007. 12. 11. 02:39
여러파일을 sorting 하기
2007. 12. 11. 02:39 in Perl Recipe
갑자기 몇개 파일을 소트해야 할 일이 생겨서
하나의 파일을 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;
이런....
뭐.. 더 빠르겠지 ㅎㅎ
하나의 파일을 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;
이런....
뭐.. 더 빠르겠지 ㅎㅎ
'Perl Recipe' 카테고리의 다른 글
두 문자열에서 중복되는 부분 찾기 (0) | 2009.03.25 |
---|---|
일전한 글자수의 단어 세기 (7) | 2009.03.24 |
원라인 펄 놀이 - 첫줄 빼고 sort (0) | 2008.04.17 |
리스트 비교 (0) | 2007.11.06 |
한꺼번에 많은 파일 지우는 세가지 방법 (1) | 2007.11.05 |