Showing posts with label regular expression. Show all posts
Showing posts with label regular expression. Show all posts

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

grep: advanced use

How to display all lines with the word text or the word name?
Here is the answer:

[pene@donunix] grep -P 'text|name' file.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

Number of characters in my file (without end_of_line/return characters)

I have some problem with the wc command.
If I write just 3 characters in a file, wc -m give to me 4 characters instead of 3.
For example :
[pene@donunix] echo 123 | wc -m
4

To explain quickly this, wc add the "end of line" character. So, if I don't want it for my echo 123, I have to write this :
[pene@donunix] echo -e "123\c" | wc -m

And if I want to exclude all the end_of_line/return characters of my wc -c, I have to write this wonderful command :
[pene@donunix] cat fic.txt | xargs -i% echo -e "%\c" | wc -c

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'

print (only) the name of file for a grep command

Search the occurence occur in *.pro files, and print only the name of files where the occurence was found.

[pene@donunix] grep occur *.pro | sed -e 's/\(.*\.pro\):.*/\1/' | uniq

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.