DEV Community

Vitali Pomanitski
Vitali Pomanitski

Posted on


AWK, Find & Grep

Beginners have frequent struggle in working with text under Unix environment, so this is short but comprehensive tutorial on the level of topics, which will cover up everything you need to know about grep, find and awk. And this is also how I got to know about them.

In Unix, you need to parse mass amount of files, possibly filtered by some regex (find command) and printout the result (grep), while you also need to printout some params from a command line running function sometimes (which is awk and it is very cool).

First of all, find command has a special "filters" syntax, in terms that your flags combine an actual filter, and furthermore you can combine a boolean expression out of them. For example, you can do filter -type -d (-iregex abc -o -iregex dbe) -exec... and it will match anything which matches abc or dbe and combine them both in the end result. -o stands for or and the parentheses are actual parentheses. For those directories, it will execute some command... in -exec you can specify "{}" which is a special character, used as the name of the found file or directory. Unix parses takes the command and wherever {} appears, it replaces them with the name of file/folder. Why {}? Because it's easy to find and match it in a command line.

Find will also always match all sub files in all sub directories.

If you want to grep inside the files you found, you can run grep. Unix grep runs on files, what it does is cat and some script for grepping and printing the regular expression, line that contains it and tge filename as needed. But what is required to remember is that grep runs on filenames, it runs on files. Files within directory, single files, but it will run on files. Don't forget it when you state it tge arguments.

You can set Perl regular expression sometimes in Unix, using the -P flag, not always. Check it against your command and learn unix regex anyways.

Confused on the file permissions? Take calculator on windows for once and for all.
2,3 will never used. It's only write and only execute write. Why would you write but not read for god's sake?
456 is easy. Read, write, read write exec. It's a poem. You always get a read permission, you add write to it or you add exec to it. 7 has them all. 
Enter fullscreen mode Exit fullscreen mode

Awk is cool, because it lets you print the n's element in the command line. It also has BEGIN option, which allows you to printout all the lines for xyz and filter them with grep (STDOUT is the filename it will run on, default bytheway) and if it - the output shall have some header like title or table, awk might add it if you use it inside the BEGIN statement which states what to printout in the beginning.

Inside awk you state your awk commands, (remember those 're awk commands) within the {} brackets which comes inside a ''. With $ sign you state the n-s element to printout like in perl.

Sed is also in tge list but feels off topic for this list.

Top comments (0)

An Animated Guide to Node.js Event Loop

>> Check out this classic DEV post <<