HOME - Recent Changes - Search:

Academic Work


Personal

* pot de départ


dblp


(:twitter:)

-----

[ edit | logout ]
[ help | sandbox | passwd ]

Perl as a command line utility

#############################

Question

How to replace sed and awk with Perl in command line / shell scripts?

Examples

$ perl -e 'print "Hello, World!\n"'
Hello, World!

The -e option (execute) instructs the Perl interpreter that the next argument is a Perl statement to be compiled and run. If -e is given, Perl will not look for a script filename in the argument list. Multiple -e commands may be given to build up a multi-line script.

$ echo laci | perl -ne 'if (/a/) { s#a#A#; print; }'
lAci
$ echo hello | perl -ne  "s/h/H/; print;"
Hello
$ echo hello | perl -pne  "s/h/H/"
Hello

The option -p prints lines.

$ echo file.ext | perl -npe  "s|\.(.*)$||"
file

Remove file extension. Note: don't use s###; the character '#' will cause problems.

jabba@hoth:~$ FILE=file.ext
jabba@hoth:~$ echo $FILE
file.ext
jabba@hoth:~$ FILE=`echo $FILE | perl -npe  "s|\.(.*)$||"`
jabba@hoth:~$ echo $FILE
file

Manipulate shell variables (as strings) with Perl in command line.

More examples

Cloud City


anime | bash | blogs | bsd | c/c++ | c64 | calc | comics | convert | cube | del.icio.us | digg | east | eBooks | egeszseg | elite | firefox | flash | fun | games | gimp | google | groovy | hardware | hit&run | howto | java | javascript | knife | lang | latex | liferay | linux | lovecraft | magyar | maths | movies | music | p2p | perl | pdf | photoshop | php | pmwiki | prog | python | radio | recept | rts | scala | scene | sci-fi | scripting | security | shell | space | súlyos | telephone | torrente | translate | ubuntu | vim | wallpapers | webutils | wikis | windows


Blogs and Dev.

* Ubuntu Incident
* Python Adventures
* me @ GitHub


Places

Debrecen | France | Hungary | Montreal | Nancy


Notes

full circle | km


Hobby Projects

* Jabba's Codes
* PmWiki
* Firefox
* PHP
* JavaScript
* Scriptorium
* Tutorials
* me @ GitHub


Quick Links


[ edit ]

View - Edit - History - Attach - Print *** Report - Recent Changes - Search
Page last modified on 2008 June 01, 08:30