Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

[Perl] a one-line command to create a random number

Here is how to create a random number between 0 and 100 with this simple perl one-line command :

[pene@donunix] perl -e "print int(rand(100))"

grep -v in Perl one-line

How to create the grep -v command in Perl, here is the clue :

[pene@donunix] perl -ne "! /your_string/ && print" file.txt

And if you want to write the output directly into the input file (file.txt), just type this :
[pene@donunix] perl -i.bak -ne "! /your_string/ && print" file.txt

[Perl] One liner calculus

In the following example file « calc.txt », I want to multiply each number by 2/3 :

199,119
326,147
385,345
250,330
157,218
67,241
402,175

Here is the solution in one line with Perl :

[pene@donunix] perl -pi.bak -e 's|(\d+),(\d+)|int($1*2/3).",".int($2*2/3)|e' calc.txt

[Perl] make changes form line 3 to line 20

How to substitute all pattern1 to pattern2 from the 3rd line to the 20st line?
Perl has the solution... and in one line :D

[pene@donunix] perl -p -i -e "s|pattern1|pattern2| if 3 .. 20" file.txt

search from the current directory and replace html files content

How replace the expression expr1 by expr2 in all the html files located from the current directory and all his sub-directories :

[pene@donunix] find . -name "*.html" -print0 | xargs -0 perl -p -i -e 's|expr1|expr2|g'

transform a path onto a variable in a list of files

in this code, we change in all IDL *.pro files, the path ../DATA onto a variable named data_toolbox.
For more precisions, the occurence '../DATA is replaced by data_toolbox+'
Here is the code :
[pene@donunix] perl -p -i.bak -e "s|\'\.\./DATA|toolbox_data+\'|g" *.pro


NB : Before making the changes, perl create a copy of the IDL files with the extension .bak.

Perl one line : how to create a html thumb page

Here is a Perl one-line command to create an html thumb page.
Then in the html thumb page you will be able to visualise gifs from a specific directory :

The code :
[pene@donunix] ls -1 *.jpg *.gif *.png > index.html
[pene@donunix] perl -p -i.bak -e 's/^(.*)\.(.{3})$/<a href="$1\.$2"><img src="$1\.$2" width="320" border="0" alt="$1" ><\/a><br\/>/g' index.html

Well done, you've created an index.html file in your pictures directory.