Tags
$ perl -pi -w -e 's/foo/bar/g;' *.txt
-e means execute the following line of code.
-i means edit in-place
-w write warnings
-p loop
It will search for “foo” in files with extension .txt and replace it with “bar”.
Credit: here.
28 Thursday Jan 2010
14 Thursday Jan 2010
Tags
[a-d] – Match one character with in a-d i.e. a, b, c, d
[^a-d] – Match one character not in the range a-d
\<test\> – Match whole word test
test\> – Match words that ends with test
\<test\> \1ing – Match following text “test testing”, \1 maps to first tag i.e \(\)
x\{5,\} – Match at least 5 occurrences of x
x\{5,9\} – Match between 5 to 9 times occurrences of x
^test – Looks for test at the beginning of a line
test$ – Looks for test at the end of the line
^test$ – Looks for test on a line by itself
th.t – “.” matches one character i.e. 4 letters has th + any character and ends with t. Example: this, that are valid matches
\. – Look for period, using “\” one can escape metacharacters
Search and replace:
:s/\(square\) and \(fair\)/\2 and \1/ – searches for “square and fair” and replaces it with fair and square
26 Saturday Sep 2009
I always get confused about various flavors of grep
. Here’s a summary to shine some light: (man grep
to know more!)
egrep
or grep -E
(in linux only) is extended grep where additional regular expression metacharacters have been added like +, ?, | and ()
fgrep
or grep -F
(in linux only) is fixed or fast grep and behaves as grep but does not recognize any regular expression metacharacters as being special.25 Friday Sep 2009
For grepping line-by-line in a file filename, I often find these very useful
Match pattern1
OR pattern2 in the same line
:
$ grep -E 'pattern1|pattern2'
filename
Match pattern1
AND pattern2 in the same line
:
$ grep -E 'pattern1.*pattern2'
filename
The above command searches for pattern1
followed by pattern2
. If the order does not matter or you want to search them in either order, then use the follwoing
$ grep -E 'pattern1.*pattern2|pattern2.*pattern1'
filename
The pipe enables the OR search which we saw earlier. Another option for this situation (i.e., AND search when the order is not important):
$ grep -E 'pattern1' filename | grep -E 'pattern2'
which basically greps the STDOUT of the first grep.
Match pattern1
AND pattern2
, but NOT pattern3
in the same line:
$ grep -E 'pattern1.*pattern2'
filename | grep -Ev 'pattern3'
when the order of the first two patterns is important. When that order is NOT important:
$ grep -E 'pattern1' filename | grep -E 'pattern2' | grep -Ev 'pattern3'
Match pattern1
OR pattern2
, but NOT pattern3
in the same line:
$ grep -E 'pattern1|pattern2'
filename | grep -Ev 'pattern3'
N.B. (1) grep -E
may be replaced by egrep
. I used grep -E
everywhere in this post assuming a general case of regular expressions as patterns. Lowercase -e
is also used for regex, but this is more “basic” than -E
which supports “extended” regex, e.g. regular expression metacharacters like +, ?, | and (). (2) The -v
flag is for non-matching grep.