当前位置:网站首页>Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量
2022-07-27 03:44:00 【Redamancy06】
1.文本处理工具
1.1cut
cut 的工作就是“剪”,具体的说就是在文件中负责剪切数据用的。cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段输出。
1.1.1基本用法
cut [选项参数] filename
说明:默认分隔符是制表符
1.1.2选项参数说明
| 选项参数 | 功能 |
|---|---|
| -f | 列号,提取第几列 |
| -d | 分隔符,按照指定分隔符分割列,默认是制表符“\t” |
| -c | 按字符进行切割 后加加 n 表示取第几列 比如 -c 1 |
1.1.3案例
1.1.3.1数据准备
[[email protected] scripts]# vim cut_test.txt
1.1.3.2切割 cut_test.txt 第一列
[[email protected] scripts]# cut -d " " -f 1 cut_test.txt
1.1.3.3切割 cut_test.txt 第二、三列
[[email protected] scripts]# cut -d " " -f 2,3 cut_test.txt
1.1.3.4在 cut_test.txt 文件中切割出 nan
[[email protected] scripts]# cat cut_test.txt | grep nan | cut -d " " -f 1

1.1.3.5要passwd中以bash结尾的第一,六,七列
[[email protected] scripts]# cat /etc/passwd | grep bash$ | cut -d “:” -f 1,6,7

如果列多的话总不能1,2,3,4,5,6,7,8,9,10…等吧,可以用“-”来,下面介绍
1.1.3.6要passwd中以bash结尾的第1-4列
[[email protected] scripts]# cat /etc/passwd | grep bash$ | cut -d “:” -f 1-4

1.1.3.7要passwd中以bash结尾的第4列往后的全部列
[[email protected] scripts]# cat /etc/passwd | grep bash$ | cut -d “:” -f 4-

1.1.3.8要passwd中以bash结尾的第4列之前的全部列
[[email protected] scripts]# cat /etc/passwd | grep bash$ | cut -d “:” -f -4

1.1.3.9选取系统 PATH 变量值,第 2 个“:”开始后的所有路径:
[[email protected] scripts]# echo $PATH | cut -d “:” -f 3-

1.1.3.10切割 ifconfig 后打印的 IP 地址
[[email protected] scripts]# ifconfig ens33 | grep netmask | cut -d " " -f 10
为什么是10呢,因为inet前面有8个空格
1.2awk
awk和gawk是一样的,awk是gawk的一个软连接
一个强大的文本分析工具,把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行分析处理。
1.2.1基本用法
awk [选项参数] ‘/pattern1/{action1} /pattern2/{action2}…’ filename
pattern:表示 awk 在数据中查找的内容,就是匹配模式
action:在找到匹配内容时所执行的一系列命令
1.2.2选项参数说明
| 选项参数 | 功能 |
|---|---|
| -F | 指定输入文件分隔符 |
| -v | 赋值一个用户定义变量 |
1.2.3案例
1.2.3.1搜索 passwd 文件以 root 关键字开头的所有行,并输出该行的第 7 列

1.2.3.2搜索 passwd 文件以 root 关键字开头的所有行,并输出该行的第 1 列和第 7 列, 中间以“,”号分割
用cut是不能改变输出中间用什么隔开的,而awk可以,记住,需要用“”包裹
1.2.3.3只显示/etc/passwd 的第一列和第七列,以逗号分割,且在所有行前面添加列名 user,shell 在最后一行添加"end of file"
[[email protected] scripts]# cat /etc/passwd | awk -F “:” ‘BEGIN{print “user,shell”}{print $1","$7} END{print “end of file”}’


1.2.3.4将 passwd 文件中的用户 id 增加数值 1 并输出


因为{}里面是代码块,可以直接在里面更改

如果{}代码块里的代码多了,需要更改会很麻烦,因此使用-v,这样的话在外面一改数字代码块里面的代码就都改了
1.2.4awk 的内置变量
| 变量 | 说明 |
|---|---|
| FILENAME | 文件名 |
| NR | 已读的记录数(行号) |
| NF | 浏览记录的域的个数(切割后,列的个数) |
1.2.4.1统计 passwd 文件名,每行的行号,每行的列数
[[email protected] scripts]# awk -v i=1 -F “:” ‘{print “文件名:” FILENAME “行号:” NR “列数:” NF}’ /etc/passwd

1.2.4.2查询 ifconfig 命令输出结果中的空行所在的行号
[[email protected] scripts]# ifconfig | grep -n ^$

用之前的方法输出会有":",但是结果不想要,用awk解决
[[email protected] scripts]# ifconfig | awk ‘/^$/ {print NR}’

也可以随意加东西
[[email protected] scripts]# ifconfig | awk ‘/^$/ {print "空行:"NR}’

1.2.4.3切割 IP
使用cut时-f后需要数前面那一堆空格

使用awk后,前面那一堆空格是不需要数的
边栏推荐
- The difference between ArrayList and LinkedList
- Brightcove appoints Dan Freund as chief revenue Officer
- Ant JD Sina 10 architects 424 page masterpiece in-depth distributed cache from principle to practice pdf
- 【day02】数据类型转换、运算符、方法入门
- Elastic certification test: 30 day FastPass Study Guide
- 通信协议综述
- Easy to use shell shortcuts
- Elastic open source community: Developer Recruitment
- Principle of bean validation --07
- Ribbon-负载均衡原理及部分源码
猜你喜欢

JMeter download and installation

Session&Cookie&token

数据库泰斗王珊:努力创新,精心打磨优质的数据库产品

Rust:axum学习笔记(1) hello world

【C语言】递归详解汉诺塔问题

Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)

How to set user-defined display for Jiaming Watch

Rust:axum learning notes (1) Hello World

卷积神经网络——24位彩色图像的卷积的详细介绍

你了解微信商户分账吗?
随机推荐
[C language] recursively explain the tower of Hanoi problem
一张图看懂KingbaseES V9
JS modify the key value of the object array
The real digital retail should have richer connotation and significance
JS three methods of traversing arrays: map, foreach, filter
php+swoole
微信input组件添加清除图标,点击清空不生效
VR panorama gold rush "careful machine" (Part 1)
Nacos启动与登录
Preliminary understanding of NiO
Install and configure Debian on a wired network
356 pages, 140000 words, weak current intelligent system of high-end commercial office complex, 2022 Edition
Detailed explanation of TCP protocol knowledge
2022-07-26:以下go语言代码输出什么?A:5;B:hello;C:编译错误;D:运行错误。 package main import ( “fmt“ ) type integer in
Wechat input component adds a clear icon, and clicking clear does not take effect
BSN IPFs (interstellar file system) private network introduction, functions, architecture and characteristics, access instructions
Post analysis of Data Analyst
BigDecimal pit summary & Best Practices
Is VR panoramic production a single weapon in the home decoration industry? Why is this?
Ribbon load balancing principle and some source codes