常用脚本命令sort head tail grep awk sed uniq

常用脚本命令sort head tail grep sed awk uniq

sort

ls -l|grep G |sort -n -k 5 -t ’ ’ -r

按照数值排序 -n
按照 空格分隔(-t)后的第5列(-k) 排序
降序 -r

~ ls -l                                     
total 0
drwx------@  4 yuan  staff   128 12  9  2021 Applications
drwxr-xr-x   6 yuan  staff   192  1 24 22:50 Calibre 书库
drwx------@ 14 yuan  staff   448  3 27 15:53 Desktop
drwx------+ 19 yuan  staff   608  2  2 10:31 Documents
drwx------@ 59 yuan  staff  1888  3 26 19:17 Downloads
drwxr-xr-x@ 28 yuan  staff   896  3 21 10:23 GitHubCode
drwxr-xr-x@ 11 yuan  staff   352  2  3  2022 GitLabCode
drwx------@ 82 yuan  staff  2624 10 18 12:11 Library
drwx------  16 yuan  staff   512  2 17 17:07 Movies
drwx------+  7 yuan  staff   224 12 28 22:32 Music
drwx------+ 11 yuan  staff   352 10 20 11:13 Pictures
drwxr-xr-x+  4 yuan  staff   128  5  7  2021 Public
drwxr-xr-x   4 yuan  staff   128  4  6  2022 PycharmProjects
drwxr-xr-x  10 yuan  staff   320 10 22  2021 apache-maven-3.8.1
drwxr-xr-x@  6 yuan  staff   192  3  2 12:15 clean_data
drwxr-xr-x  11 yuan  staff   352  5 27  2022 myfile
drwxr-xr-x   5 yuan  staff   160  3 11 23:45 soft
drwxr-xr-x@  5 yuan  staff   160  6 20  2021 work
drwxr-xr-x@  6 yuan  staff   192  2 20 08:43 wroot
drwxr-xr-x@ 14 yuan  staff   448 12 24  2021 yw-shell
➜  ~ 
# 
➜  ~ ls -l|sort -n -k 5                        
total 0
drwx------@  4 yuan  staff   128 12  9  2021 Applications
drwxr-xr-x   4 yuan  staff   128  4  6  2022 PycharmProjects
drwxr-xr-x+  4 yuan  staff   128  5  7  2021 Public
drwxr-xr-x   5 yuan  staff   160  3 11 23:45 soft
drwxr-xr-x@  5 yuan  staff   160  6 20  2021 work
drwxr-xr-x   6 yuan  staff   192  1 24 22:50 Calibre 书库
drwxr-xr-x@  6 yuan  staff   192  2 20 08:43 wroot
drwxr-xr-x@  6 yuan  staff   192  3  2 12:15 clean_data
drwx------+  7 yuan  staff   224 12 28 22:32 Music
drwxr-xr-x  10 yuan  staff   320 10 22  2021 apache-maven-3.8.1
drwx------+ 11 yuan  staff   352 10 20 11:13 Pictures
drwxr-xr-x  11 yuan  staff   352  5 27  2022 myfile
drwxr-xr-x@ 11 yuan  staff   352  2  3  2022 GitLabCode
drwx------@ 14 yuan  staff   448  3 27 15:53 Desktop
drwxr-xr-x@ 14 yuan  staff   448 12 24  2021 yw-shell
drwx------  16 yuan  staff   512  2 17 17:07 Movies
drwx------+ 19 yuan  staff   608  2  2 10:31 Documents
drwxr-xr-x@ 28 yuan  staff   896  3 21 10:23 GitHubCode
drwx------@ 59 yuan  staff  1888  3 26 19:17 Downloads
drwx------@ 82 yuan  staff  2624 10 18 12:11 Library
➜  ~ 
➜  ~ ls -l|sort -nrk 5 
drwx------@ 82 yuan  staff  2624 10 18 12:11 Library
drwx------@ 59 yuan  staff  1888  3 26 19:17 Downloads
drwxr-xr-x@ 28 yuan  staff   896  3 21 10:23 GitHubCode
drwx------+ 19 yuan  staff   608  2  2 10:31 Documents
drwx------  16 yuan  staff   512  2 17 17:07 Movies
drwxr-xr-x@ 14 yuan  staff   448 12 24  2021 yw-shell
drwx------@ 14 yuan  staff   448  3 27 15:53 Desktop
drwxr-xr-x@ 11 yuan  staff   352  2  3  2022 GitLabCode
drwxr-xr-x  11 yuan  staff   352  5 27  2022 myfile
drwx------+ 11 yuan  staff   352 10 20 11:13 Pictures
drwxr-xr-x  10 yuan  staff   320 10 22  2021 apache-maven-3.8.1
drwx------+  7 yuan  staff   224 12 28 22:32 Music
drwxr-xr-x@  6 yuan  staff   192  3  2 12:15 clean_data
drwxr-xr-x@  6 yuan  staff   192  2 20 08:43 wroot
drwxr-xr-x   6 yuan  staff   192  1 24 22:50 Calibre 书库
drwxr-xr-x@  5 yuan  staff   160  6 20  2021 work
drwxr-xr-x   5 yuan  staff   160  3 11 23:45 soft
drwxr-xr-x+  4 yuan  staff   128  5  7  2021 Public
drwxr-xr-x   4 yuan  staff   128  4  6  2022 PycharmProjects
drwx------@  4 yuan  staff   128 12  9  2021 Applications
total 0
➜  ~ 

