Perl Commandline CookBook
Perl Commandline CookBook ¶PerlÀº ¸¹Àº ºÐ¾ß¿¡¼ °·ÂÇÑ ÈûÀ» °¡Áö°í ÀÖÁö¸¸ ½© Ä¿¸Çµå ¶óÀο¡¼ ±âÁ¸ÀÇ awk¿Í sedµîÀ» ´ëüÇÏ°í ±×ÀÌ»óÀÇ ¸¹Àº ÀÏÀ» Çس¾ ¼ö ÀÖ´Â °·ÂÇÑ µµ±¸ÀÔ´Ï´Ù. Ä¿¸Çµå¶óÀÎ ÆÞÀº "¾Æ´Â¸¸Å ¾²¸éµÈ´Ù"´Â ÆÞÀÇ ÀåÁ¡À» º¸¿©ÁÖ´Â °¡Àå ÁÁÀº ¿¹ÀÔ´Ï´Ù.
Ä¿¸Çµå ¶óÀο¡¼ ¾µ¼ö ÀÖ´Â ´Ù¾çÇÑ Perl ÆÁÀ» ¸ð¾Æº¾½Ã´Ù. Perl¿ÜÀÇ ´Ù¾çÇÑ ¹æ¹ýµéµµ ÇÔ²² ¸ð¾Æ¼ ºñ±³Çغ¸´Â °Íµµ ÁÁ°ÚÁÒ?
¹Ù·Î º¹»çÇؼ ¾µ ¼ö ÀÖ´Â CookBookÀ» ¸¸µå´Â °ÍÀÌ ¸ñÀûÀθ¸Å ÆÁµéÀ» ¹®¼ÀÇ ¾ÕÀ¸·Î µ¹¸®°í ¹®¼ÀÇ µÚÂÊ¿¡ Ä¿¸Çµå¶óÀÎ ¿É¼ÇÀ̳ª À¯¿ëÇÑ ¹®¹ý¿¡ ´ëÇØ Ãß°¡ÇÏ°Ú½À´Ï´Ù.
-- pung96 2007-01-31 23:30:27
1.1.1. ¸¹Àº ÆÄÀϵéÀ» Çѹø¿¡ »èÁ¦Çϱ⠶find -name "*.log" -print | perl -nle unlink find -name "*.log" -exec rm \{\} \; find -name "*.log" -print | xargs rm 1000°³ Áö¿îÈÄ¿¡ 5ÃÊ ½¬µµ·Ï ÇÒ °æ¿ì
find -name "*.log" -print | perl -nle 'unlink; $.%1000==0 and sleep(10);'
1.1.2. ¿©·¯ÁÙÀÇ µ¥ÀÌÅ͸¦ grepÇϱ⠶perl -0777ne '/(START_FLAG.*?END_FLAG)/s and print $1' list.txt # ÆÄÀÏÀÌ ÀÛÀ»¶§ perl -ne '/START_FLAG/ ... /END_FLAG/ and print;' # ÆÄÀÏÀÌ Å¬¶§ 1.1.3. ¸®½ºÆ® ºñ±³ : º¹ÀâÇÑ comm ±¸ÇöÇϱ⠶±âº» ¾ÆÀ̵ð¾î´Â ¿©·¯ ÆÄÀÏÀ» ÀÐ¾î¼ °¢ ¶óÀÎÀÇ Àû´çÇÑ ÆÐÅÏ È¤Àº Àüü ¶óÀÎÀ» Çؽ¬ Å°·Î ¸¸µé°í ¶óÀÎÀ» Çؽ¬ °ªÀ¸·Î ¸¸µé¾î¼ °¢°¢ ÆÄÀÏ¿¡ ´ëÀÀµÇ´Â Çؽ¬¿¡ ÀúÀåÇÑ ÈÄ¿¡ Å°°¡ °¢ Çؽ¬¿¡ Á¸ÀçÇÏ´ÂÁö È®ÀÎÇÏ¿© Ãâ·ÂÇÕ´Ï´Ù. ÆíÀÇ»ó ½ºÅ©¸³Æ®¿¡¼ ÁÖ·Î »ç¿ëÇÏÁö ¾Ê´Â ½Éº¼¸¯ ·¹ÆÛ·±½º¸¦ »ç¿ëÇß½À´Ï´Ù.
# list1 °ú list2 ¸ðµÎ¿¡ ÀÖ´Â Ç׸ñ
grep -f list2 list1 perl -e '@a{@ARGV}=q(A..Z);while(<>){$a{$ARGV}->{$_}++};for(keys %A){print $_ if $B{$_}}' list1 list2 perl -ne 'BEGIN{@a{@ARGV}=(A..Z)}{$a{$ARGV}->{$_}++}END{for(keys %A){print $_ if $B{$_} } }' ls.log ls.log2 # list1 ¿¡¸¸ ÀÖ´Â Ç׸ñ grep -vf list2 list1 perl -e '@a{@ARGV}=(A..Z);while(<>){$a{$ARGV}->{$_}++};for(keys %A){print $_ unless $B{$_}}' list1 list2 # list1 + list2 cat list1 list2 | uniq perl -e 'while(<>){$A{$_}++};for(keys %A){print $_;};' # ¼Óµµ´Â Á» ´À¸®Áö¸¸ °£´ÜÇÑ ¹æ¹ý perl -e '@s{`cat OLD`}=( ); exists $s{$_} && print for `cat NEW`'
patternÀÌ list1 °ú list2¿¡ ¸ðµÎ ¸ÅÄ¡µÇ´Â Ç׸ñÀÇ list1 Ç׸ñ. ¸»ÀÌ Á» º¹ÀâÇϳ׿ä. perl -e '@A{@ARGV}=qw(L R);while(<>){/(pattern)/ and $A{$ARGV}->{$1}=$_};for(keys %L){print $L{$_} if $R{$_};}' list1 list2
1.1.4. ¿¢¼¿Ã³·³ Ä÷³ °è»êÇϱ⠶cat data.txt | perl -ane '{$avr=$F[2]+$F[3];$sum+=$avr;print "Avr is $avr\n";}END{print "Sum is $sum\n";}' cat data.txt | awk '{avr=$1+$2;sum+=avr;print "Avr is "avr;}END{print "Sum is "sum;}' perl -nle '{/(pattern1).*(pattern2)/ and $avr=$1+$2 and $sum+=$avr and print "Avr is $avr"}END{print "Sum is $sum";}' data.txt 1.1.5. ÆÄÀÏ ¼öÁ¤Çϱ⠶
1.1.6. °¢ ¶óÀÎÀÇ ÆÐÅÏ¿¡ µû¶ó ´Ù¸¥ ÆÄÀÏ(pattern.txt)·Î ºÐ·ùÇϱ⠶perl -ne '{/(pattern)/ and $H{$1}.=$_;}END{for(keys %H){open F,">$_.txt";print F $H{$_};close F;}' data.txt
|
He who has imagination without learning has wings but no feet. |