当前位置:网站首页>Text processing tool in shell, cut [option parameter] filename Description: the default separator is the built-in variable of tab, awk [option parameter] '/pattern1/{action1}filename and awk
Text processing tool in shell, cut [option parameter] filename Description: the default separator is the built-in variable of tab, awk [option parameter] '/pattern1/{action1}filename and awk
2022-07-27 04:42:00 【Redamancy06】
1. Text processing tools
1.1cut
cut The job of “ cut ”, Specifically, it is used to cut data in the file .cut Command to cut bytes from each line of a file 、 Characters and fields and put these bytes 、 Character and field output .
1.1.1 Basic usage
cut [ Option parameters ] filename
explain : The default separator is tab
1.1.2 Option parameter description
| Option parameters | function |
|---|---|
| -f | Column number , Extract which column |
| -d | Separator , Splits columns according to the specified separator , The default is tabs “\t” |
| -c | Cut by character Add after n It means which column to take such as -c 1 |
1.1.3 Case study
1.1.3.1 Data preparation
[[email protected] scripts]# vim cut_test.txt
1.1.3.2 cutting cut_test.txt First column
[[email protected] scripts]# cut -d " " -f 1 cut_test.txt
1.1.3.3 cutting cut_test.txt second 、 The three column
[[email protected] scripts]# cut -d " " -f 2,3 cut_test.txt
1.1.3.4 stay cut_test.txt Cut out in file nan
[[email protected] scripts]# cat cut_test.txt | grep nan | cut -d " " -f 1

1.1.3.5 want passwd China and Israel bash First at the end , 6、 ... and , Seven columns
[[email protected] scripts]# cat /etc/passwd | grep bash$ | cut -d “:” -f 1,6,7

If there are too many columns, you can't 1,2,3,4,5,6,7,8,9,10… Wait , It can be used “-” Come on , Let's introduce
1.1.3.6 want passwd China and Israel bash At the end of the paragraph 1-4 Column
[[email protected] scripts]# cat /etc/passwd | grep bash$ | cut -d “:” -f 1-4

1.1.3.7 want passwd China and Israel bash At the end of the paragraph 4 All columns after column
[[email protected] scripts]# cat /etc/passwd | grep bash$ | cut -d “:” -f 4-

1.1.3.8 want passwd China and Israel bash At the end of the paragraph 4 All columns before column
[[email protected] scripts]# cat /etc/passwd | grep bash$ | cut -d “:” -f -4

1.1.3.9 Selection system PATH A variable's value , The first 2 individual “:” All paths after start :
[[email protected] scripts]# echo $PATH | cut -d “:” -f 3-

1.1.3.10 cutting ifconfig Post print IP Address
[[email protected] scripts]# ifconfig ens33 | grep netmask | cut -d " " -f 10
Why 10 Well , because inet There is 8 A space 
1.2awk
awk and gawk It's the same ,awk yes gawk A soft connection of
A powerful text analysis tool , Read the document line by line , Slice each line with a space as the default separator , The cut part is analyzed again .
1.2.1 Basic usage
awk [ Option parameters ] ‘/pattern1/{action1} /pattern2/{action2}…’ filename
pattern: Express awk What to look for in the data , It's a matching pattern
action: A series of commands executed when a match is found
1.2.2 Option parameter description
| Option parameters | function |
|---|---|
| -F | Specify the input file separator |
| -v | Assign a user-defined variable |
1.2.3 Case study
1.2.3.1 Search for passwd Document to root All lines at the beginning of the keyword , And output the 7 Column

1.2.3.2 Search for passwd Document to root All lines at the beginning of the keyword , And output the 1 Column and the first 7 Column , In the middle to “,” Division of no.
use cut You cannot change what is used to separate the output , and awk Sure , remember , Need to use “” The parcel 
1.2.3.3 Display only /etc/passwd The first and seventh columns of , Comma separated , And add column names before all rows user,shell Add on last line "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 take passwd Users in files id Increase in numerical value 1 And the output


because {} There are code blocks , You can change it directly inside

If {} There is too much code in the code block , It will be troublesome to change , Therefore use -v, In this way, when you change the number outside, the code inside the code block will be changed
1.2.4awk Built in variables for
| Variable | explain |
|---|---|
| FILENAME | file name |
| NR | Number of records read ( Line number ) |
| NF | The number of fields in the browsing record ( After cutting , Number of columns ) |
1.2.4.1 Statistics passwd file name , Line number of each line , Columns per row
[[email protected] scripts]# awk -v i=1 -F “:” ‘{print “ file name :” FILENAME “ Line number :” NR “ Number of columns :” NF}’ /etc/passwd

1.2.4.2 Inquire about ifconfig The line number of the empty line in the command output result
[[email protected] scripts]# ifconfig | grep -n ^$

Output with the previous method will have ":", But the result is not desired , use awk solve
[[email protected] scripts]# ifconfig | awk ‘/^$/ {print NR}’

You can also add things at will
[[email protected] scripts]# ifconfig | awk ‘/^$/ {print " Blank line :"NR}’

1.2.4.3 cutting IP
Use cut when -f After that, you need to count the pile of spaces in front

Use awk after , There is no need to count the first pile of spaces 
边栏推荐
- Horizon sunrise X3 PI (IV) running on board (unfinished)
- P1438 boring sequence line segment tree + difference
- [hcip] redistribute, redistribute and redistribute experiments
- Sed output specified line
- How does novice Xiaobai learn to be we media?
- 好用移动APP自动化测试框架哪里找?收藏这份清单就好了!
- Easy to use mobile app automation testing framework where to find? Just collect this list!
- 5.component动态组件的展示
- 【AtCoder Beginner Contest 260 (A·B·C)】
- 无有线网络下安装并配置debian
猜你喜欢

Vscode opens a new chapter in the visualization of pull request update code branches

干货 | 独立站运营怎么提高在线聊天客户服务?

Scala immutable map, variable map, map conversion to other data types

The project parameters are made into configurable items, and the @configurationproperties annotation is used
![[hcip] redistribute, redistribute and redistribute experiments](/img/53/18e1a589a85b508675c97a0108aee6.png)
[hcip] redistribute, redistribute and redistribute experiments

结构型模式-装饰者模式

JS three methods of traversing arrays: map, foreach, filter

Unity:Resource Merging、Static Batching、Dynamic Batching、GPU Instancing
![Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量](/img/ed/941276a15d1c4ab67d397fb3286022.png)
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量

Unity:Resource Merging、Static Batching、Dynamic Batching、GPU Instancing
随机推荐
Effect Hook
【AtCoder Beginner Contest 260 (A·B·C)】
RSA 非对称 加密解密 加签验签工具
JS day 2 (variables, variable usage, naming rules, syntax extensions)
Dry goods | how can independent station operation improve online chat customer service?
【C语言】递归详解汉诺塔问题
Ref Hook
timestamp列使用varchar类型和使用date类型有什么区别?
冒泡排序(详细)
Oracle数据库字段date怎么才能走索引?
Hash table questions (Part 2)
F - Pre-order and In-order(Atcoder 255)
Head detached from origin/... Causes push failure
The external symbol parsed by the method "public: virtual _ucdecl nvinfer1:: yololayerplugin:: ~yololayerplugin (void)" "public: virtual
Database leader Wang Shan: strive for innovation and carefully Polish high-quality database products
Ribbon load balancing strategy and configuration, lazy loading and hungry loading of ribbon
Standard C language 11
shell编程增强
负数的右移
Explain left value, right value, left value reference and right value reference in detail