linux 命令记录 @ Thu, Mar 2, 2023 12:20 PM
linux 命令记录 @ Thu, Mar 2, 2023 1:49 PM
Expand 1 lines ...
2
title: linux 命令记录
2
title: linux 命令记录
3
author: powerfulyang
3
author: powerfulyang
4
date: 2023-01-09
4
date: 2023-01-09
5
-
summary: linux 查看目录文件列表
5
+
summary: linux 查看目录文件列表,学习超好用的 awk 命令
6
public: true
6
public: true
7
tags: 
7
tags: 
8
    - linux command
8
    - linux command
Expand 15 lines ...
24
24
25
使用 \ 来转义就好啦。比如使用 ifconfig 命令来查看 ipv4 地址 `ifconfig | grep inet\ `
25
使用 \ 来转义就好啦。比如使用 ifconfig 命令来查看 ipv4 地址 `ifconfig | grep inet\ `
26
+
27
+
## 超有用的 awk 命令
28
+
29
+
> https://www.geeksforgeeks.org/awk-command-unixlinux-examples/
30
+
31
+
**Syntax:**  
32
+
```shell
33
+
awk options 'selection _criteria {action }' input-file > output-file
34
+
```
35
+
**Options:**  
36
+
```shell
37
+
-f program-file : Reads the AWK program source from the file 
38
+
                  program-file, instead of from the 
39
+
                  first command line argument.
40
+
-F fs            : Use fs for the input field separator
41
+
```
42
+
**Example:**
43
+
44
+
```example.txt
45
+
ajay manager account 45000
46
+
sunil clerk account 25000
47
+
varun manager sales 50000
48
+
amit manager account 47000
49
+
tarun peon sales 15000
50
+
deepak clerk sales 23000
51
+
sunil peon sales 13000
52
+
satvik director purchase 80000 
53
+
```
54
+
55
+
+ **Print the lines which match the given pattern.**
56
+
  ```shell
57
+
  awk '/manager/ {print}' employee.txt 
58
+
  ```
59
+
  output:
60
+
  ```output
61
+
  ajay manager account 45000
62
+
  varun manager sales 50000
63
+
  amit manager account 47000 
64
+
  ```
65
+
+ **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.
66
+
  ```shell
67
+
  awk '{print $1,$4}' employee.txt 
68
+
  ```
69
+
  output:
70
+
  ```output
71
+
  ajay 45000
72
+
  sunil 25000
73
+
  varun 50000
74
+
  amit 47000
75
+
  tarun 15000
76
+
  deepak 23000
77
+
  sunil 13000
78
+
  satvik 80000 
79
+
  ```
80
+
+ **macOS 获取某个网段的 ip 地址**
81
+
  ```shell
82
+
  ifconfig | grep 172.16.16 | awk '{print $2}'
83
+
  ```
84
+
  output:
85
+
  ```output
86
+
  172.16.16.xxx
87
+
  ```
88
+
89
+
### Built-In Variables In Awk
90
+
91
+
Awk’s built-in variables include the field variables—\$1, \$2, \$3, and so on (\$0 is the entire line) — that break a line of text into individual words or pieces called fields. 
92
+
93
+
* **NR:** NR command keeps a current count of the number of input records. Remember that records are usually lines. Awk command performs the pattern/action statements once for each record in a file. 
94
+
* **NF:** NF command keeps a count of the number of fields within the current input record. 
95
+
* **FS:** FS command contains the field separator character which is used to divide fields on the input line. The default is “white space”, meaning space and tab characters. FS can be reassigned to another character (typically in BEGIN) to change the field separator. 
96
+
* **RS:** RS command stores the current record separator character. Since, by default, an input line is the input record, the default record separator character is a newline. 
97
+
* **OFS:** OFS command stores the output field separator, which separates the fields when Awk prints them. The default is a blank space. Whenever print has several parameters separated with commas, it will print the value of OFS in between each parameter. 
98
+
* **ORS:** ORS command stores the output record separator, which separates the output lines when Awk prints them. The default is a newline character. print automatically outputs the contents of ORS at the end of whatever it is given to print.  
99
+
100
+
**Use of NR built-in variables (Display Line Number)**  
101
+
102
+
```shell
103
+
awk '{print NR,$0}' employee.txt 
104
+
```
105
+
106
+
**Output:**  
107
+
108
+
```output
109
+
1 ajay manager account 45000
110
+
2 sunil clerk account 25000
111
+
3 varun manager sales 50000
112
+
4 amit manager account 47000
113
+
5 tarun peon sales 15000
114
+
6 deepak clerk sales 23000
115
+
7 sunil peon sales 13000
116
+
8 satvik director purchase 80000 
117
+
```
118
+
119
+
**Use of NF built-in variables (Display Last Field)**  
120
+
121
+
```shell
122
+
awk '{print $1,$NF}' employee.txt 
123
+
```
124
+
125
+
**Output:**  
126
+
127
+
```output
128
+
ajay 45000
129
+
sunil 25000
130
+
varun 50000
131
+
amit 47000
132
+
tarun 15000
133
+
deepak 23000
134
+
sunil 13000
135
+
satvik 80000 
136
+
```
137
+
138
+
**Another use of NR built-in variables (Display Line From 3 to 6)**  
139
+
140
+
```shell
141
+
awk 'NR==3, NR==6 {print NR,$0}' employee.txt 
142
+
```
143
+
144
+
**Output:**  
145
+
146
+
```output
147
+
3 varun manager sales 50000
148
+
4 amit manager account 47000
149
+
5 tarun peon sales 15000
150
+
6 deepak clerk sales 23000 
151
+
```