How to Remove Document Management Hurdles with X-Docs?
Why Perl, when you can use bash+awk+sed? :P
1. Why Perl, when you can use bash+awk+sed? :P
Luciano Rocha
June 6th, 2008
2. Introduction
1 What’s your excuse?
2 The hand is faster than the mind!
3 What if...
4 No more hunting!
5 Vrooom!
3. What’s your excuse (or, Learn your tools)
Increase productivity
Having a profound understanding of the tools you interact with daily will
increase your productivity immensely.
Example: shell history
$ wget h t t p : / / . . . / r s y s l o g −3.19.6. t a r . gz
...
$ tar xf ! $ : t
t a r x f r s y s l o g −3.19.6. t a r . gz
$
4. Learn your tools, cont.
Example: extended globs
# rpm −Uvh . . . / RPMS/ i 3 8 6 / postgres −∗
E r r o r : something r e q u i r e s p g s q l
# rpm −i v h ! $ : h / p o s t r e s q l−l i b s ∗ # i n s t a l l l i b
# rpm −Uvh ! $ : h / p o s t g r e s q l −!( t e s t ∗ | d e b u g i n f o ∗ | l i b s ∗) # upgrade t o o l s
...
# # r e b u i l d dependencies
Example: lib dependencies
$ f i n d . −type f −perm −u+x −o −name ∗ . so |
xargs r e a d e l f −d 2 >/ dev / n u l l |
sed −ne ’ /NEED/ s , . ∗ [ ( . ∗ ) ] , 1 , p ’ |
s o r t −u
l i b a s o u n d . so . 2
l i b c o m _ e r r . so . 2
l i b c r y p t o . so . 6
l i b c r y p t o . so . 7
...
5. Learn your tools, cont.
Example: fix track numbers
$ f o r f i n ∗ .mp3 ; do i d 3 v 2 −T quot; $ { f%%[ −.]∗} quot; quot; $ f quot; ; done
Example: remove empty files
$ f i n d . −type f −s i z e 0 −p r i n t 0 | xargs −r 0 rm −f
Example: create archive without ‘.svn’
$ f i n d . −name . svn −prune −o −p r i n t | c p i o −o | g z i p −9 > source . cgz
6. The hand is faster than the mind! (Or, Think as you type)
Mechanize
Entering common commands becomes mechanical, and leaves you time to
think the solution:
Do ‘command’ to some files:
find . ... | xargs −r . . .
fix files:
f o r i i n . . . ; do . . ; done
7. What if... (or, Incremental solutions)
Incremental solutions
Keep trying until you arrive to the correct solution.
Example: find largest file
$ f i n d . −type f |
xargs −r s t a t −c quot;%s %n quot; |
s o r t −r n | head
Example: usb device associated with a disk
$ p=$ ( udevadm i n f o −−path = / sys / b l o c k / sdc −−query=path )
$ udevadm i n f o −e | grep −B1 bus / usb / |
while read p sys && read n dev && read dash ; do
echo $sys $dev ; done | LANG=C s o r t |
while read sys dev ; do
i f echo $p | f g r e p −q − − $sys ; then echo $dev ; break ; f i ;
done
8. No more hunting! (or, Fewer dependencies)
Standard Unix R tools
Perl isn’t always available, or with the packages you desire:
$ ...
Can ’ t l o c a t e A : : B i n @INC . . .
$ yum −y i n s t a l l ’ p e r l (A : : B ) ’
( wait )
...
Nothing t o do
$ cpan A : : B
−bash : cpan : command n o t found
$ p e r l −MCPAN −e s h e l l
install A::B
( wait )
( t r i e s f t p , not possible i n s i d e t h i s f i r e w a l l )
^C
^C
Easier to learn
The documentation for those tools are tiny, compared to Perl’s.
9. Vrooom! (or, Low overhead)
Small and fast utilities
Sometimes, you’ll be restricted in space, like in an initrd, or on the time a
script takes to execute:
$ t i m e awk / dev / n u l l < / dev / n u l l
0.010 s
$ ^awk^ p e r l
0.023 s
Example: Xen bridge script
n e t c o n f _ i p = quot; $ ( i p addr show dev $1 | awk −v ORS= quot; quot; ’
$1 == quot; i n e t quot; | | $1 == quot; i n e t 6 quot; {
p r i n t quot; i p addr add quot; $2 ;
i f ( $3 == quot; brd quot; ) p r i n t quot; brd quot; $4 ;
p r i n t quot; dev $dev ; quot; ;
} ’) quot;