Notes on grep, awk and cut

grep can be a super useful command when combined with other commands in processing documents. This is a post summarizing some useful cases.

This post has included many techniques first taught to me by my office mate Vladimir Kiriansky, many thanks.  This post like other tools posts, will be updated every now and then when I learn a new trick.

One example grep command
 less log_file | grep pattern | less | cut -f2 -d: | head -n30
The top 30 occurrences of the pattern, only take the second column
grep for multiple patterns

less log_file | grep -E ‘pattern1|pattern2

grep using a specific column of the data (cut out the useful number)

less log_file | grep ‘pattern1‘ | awk ‘{print $2}’

[print the second column of the line that has pattern1]

copy the results of grep to a clipboard that we can copy somewhere else (e.g. spreadshee) for further analysis

less log_file | grep ‘pattern1‘ | awk ‘{print $2}’ | pbcopy

Use awk to cut out content before an extension

(recognize ‘.’, and print the first column)

ls dir | awk -F. ‘{print $1}’

you can do the same thing with cut

(split by “.”, and pick the first column)

ls dir | cut -d. -f1

Some more awk magic! cut the third column 

awk '!($3="")' file
Advertisements
This entry was posted in Tools, Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s