98 | | == Filters - find, grep, sed and awk == |
99 | | |
100 | | ==== find ==== |
101 | | |
102 | | 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. |
103 | | |
104 | | `$ find [where to start searching from]` |
105 | | |
106 | | 1. Search a file with specific name. |
107 | | |
108 | | `$ find ./folder -name sample.txt ` |
109 | | |
110 | | 2. Search a file with pattern. |
111 | | |
112 | | `$ find ./folder -name *.txt ` |
113 | | |
114 | | It will give all files which have ‘.txt’ at the end. |
115 | | |
116 | | 3. How to find and delete a file with confirmation. |
117 | | |
118 | | `$ find ./folder -name sample.txt -exec rm -i {} \; ` |
119 | | |
120 | | 4. Search for empty files and directories. |
121 | | |
122 | | `$ find ./folder -empty` |
123 | | |
124 | | 5. Search for file with entered permissions. |
125 | | |
126 | | `$ find ./folder -perm 664` |
127 | | |
128 | | 6. Search text within multiple files. |
129 | | |
130 | | `$ find ./ -type f -name "*.txt" -exec grep 'Geek' {} \;` |
131 | | |
132 | | |
133 | | ==== grep ==== |
134 | | |
135 | | 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). |
136 | | Syntax: |
137 | | |
138 | | grep [options] pattern [files] |
139 | | |
140 | | 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”. |
141 | | |
142 | | `$grep -i "UNix" file.txt` |
143 | | |
144 | | 2. Displaying the count of number of matches : We can find the number of lines that matches the given string/pattern |
145 | | |
146 | | `$grep -c "unix" file.txt` |
147 | | |
148 | | 3. Display the file names that matches the pattern : We can just display the files that contains the given string/pattern. |
149 | | |
150 | | `$grep -l "unix" *` |
151 | | |
152 | | or |
153 | | |
154 | | `$grep -l "unix" f1.txt f2.txt f3.xt f4.txt` |
155 | | |
156 | | 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. |
157 | | |
158 | | `$ grep -w "unix" file.txt` |
159 | | |
160 | | 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. |
161 | | |
162 | | `$ grep -o "unix" file.txt` |
163 | | |
164 | | 6. Show line number while displaying the output using grep -n : To show the line number of file with the line matched. |
165 | | |
166 | | `$ grep -n "unix" file.txt` |
167 | | |
168 | | 7. Inverting the pattern match : You can display the lines that are not matched with the specified search string pattern using the -v option. |
169 | | |
170 | | `$ grep -v "unix" file.txt` |
171 | | |
172 | | 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. |
173 | | |
174 | | `$ grep "^unix" file.txt` |
175 | | |
176 | | 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. |
177 | | |
178 | | `$ grep "os$" file.txt` |
179 | | |
180 | | 10.Specifies expression with -e option. Can use multiple times : |
181 | | |
182 | | `$grep –e "Agarwal" –e "Aggarwal" –e "Agrawal" file.txt` |
183 | | |
184 | | 12. Search recursively for a pattern in the directory: -R prints the searched pattern in the given directory recursively in all the files. |
185 | | |
186 | | Syntax |
187 | | |
188 | | `$grep -R [Search] [directory]` |
189 | | |
190 | | ==== sed ==== |
191 | | |
192 | | 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. |
193 | | |
194 | | - SED is a powerful text stream editor. Can do insertion, deletion, search and replace(substitution). |
195 | | - SED command in unix supports regular expression which allows it perform complex pattern matching. |
196 | | |
197 | | 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. |
198 | | |
199 | | `$sed 's/unix/linux/' geekfile.txt` |
200 | | |
201 | | 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. |
202 | | |
203 | | `$sed 's/unix/linux/2' geekfile.txt` |
204 | | |
205 | | 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. |
206 | | |
207 | | `$sed 's/unix/linux/g' geekfile.txt` |
208 | | |
209 | | 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. |
210 | | |
211 | | `$sed 's/unix/linux/3g' geekfile.txt` |
212 | | |
213 | | 5.Parenthesize first character of each word : This sed example prints the first character of every word in parenthesis. |
214 | | |
215 | | `$ echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g'` |
216 | | |
217 | | 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 |
218 | | |
219 | | `$sed '3 s/unix/linux/' geekfile.txt` |
220 | | |
221 | | 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. |
222 | | |
223 | | `$sed 's/unix/linux/p' geekfile.txt` |
224 | | |
225 | | 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. |
226 | | |
227 | | `$sed -n 's/unix/linux/p' geekfile.txt` |
228 | | |
229 | | 9.Replacing string on a range of lines : You can specify a range of line numbers to the sed command for replacing a string. |
230 | | |
231 | | `$sed '1,3 s/unix/linux/' geekfile.txt` |
232 | | |
233 | | 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 |
234 | | Examples: |
235 | | 1.To Delete a particular line say n in this example |
236 | | Syntax: |
237 | | `$ sed 'nd' filename.txt` |
238 | | |
239 | | Example: |
240 | | `$ sed '5d' filename.txt` |
241 | | |
242 | | 2. To Delete a last line |
243 | | |
244 | | Syntax: |
245 | | |
246 | | `$ sed '$d' filename.txt` |
247 | | |
248 | | 3. To Delete line from range x to y |
249 | | |
250 | | Syntax: |
251 | | |
252 | | `$ sed 'x,yd' filename.txt` |
253 | | |
254 | | Example: |
255 | | |
256 | | `$ sed '3,6d' filename.txt` |
257 | | |
258 | | 4. To Delete from nth to last line |
259 | | |
260 | | Syntax: |
261 | | |
262 | | `$ sed 'nth,$d' filename.txt` |
263 | | |
264 | | Example: |
265 | | |
266 | | `$ sed '12,$d' filename.txt` |
267 | | |
268 | | 5. To Delete pattern matching line |
269 | | |
270 | | Syntax: |
271 | | `$ sed '/pattern/d' filename.txt` |
272 | | |
273 | | Example: |
274 | | |
275 | | `$ sed '/abc/d' filename.txt` |
276 | | |
277 | | ==== awk ==== |
278 | | |
279 | | 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. |
280 | | |
281 | | Syntax: |
282 | | |
283 | | `awk options 'selection _criteria {action }' input-file > output-file` |
284 | | |
285 | | 1. Default behavior of Awk: By default Awk prints every line of data from the specified file. |
286 | | |
287 | | `$ awk '{print}' employee.txt` |
288 | | |
289 | | 2. Print the lines which match the given pattern. |
290 | | |
291 | | `$ awk '/manager/ {print}' employee.txt ` |
292 | | |
293 | | 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. |
294 | | |
295 | | `$ awk '{print $1,$4}' employee.txt` |
296 | | |
297 | | |
298 | | |
299 | | |
300 | | |
301 | | |
302 | | |
303 | | |
304 | | |
305 | | |
306 | | |