Linux Commands: Difference between revisions

From miki
Jump to navigation Jump to search
m (→‎grep: typo)
(→‎sed: sed and some regular expression examples)
Line 1: Line 1:
== grep ==
== Frequently Used Shell Commands ==
<source lang="bash">
grep -Rsl PATTERN [FILE] # Recursive, no error output, only list filename
grep BASIC-REG-EXP-PATTERN [FILE] # Use classic regexp (like "dma\|DMA")
egrep EXT-REG-EXP-PATTERN [FILE] # Same as grep -E. Use extended regexp (like "dma|DMA")
fgrep FIXED-STRINGS-REG-EXP [FILE] # Same as grep -F. Pattern is a list of strings to match.
grep -n PATTERN [FILE] # Print matched line numbers.
</source>


=== grep ===
== sed ==
<source lang="bash">
% grep '''-Rsl''' ''PATTERN'' [''FILE''] # Recursive, no error output, only list filename
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
% '''grep''' ''BASIC-REG-EXP-PATTERN'' [''FILE''] # Use classic regexp (like "dma\|DMA")
sed -n # Silent - suppress automatic printing of pattern space
% '''egrep''' ''EXT-REG-EXP-PATTERN'' [''FILE''] # Same as grep -E. Use extended regexp (like "dma|DMA")
sed -r # Use extended regular expression
% '''fgrep''' ''FIXED-STRINGS-REG-EXP'' [''FILE''] # Same as grep -F. Pattern is a list of strings to match.
</source>
% grep '''-n''' ''PATTERN'' [''FILE''] # Print matched line numbers.

=== Regular expressions===
The information below is only illustrative. See e.g. [[wikipedia:Regular_expression|Wikipedia page ]] for reference information. The list below is actually for ''extended regular expressions'', which can be obtained in ''sed'' using option '''-r''' (<tt>sed -r</tt>).
{| class="wikitable"
!Regexp!!Description
|-
|<tt>.</tt>||Match any character
|-
||<tt>gray|grey</tt>||Match ''gray'' or ''grey''
|-
||<tt>gr(a|e)y</tt>||Match ''gray'' or ''grey''
|-
||<tt>gr[ae]y</tt>||Match ''gray'' or ''grey''
|-
||<tt>file[^0-2]</tt>||Match ''file3'' or ''file4'', but not ''file0'', ''file1'', ''file2''.
|-
|<tt>colou?r</tt>||''(zero or one)'' - Match ''Color'' or ''Colour''.
|-
|<tt>ab*c</tt>||''(zero or more)'' - Match ''ac'', ''abc'', ''abbc'', ....
|-
|<tt>ab+c</tt>||''(one or more)'' - Match ''abc'', ''abbc'', ''abbbc'', ....
|-
|<tt>a{3,5}</tt>||''(at least m and not more than n times)'' - Match ''aaa'', ''aaaa'', ''aaaaa''.
|-
|<tt>^on single line$</tt>||''(start and end of line)'' - Match ''on single line'' on a single line.
|}
<small>When using standard (non-extended) regular expression, some special meta-characters (like the parenthesis '''( )''', or braces '''{ }''') must be quoted with '''backslash \'''.</small>

Revision as of 11:27, 27 September 2008

grep

grep -Rsl PATTERN [FILE]            # Recursive, no error output, only list filename
grep BASIC-REG-EXP-PATTERN [FILE]   # Use classic regexp (like "dma\|DMA")
egrep EXT-REG-EXP-PATTERN [FILE]    # Same as grep -E. Use extended regexp (like "dma|DMA")
fgrep FIXED-STRINGS-REG-EXP [FILE]  # Same as grep -F. Pattern is a list of strings to match.
grep -n PATTERN [FILE]              # Print matched line numbers.

sed

sed [OPTION]... {script-only-if-no-other-script} [input-file]...
sed -n                              # Silent - suppress automatic printing of pattern space
sed -r                              # Use extended regular expression

Regular expressions

The information below is only illustrative. See e.g. Wikipedia page for reference information. The list below is actually for extended regular expressions, which can be obtained in sed using option -r (sed -r).

Regexp Description
. Match any character
gray|grey Match gray or grey
gr(a|e)y Match gray or grey
gr[ae]y Match gray or grey
file[^0-2] Match file3 or file4, but not file0, file1, file2.
colou?r (zero or one) - Match Color or Colour.
ab*c (zero or more) - Match ac, abc, abbc, ....
ab+c (one or more) - Match abc, abbc, abbbc, ....
a{3,5} (at least m and not more than n times) - Match aaa, aaaa, aaaaa.
^on single line$ (start and end of line) - Match on single line on a single line.

When using standard (non-extended) regular expression, some special meta-characters (like the parenthesis ( ), or braces { }) must be quoted with backslash \.