Awk the swiss army knife of the Unix toolkit.

The awk command is very handy when you need to filter the contents of a file, a pipe,or keyboard. It searches each line of input for patterns that you specify and when the pattern is matched, it performs an action. You specify the action and patterns.

The action could be to count the number of times the word youtube appears in a file. There are many action options, it is really up to you to figure out how you can manipulate it.

Here is an example, suppose you have the following text in the file myfile.

What-is-the-word-to-remove-from-this-line.

awk -F-remove ‘/-remove/ {print $1}’ myfile

-F-remove, is the field separator. In this case the separator is -remove, it separates the text before and after it into separate fields, logically speaking.

-remove, is the pattern to match, it has been defined as a field separator earlier with the -F option. The pattern is enclosed within /.

print $1, prints all text in the first column. The column separator has been defined as the word -remove. The input is from the file myfile.

Here is the result of the awk command.

what-is-the-word-to

About Andrew Lin

Hi, I have always wanted to creat a blog site but never had the time. I have been working in Information Technology for over 15 years. I specialize mainly in networks and server technologies and dabble a little with the programming aspects. Andrew Lin

View all posts by Andrew Lin →