| 1 | == Filters - find, grep, sed and awk == |
| 2 | |
| 3 | ==== find ==== |
| 4 | |
| 5 | The 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 | |
| 9 | 1. Search a file with specific name. |
| 10 | |
| 11 | `$ find ./folder -name sample.txt ` |
| 12 | |
| 13 | 2. Search a file with pattern. |
| 14 | |
| 15 | `$ find ./folder -name *.txt ` |
| 16 | |
| 17 | It will give all files which have ‘.txt’ at the end. |
| 18 | |
| 19 | 3. How to find and delete a file with confirmation. |
| 20 | |
| 21 | `$ find ./folder -name sample.txt -exec rm -i {} \; ` |
| 22 | |
| 23 | 4. Search for empty files and directories. |
| 24 | |
| 25 | `$ find ./folder -empty` |
| 26 | |
| 27 | 5. Search for file with entered permissions. |
| 28 | |
| 29 | `$ find ./folder -perm 664` |
| 30 | |
| 31 | 6. Search text within multiple files. |
| 32 | |
| 33 | `$ find ./ -type f -name "*.txt" -exec grep 'Geek' {} \;` |
| 34 | |
| 35 | |
| 36 | ==== grep ==== |
| 37 | |
| 38 | The 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). |
| 39 | Syntax: |
| 40 | |
| 41 | grep [options] pattern [files] |
| 42 | |
| 43 | 1. 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 | |
| 47 | 2. 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 | |
| 51 | 3. 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 | |
| 55 | or |
| 56 | |
| 57 | `$grep -l "unix" f1.txt f2.txt f3.xt f4.txt` |
| 58 | |
| 59 | 4. 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 | |
| 63 | 5. 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 | |
| 67 | 6. 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 | |
| 71 | 7. 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 | |
| 75 | 8. 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 | |
| 79 | 9. 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 | |
| 83 | 10.Specifies expression with -e option. Can use multiple times : |
| 84 | |
| 85 | `$grep –e "Agarwal" –e "Aggarwal" –e "Agrawal" file.txt` |
| 86 | |
| 87 | 12. Search recursively for a pattern in the directory: -R prints the searched pattern in the given directory recursively in all the files. |
| 88 | |
| 89 | Syntax |
| 90 | |
| 91 | `$grep -R [Search] [directory]` |
| 92 | |
| 93 | ==== sed ==== |
| 94 | |
| 95 | SED 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 | |
| 100 | 1.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 | |
| 104 | 2. 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 | |
| 108 | 3.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 | |
| 112 | 4.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 | |
| 116 | 5.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 | |
| 120 | 6.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 | |
| 124 | 7.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 | |
| 128 | 8.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 | |
| 132 | 9.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 | |
| 136 | 10.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 |
| 137 | Examples: |
| 138 | 1.To Delete a particular line say n in this example |
| 139 | Syntax: |
| 140 | `$ sed 'nd' filename.txt` |
| 141 | |
| 142 | Example: |
| 143 | `$ sed '5d' filename.txt` |
| 144 | |
| 145 | 2. To Delete a last line |
| 146 | |
| 147 | Syntax: |
| 148 | |
| 149 | `$ sed '$d' filename.txt` |
| 150 | |
| 151 | 3. To Delete line from range x to y |
| 152 | |
| 153 | Syntax: |
| 154 | |
| 155 | `$ sed 'x,yd' filename.txt` |
| 156 | |
| 157 | Example: |
| 158 | |
| 159 | `$ sed '3,6d' filename.txt` |
| 160 | |
| 161 | 4. To Delete from nth to last line |
| 162 | |
| 163 | Syntax: |
| 164 | |
| 165 | `$ sed 'nth,$d' filename.txt` |
| 166 | |
| 167 | Example: |
| 168 | |
| 169 | `$ sed '12,$d' filename.txt` |
| 170 | |
| 171 | 5. To Delete pattern matching line |
| 172 | |
| 173 | Syntax: |
| 174 | `$ sed '/pattern/d' filename.txt` |
| 175 | |
| 176 | Example: |
| 177 | |
| 178 | `$ sed '/abc/d' filename.txt` |
| 179 | |
| 180 | ==== awk ==== |
| 181 | |
| 182 | Awk 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 | |
| 184 | Syntax: |
| 185 | |
| 186 | `awk options 'selection _criteria {action }' input-file > output-file` |
| 187 | |
| 188 | 1. Default behavior of Awk: By default Awk prints every line of data from the specified file. |
| 189 | |
| 190 | `$ awk '{print}' employee.txt` |
| 191 | |
| 192 | 2. Print the lines which match the given pattern. |
| 193 | |
| 194 | `$ awk '/manager/ {print}' employee.txt ` |
| 195 | |
| 196 | 3. 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 | |