Awk is more complex and difficult to master than grep and sed because it can be used as a programming language. It’s hard to get back to, but this command is a must, because it’s so powerful!

The basic structure of AWK is as follows:

Awk [options] 'Pattern1 {action1} patten2 {action2}... ' filenameCopy the code
  • Single quotes are used to distinguish them from shell commands.

  • Braces indicate a group of commands. It can be a single action or multiple actions. If it is multiple actions, it needs to be added between actions. Or enter

  • Pattern is a pattern, indicating that action is performed only on matched rows. Patterns can be regular, arithmetic expressions, and so on

  • You can have either pattern or action, but not both

The common options for AWK are as follows:

  • -f: Specifies the separator. The separator can be a character or a regular expression

  • -v val=value, defines a variable and assigns a value

Here, I’ll explain the use of the AWk command through an application. The following is part of a text file

# head city.txt BEIJING BEIJING BJ SHANGHAI SH TIANJIN TJ CHONGQING ZQ AKESU AKS......Copy the code

Now the requirement is to take the full name of each city, convert it to lowercase and connect to Hellowx.com. All other information is filtered out.

# awk 'NR % 2 = = 0 {next} {print}' city. TXT | head -n 10 BEIJING BEIJING SHANGHAI SH BJ SHANGHAI TIANJIN TIANJIN TJ CHONGQING CHONGQING ZQ aksu AKESU AKS peace ANNING AN ANQING AQ ANSHAN AS ANSHUN AS ANYANG AYCopy the code

Notice that even rows are blank. So you just filter even rows. This filters out blank lines. Note that NR represents the current line number, meaning that all even lines are filtered out. Next means to ignore the current row.

Next, you need to filter the first and third fields.

# awk 'NR%2==0{next}{print $2}' city.txt  | head -n 10BEIJINGSHANGHAITIANJINCHONGQINGAKESUANNINGANQINGANSHANANSHUNANYANG
Copy the code

The $2 above indicates the second field, combined with print to indicate that only the second field is printed. Finally, the conversion and connection work comes down to using tr.

# awk 'NR%2==0{next}{print $2}' city.txt | head -n 10 | tr [A-Z] [a-z] | awk '{print $1"hellowx.com"}'beijinghellowx.comshanghaihellowx.comtianjinhellowx.comchongqinghellowx.comakesuhellowx.comanninghellow x.comanqinghellowx.comanshanhellowx.comanshunhellowx.comanyanghellowx.comCopy the code

Tr is used to convert all uppercase to lowercase and then use awk to concatenate the string.

The commands mentioned above may not be easy to understand if you are not familiar with them. Suggestions can be taken step by step. Familiarize yourself with step 1 and then go on to understand Part 2.

Finally, I hope you can seriously learn this command, if you master AWK, you can do a lot of interesting things!