Linux Commands : file - search / replace
For the following cmds, the test.file contains:
1 cat barn food 2 dog house kibble 3 fish water food 4 cow pasture grass
grep
“searches the named input FILE for lines containing a match to PATTERN”
* Good for searching a file for a certain pattern
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 user@tim:~$ grep food test.file 1 cat barn food 3 fish water food
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) and can be specified with -F
user@tim:~$ awk '{print $2}' test.file cat dog fish cow
sed
“stream editor for filtering and transforming text”
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
diff
“Compare files”
user@tim:~$ diff --help Usage: diff [OPTION]... FILES Compare FILES line by line. // snip // user@tim:~$ diff test.file new.file 1c1 < 1 cat barn food --- > 1 lion barn food