Changes between Initial Version and Version 1 of Csle2022/Agenda/linuxfilters


Ignore:
Timestamp:
Nov 3, 2022, 4:41:21 AM (2 years ago)
Author:
deepthi
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Csle2022/Agenda/linuxfilters

    v1 v1  
     1== Filters - find, grep, sed and awk ==
     2
     3==== find ====
     4
     5The find command in UNIX is a command line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions. By using the ‘-exec’ other UNIX commands can be executed on files or folders found.
     6
     7`$ find [where to start searching from]`
     8
     91. Search a file with specific name.
     10
     11`$ find ./folder -name sample.txt `
     12
     132. Search a file with pattern.
     14
     15`$ find ./folder -name *.txt `
     16
     17It will give all files which have ‘.txt’ at the end.
     18
     193. How to find and delete a file with confirmation.
     20
     21`$ find ./folder -name sample.txt -exec rm -i {} \; `
     22
     234. Search for empty files and directories.
     24
     25`$ find ./folder -empty`
     26
     275. Search for file with entered permissions.
     28
     29`$ find ./folder -perm 664`
     30
     316. Search text within multiple files.
     32
     33`$ find ./ -type f -name "*.txt" -exec grep 'Geek'  {} \;`
     34
     35
     36==== grep ====
     37
     38The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expression and print out).
     39Syntax:
     40 
     41grep [options] pattern [files]
     42
     431. Case insensitive search : The -i option enables to search for a string case insensitively in the given file. It matches the words like “UNIX”, “Unix”, “unix”.
     44 
     45`$grep -i "UNix" file.txt`
     46
     472. Displaying the count of number of matches : We can find the number of lines that matches the given string/pattern
     48
     49`$grep -c "unix" file.txt`
     50
     513. Display the file names that matches the pattern : We can just display the files that contains the given string/pattern.
     52 
     53`$grep -l "unix" *`
     54
     55or
     56 
     57`$grep -l "unix" f1.txt f2.txt f3.xt f4.txt`
     58
     594. Checking for the whole words in a file : By default, grep matches the given string/pattern even if it is found as a substring in a file. The -w option to grep makes it match only the whole words.
     60 
     61`$ grep -w "unix" file.txt`
     62
     635. Displaying only the matched pattern : By default, grep displays the entire line which has the matched string. We can make the grep to display only the matched string by using the -o option.
     64 
     65`$ grep -o "unix" file.txt`
     66
     676. Show line number while displaying the output using grep -n : To show the line number of file with the line matched.
     68 
     69`$ grep -n "unix" file.txt`
     70
     717. Inverting the pattern match : You can display the lines that are not matched with the specified search string pattern using the -v option.
     72 
     73`$ grep -v "unix" file.txt`
     74
     758. Matching the lines that start with a string : The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.
     76 
     77`$ grep "^unix" file.txt`
     78
     799. Matching the lines that end with a string : The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines which end with the given string or pattern.
     80 
     81`$ grep "os$" file.txt`
     82
     8310.Specifies expression with -e option. Can use multiple times :
     84 
     85`$grep –e "Agarwal" –e "Aggarwal" –e "Agrawal" file.txt`
     86
     8712. Search recursively for a pattern in the directory: -R prints the searched pattern in the given directory recursively in all the files.
     88
     89Syntax
     90
     91`$grep -R [Search] [directory]`
     92
     93==== sed ====
     94
     95SED is a text stream editor used on Unix systems to edit files quickly and efficiently. The tool searches through, replaces, adds, and deletes lines in a text file without opening the file in a text editor.
     96
     97- SED is a powerful text stream editor. Can do insertion, deletion, search and replace(substitution).
     98- SED command in unix supports regular expression which allows it perform complex pattern matching.
     99
     1001.Replacing or substituting string : Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word “unix” with “linux” in the file.
     101
     102`$sed 's/unix/linux/' geekfile.txt`
     103
     1042. Replacing the nth occurrence of a pattern in a line : Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below command replaces the second occurrence of the word “unix” with “linux” in a line.
     105
     106`$sed 's/unix/linux/2' geekfile.txt`
     107
     1083.Replacing all the occurrence of the pattern in a line : The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.
     109
     110`$sed 's/unix/linux/g' geekfile.txt`
     111
     1124.Replacing from nth occurrence to all occurrences in a line : Use the combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a pattern in a line. The following sed command replaces the third, fourth, fifth… “unix” word with “linux” word in a line.
     113
     114`$sed 's/unix/linux/3g' geekfile.txt`
     115
     1165.Parenthesize first character of each word : This sed example prints the first character of every word in parenthesis.
     117
     118`$ echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g'`
     119
     1206.Replacing string on a specific line number : You can restrict the sed command to replace the string on a specific line number. An example is
     121
     122`$sed '3 s/unix/linux/' geekfile.txt`
     123
     1247.Duplicating the replaced line with /p flag : The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that line only once.
     125
     126`$sed 's/unix/linux/p' geekfile.txt`
     127
     1288.Printing only the replaced lines : Use the -n option along with the /p print flag to display only the replaced lines. Here the -n option suppresses the duplicate rows generated by the /p flag and prints the replaced lines only one time.
     129
     130`$sed -n 's/unix/linux/p' geekfile.txt`
     131
     1329.Replacing string on a range of lines : You can specify a range of line numbers to the sed command for replacing a string.
     133
     134`$sed '1,3 s/unix/linux/' geekfile.txt`
     135
     13610.Deleting lines from a particular file : SED command can also be used for deleting lines from a particular file. SED command is used for performing deletion operation without even opening the file
     137Examples:
     138 1.To Delete a particular line say n in this example
     139Syntax:
     140`$ sed 'nd' filename.txt`
     141
     142Example:
     143`$ sed '5d' filename.txt`
     144
     1452. To Delete a last line
     146
     147Syntax:
     148
     149`$ sed '$d' filename.txt`
     150
     1513. To Delete line from range x to y
     152
     153Syntax:
     154
     155`$ sed 'x,yd' filename.txt`
     156
     157Example:
     158
     159`$ sed '3,6d' filename.txt`
     160
     1614. To Delete from nth to last line
     162
     163Syntax:
     164
     165`$ sed 'nth,$d' filename.txt`
     166
     167Example:
     168
     169`$ sed '12,$d' filename.txt`
     170
     1715. To Delete pattern matching line
     172
     173Syntax:
     174`$ sed '/pattern/d' filename.txt`
     175
     176Example:
     177
     178`$ sed '/abc/d' filename.txt`
     179
     180==== awk ====
     181
     182Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators.
     183
     184Syntax:
     185
     186`awk options 'selection _criteria {action }' input-file > output-file`
     187
     1881. Default behavior of Awk: By default Awk prints every line of data from the specified file. 
     189
     190`$ awk '{print}' employee.txt`
     191
     1922. Print the lines which match the given pattern.
     193
     194`$ awk '/manager/ {print}' employee.txt `
     195
     1963. Splitting a Line Into Fields : For each record i.e line, the awk command splits the record delimited by whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and $4 respectively. Also, $0 represents the whole line. 
     197
     198`$ awk '{print $1,$4}' employee.txt`
     199
     200
     201
     202
     203
     204
     205
     206
     207
     208