Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revision Both sides next revision
help:general_linux:linux_advanced [2016/05/18 13:35]
jebailie created
help:general_linux:linux_advanced [2016/05/18 14:13]
jebailie
Line 1: Line 1:
-===== Linux Commands : advanced=== +===== Linux Commands : file - search / replace ===== 
 +For the following cmds, the //test.file// contains: 
 +<code> 
 +1 cat barn food 
 +2 dog house kibble 
 +3 fish water food 
 +4 cow pasture grass 
 +</code>
  
 ==== grep ==== ==== grep ====
 +"searches the named input FILE for lines containing a match to PATTERN"
  
 +* Good for searching a file for a certain pattern
 +<code>
 +user@tim:~$ grep --help
 +Usage: grep [OPTION]... PATTERN [FILE]...
 +Search for PATTERN in each FILE or standard input.
 +  // snip //
 +  
 +user@tim:~$ grep cat test.file 
 +1 cat barn food
  
-==== diff ====+user@tim:~$ grep food test.file  
 +1 cat barn food 
 +3 fish water food 
 + 
 +</code>
  
 ==== awk ==== ==== awk ====
 +"pattern scanning and processing language"
 +
 +* good for printing columnar data, note that the field separator (between the columns) can be anything (space, tab, colon, etc)
 +<code>
 +user@tim:~$ cat test.file |awk '{print $2}'
 +cat
 +dog
 +fish
 +cow
 +</code>
  
 ==== sed ==== ==== sed ====
 +"stream editor for filtering and transforming text"
 +<code>
 +user@tim:~$ sed s/cat/lion/ test.file
 +1 lion barn food
 +2 dog house kibble
 +3 fish water food
 +4 cow pasture grass
 +
 +(note that the input file //test.file// did NOT change)
 +
 +user@tim:~$ cat test.file
 +1 cat barn food
 +2 dog house kibble
 +3 fish water food
 +4 cow pasture grass
 +
 +user@tim:~$ sed s/cat/lion/ test.file > new.file
 +(save into //new.file//)
 +
 +user@tim:~$ cat new.file
 +1 lion barn food
 +2 dog house kibble
 +3 fish water food
 +4 cow pasture grass
 +
 +user@tim:~$ cat test.file
 +1 cat barn food
 +2 dog house kibble
 +3 fish water food
 +4 cow pasture grass
 +</code>