head tail

  • 前5行
    |head -n 5

  • 后3行
    |tail -3

  • 前5行中的后3行 (第3-5行)
    |head -5 |tail -3

  • 通过less再查看
    |less

➜  ~ 
➜  ~ ls -l|sort -nrk 5 |head -n5          
drwx------@ 82 yuan  staff  2624 10 18 12:11 Library
drwx------@ 59 yuan  staff  1888  3 26 19:17 Downloads
drwxr-xr-x@ 28 yuan  staff   896  3 21 10:23 GitHubCode
drwx------+ 19 yuan  staff   608  2  2 10:31 Documents
drwx------  16 yuan  staff   512  2 17 17:07 Movies
➜  ~ 
➜  ~ ls -l|sort -nrk 5 |head -5           
drwx------@ 82 yuan  staff  2624 10 18 12:11 Library
drwx------@ 59 yuan  staff  1888  3 26 19:17 Downloads
drwxr-xr-x@ 28 yuan  staff   896  3 21 10:23 GitHubCode
drwx------+ 19 yuan  staff   608  2  2 10:31 Documents
drwx------  16 yuan  staff   512  2 17 17:07 Movies
➜  ~ ls -l|sort -nrk 5 |head -5 |tail -3  
drwxr-xr-x@ 28 yuan  staff   896  3 21 10:23 GitHubCode
drwx------+ 19 yuan  staff   608  2  2 10:31 Documents
drwx------  16 yuan  staff   512  2 17 17:07 Movies

➜  ~ 
➜  ~ ls -l|sort -nrk 5 |head -5 |tail -3 |less |grep Doc
drwx------+ 19 yuan  staff   608  2  2 10:31 Documents

grep

正则匹配

# 正则匹配 
tail -f app.log |grep -P 'ueue: \d{0,3}00|done'

# 包含 a或b
tail -f app.log |grep -E 'a|b'

echo

# 输出测试数据序列 300个序列,每行10个
➜  ~ echo {1..300} | xargs -n 10

# 输出测试数据序列 到指定文件
➜  ~ echo {1..300} | xargs -n 10 > a.txt
➜  ~ less a.txt 
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
101 102 103 104 105 106 107 108 109 110
111 112 113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128 129 130
131 132 133 134 135 136 137 138 139 140
141 142 143 144 145 146 147 148 149 150
151 152 153 154 155 156 157 158 159 160
161 162 163 164 165 166 167 168 169 170
171 172 173 174 175 176 177 178 179 180
181 182 183 184 185 186 187 188 189 190
191 192 193 194 195 196 197 198 199 200
201 202 203 204 205 206 207 208 209 210
211 212 213 214 215 216 217 218 219 220
221 222 223 224 225 226 227 228 229 230
231 232 233 234 235 236 237 238 239 240
241 242 243 244 245 246 247 248 249 250
251 252 253 254 255 256 257 258 259 260
261 262 263 264 265 266 267 268 269 270
271 272 273 274 275 276 277 278 279 280
281 282 283 284 285 286 287 288 289 290
291 292 293 294 295 296 297 298 299 300

awk

awk 入门

# 获取第2列,默认分隔符为空格
| awk '{print $2}'

#  变量NR表示当前处理行
| awk '{print NR ") "$2}'


# -F,用,号分隔,获取最后一列
|awk -F ',' '{print $NF}'


# 将记录按照 \t 需要切分开 对齐
ps -ef|awk '{print $2 "\t" $8}'

sed

#取第1-5行
➜  ~ ps -ef|sed -n 1,5p

# 打印进程1-5行(第一行为表头),获取第2,8列
➜  ~ ps -ef|sed -n 1,5p | awk '{print $2 "\t" $8}'

# 查找前五个最大的文件
➜  ~ ls -l|sort -nrk 5|sed -n 1,5p
drwx------@ 82 yuan  staff  2624 10 18 12:11 Library
drwx------@ 59 yuan  staff  1888  3 26 19:17 Downloads
drwxr-xr-x@ 28 yuan  staff   896  3 21 10:23 GitHubCode
drwx------+ 19 yuan  staff   608  2  2 10:31 Documents
drwx------  16 yuan  staff   512  2 17 17:07 Movies

uniq

uniq是去重,不相邻的行不算重复值。
建议先排序再去重

选项说明:
-c:统计出现的次数(count)。
-d:只显示被计算为重复的行。
-D:显示所有被计算为重复的行。
-u:显示唯一值,即没有重复值的行。
-i:忽略大小写。
-f:跳过多少个字段(field)开始比较重复值。
-s:跳过多少个字符开始比较重复值。

# 原始文件
root@yuan>cat uniq.log 
111
222
33
111
111
333
222

# 相邻去重,不相邻的不会去重
root@yuan>uniq uniq.log 
111
222
33
111
333
222

# 排序后去重
root@yuan>sort uniq.log|uniq 
111
222
33
333

# -d显示重复的行
root@yuan>sort uniq.log|uniq -d
111
222

# -D 显示所有重复过的行。
root@yuan>sort uniq.log|uniq -D
111
111
111
222
222

# -u显示唯一行
root@yuan>sort uniq.log|uniq -u
33
333

# -c统计所有记录出现的次数
root@yuan>sort uniq.log|uniq -c
      3 111
      2 222
      1 33
      1 333

# -d -c统计重复行出现的次数
root@yuan>sort uniq.log|uniq -d -c
      3 111
      2 222

# 搜索ERROR日志文件 获取第二列 排序并去重
less app.log |grep ERROR |awk '{print $2}' |sort | uniq -c
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值