# http://programming.newsforge.com/comments.pl?sid=54523&cid=123985 # Pick a random file (e.g. MP3) ls | perl -e '@a=<>;print $a[rand(@a)]' # Shuffle the contents of a file cat foo | perl -e '@a=<>;while(@a){print splice(@a,rand(@a),1)}' # http://linuxgazette.net/issue85/okopnik.html # A list of duplicate UIDs, along with their related usernames # perl -F: -walne'$h{$F[2]}.="$F[0] ";END{$h{$_}=~/ ./&&print"$_: $h{$_}"for keys%h}' /etc/passwd # the next available UID perl -wle'{getpwuid++$n&&redo;print$n}' # после 1000 perl -wle '$n=1000; {getpwuid ++$n && redo; print $n}' # Удаление повторяющихся строк из файла perl -i -ne '$s{$_}++ || print' file # Удаление из файла строк с неуникальным первым полем perl -i -ane '$s{$F[0]}++ || print' file # Печатает номера повторяющихся строк файла perl -lne '$s{$_}++ && print $.' file # Объединение файлов (с удалением неуникальных строк) perl -ne '$s{$_}++ || print' file1 file2 # Пересечение файлов (с удалением неуникальных строк) perl -ne '$s{$_}++ || print' file1 file2 # Симметричная разность файлов (не содержищих неуникальных строк) # (a la Nab, http://forum.vingrad.ru/index.php?showtopic=111918&view=findpost&p=857611) perl -e '$"=""; ($_="@{[sort(<>)]}")=~s/(.+\n)\1//g; print' file1 file2 # Perl Cookbook # Поиск длинных палиндромов (слов, читаемых одинаково в обоих направлениях) perl -nle 'print if $_ eq reverse && length >5' words # http://ru.wikipedia.org/wiki/Perl # печатаются простые числа perl -wle '(1 x $_) !~ /^(11+)\1+$/ && print while ++ $_' ############################################################################## # ftp://www6.software.ibm.com/software/developer/library/l-p102/tomc.txt # run contents of "my_file" as a program perl my_file # run debugger "stand-alone" perl -d -e 42 # run program, but with warnings perl -w my_file # run program under debugger perl -d my_file # just check syntax, with warnings perl -wc my_file # useful at end of "find foo -print" perl -nle unlink # simplest one-liner program perl -e 'print "hello world!\n"' # add first and penultimate columns perl -lane 'print $F[0] + $F[-2]' # just lines 15 to 17 perl -ne 'print if 15 .. 17' *.pod # in-place edit of *.c files changing all foo to bar perl -p -i.bak -e 's/\bfoo\b/bar/g' *.c # command-line that prints the first 50 lines (cheaply) perl -pe 'exit if $. > 50' f1 f2 f3 ... # delete first 10 lines perl -i.old -ne 'print unless 1 .. 10' foo.txt # change all the isolated oldvar occurrences to newvar perl -i.old -pe 's{\boldvar\b}{newvar}g' *.[chy] # command-line that reverses the whole file by lines perl -e 'print reverse <>' file1 file2 file3 .... # find palindromes perl -lne 'print if $_ eq reverse' /usr/dict/words # command-line that reverse all the bytes in a file perl -0777e 'print scalar reverse <>' f1 f2 f3 ... # command-line that reverses the whole file by paragraphs perl -00 -e 'print reverse <>' file1 file2 file3 .... # increment all numbers found in these files perl i.tiny -pe 's/(\d+)/ 1 + $1 /ge' file1 file2 .... # command-line that shows each line with its characters backwards perl -nle 'print scalar reverse $_' file1 file2 file3 .... # delete all but lines between START and END perl -i.old -ne 'print unless /^START$/ .. /^END$/' foo.txt # binary edit (careful!) perl -i.bak -pe 's/Mozilla/Slopoke/g' /usr/local/bin/netscape # look for dup words perl -0777 -ne 'print "$.: doubled $_\n" while /\b(\w+)\b\s+\b\1\b/gi' # command-line that prints the last 50 lines (expensively) perl -e '@lines = <>; print @lines[ $#lines .. $#lines-50' f1 f2 f3 ... ############################################################################## # http://www-128.ibm.com/developerworks/linux/library/l-p102.html # add first and penultimate columns # NOTE the equivalent awk script: # awk '{i = NF - 1; print $1 + $i}' perl -lane 'print $F[0] + $F[-2]' ### Printing a range of lines # 1. just lines 15 to 17 perl -ne 'print if 15 .. 17' # 2. just lines NOT between line 10 and 20 perl -ne 'print unless 10 .. 20' # 3. lines between START and END perl -ne 'print if /^START$/ .. /^END$/' # 4. lines NOT between START and END perl -ne 'print unless /^START$/ .. /^END$/' # just lines 15 to 17, efficiently perl -ne 'print if $. >= 15; exit if $. >= 17;' ### In-place editing # 1. in-place edit of *.c files changing all foo to bar perl -p -i.bak -e 's/\bfoo\b/bar/g' *.c # 2. delete first 10 lines perl -i.old -ne 'print unless 1 .. 10' foo.txt # 3. change all the isolated oldvar occurrences to newvar perl -i.old -pe 's{\boldvar\b}{newvar}g' *.[chy] # 4. increment all numbers found in these files perl -i.tiny -pe 's/(\d+)/ 1 + $1 /ge' file1 file2 .... # 5. delete all but lines between START and END perl -i.old -ne 'print unless /^START$/ .. /^END$/' foo.txt # 6. binary edit (careful!) perl -i.bak -pe 's/Mozilla/Slopoke/g' /usr/local/bin/ne ### Reversal of files' fortunes # 1. command-line that reverses the whole input by lines # (printing each line in reverse order) perl -e 'print reverse <>' file1 file2 file3 .... # 2. command-line that shows each line with its characters backwards perl -nle 'print scalar reverse $_' file1 file2 file3 .... # 3. find palindromes in the /usr/dict/words dictionary file perl -lne '$_ = lc $_; print if $_ eq reverse' /usr/dict/words # 4. command-line that reverses all the bytes in a file perl -0777e 'print scalar reverse <>' f1 f2 f3 ... # 5. command-line that reverses each paragraph in the file but prints # them in order perl -00 -e 'print reverse <>' file1 file2 file3 .... # replace string XYZ with a random number less than 611 in these files perl -i.bak -pe "s/XYZ/int rand(611)/e" f1 f2 f3 ### Revealing the files' base nature # 1. Run basename on contents of file perl -pe "s@.*/@@gio" INDEX # 2. Run dirname on contents of file perl -pe 's@^(.*/)[^/]+@$1\n@' INDEX # 3. Run basename on contents of file perl -MFile::Basename -ne 'print basename $_' INDEX # 4. Run dirname on contents of file perl -MFile::Basename -ne 'print dirname $_' INDEX ### Moving or renaming, it's all the same in UNIX # 1. write command to mv dirs XYZ_asd to Asd # (you may have to preface each '!' with a '\' depending on your shell) ls | perl -pe 's!([^_]+)_(.)(.*)!mv $1_$2$3 \u$2\E$3!gio' # 2. Write a shell script to move input from xyz to Xyz ls | perl -ne 'chop; printf "mv $_ %s\n", ucfirst $_;' # http://www-128.ibm.com/developerworks/linux/library/l-p101/ # Ничего особо интересного # http://www.ajs.com/ajswiki/Perl_one-liners # http://www.rollmop.org/oneliners/perl.html # http://defindit.com/readme_files/perl_one_liners.html