当前位置:网站首页>Shell -- text processing command
Shell -- text processing command
2022-06-29 22:49:00 【Wan Li Gu Cheng】
List of articles
Shell—— Text processing commands
1、cut
cut The command is used to display each line from the beginning num1 To num2 The characters of , Bytes and fields , And put these characters , Byte and field output .
Grammar format
cut [ Parameters ] file name
Parameters :
- -c : Split in characters , Back plus n It means which column to take , Such as -c 3.
- -d : Custom delimiter , The default separator is tab
“\t”. - -f : Specify which column to extract .
Using examples
Space as separator , Intercept and output text cut.txt The first column of characters :
[[email protected] home]# cat cut.txt
jin mao
jing xian
ling xia
long cai
chi neng
lan chi
fen ji
[[email protected] home]# cut -d " " -f 1 cut.txt
jin
jing
ling
long
chi
lan
fen
Combined with the use of pipe symbols : Search for passwd file , Use colon as separator , Cut off the 1,6,7 Column
[[email protected] home]# cat /etc/passwd |grep bash$
root:x:0:0:root:/root:/bin/bash
mysql:x:997:1001::/home/mysql:/bin/bash
es:x:1001:1002::/home/es:/bin/bash
[[email protected] home]# cat /etc/passwd | grep bash$| cut -d ":" -f 1,6,7
root:/root:/bin/bash
mysql:/home/mysql:/bin/bash
es:/home/es:/bin/bash
Combined with the use of pipe symbols : Use colon as separator , Intercept all columns before the third colon :
cut -d ":" -f -3: Intercept all columns before the third coloncut -d ":" -f 3-: Intercept all columns after the third column
[[email protected] home]# cat /etc/passwd | grep bash$| cut -d ":" -f -3
root:x:0
mysql:x:997
es:x:1001
2、awk
awk The command is to scan the file line by line ( From 1 Line to last line ), Find the line that contains the target text , If the match is successful , Then the user's desired action will be performed on that line ; conversely , Do nothing with the line .
Grammar format
awk [ Parameters ] ' Script command ' file name
Parameters :
-F fs : Specified in fs As a separator for the input line ,awk The default separator for the command is a space or tab .
-f file : Read from script file awk Script instructions , Instead of entering instructions directly on the command line .
-v var=val : Before executing the process , Set a variable var, The initial value of the device is val.
Using examples
Use colon as separator , Search for passwd In file root All the lines at the beginning , And output the first and seven columns of the row , And separated by commas :
[[email protected] home]# cat /etc/passwd | awk -F ":" '/^root/{print $1","$7}'
root,/bin/bash
$0 For the entire text line
$n Represents the... In the text line n Data fields
BEGIN and AND keyword
BEGIN Will enforce awk Execute the script command specified after the keyword before reading data
END Will be in awk After reading the data, execute the script command specified after the keyword
awk 'BEGIN {print "USERNAME"} awk 'BEGIN {
print "PASSWORD"}
-v Use of custom variables
Put all users id Add the specified number i:
-v i=1: Defining variables i, The initial value is 1, So the variables in the script command i There is no need to modify , Modify outside i The value of the can
# Query all users id
[[email protected] home]# cat /etc/passwd | awk -v i=1 -F ":" '{print $3}'
0
1
2
3
4
5
6
7
997
1001
996
# All users id+1
[[email protected] home]# cat /etc/passwd | awk -v i=1 -F ":" '{print $3+i}'
1
2
3
4
5
6
7
8
998
1002
997
# All users id+5
[[email protected] home]# cat /etc/passwd | awk -v i=5 -F ":" '{print $3+i}'
5
6
7
8
9
10
11
12
1002
1006
1001
边栏推荐
- Free PDF to word software sharing, these software must know!
- Go zero micro Service Practice Series (VII. How to optimize such a high demand)
- The soft youth under the blessing of devcloud makes education "smart" in the cloud
- Discussion on distributed unique ID generation scheme
- 【Proteus仿真】步进电机转速数码管显示
- Is it safe to open an account on the flush? Where to apply for opening an account
- Just like our previous views on the Internet, our understanding of the Internet began to become deeper
- STM32 basic knowledge points
- AI scene Storage Optimization: yunzhisheng supercomputing platform storage practice based on juicefs
- Does rapid software delivery really need to be at the cost of security?
猜你喜欢

字节云数据库未来方向的探索与实践

5-1 system vulnerability scanning

Cloud native enthusiast weekly: cool collection of grafana monitoring panels

Nacos-配置中心基本使用

Summer rainbow comes for dinner

Arrange the array into the smallest number_ Reverse pairs in an array (merge Statistics)_ Number of occurrences of a number in an ascending array_ Ugly number (Sword finger offer)

With the rise of China's database, Alibaba cloud lifeifei: China's cloud database has taken the lead in various mainstream technological innovations abroad

What if MySQL fails to store emoticons
![The server quickly sets up the alist integrated network disk website [pagoda panel one click deployment of alist]](/img/96/3e634c173c96082881286ba402a067.png)
The server quickly sets up the alist integrated network disk website [pagoda panel one click deployment of alist]

服务器快速搭建AList集成网盘网站【宝塔面板一键部署AList】
随机推荐
LeetCode85+105+114+124
Hematemesis finishing: a rare map of architects!
[从零开始学习FPGA编程-51]:高阶篇 - 基于IP核的FPGA开发- 什么是FPGA IP核(软核、固核、硬核)与学习方法
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
Unicom warehousing | all Unicom companies that need to sell their products need to enter the general warehouse first
利用kubernetes中的leader选举机制来完成自己的HA应用
Low code, end-to-end, one hour to build IOT sample scenarios, and the sound network released lingfalcon Internet of things cloud platform
The details of industry are all made by money and time
地方/园区如何做好产业分析?
Does Australia require that PVC plastic sheets comply with as/nzs 1530.3 with a flame spread index of 0?
STM32基础知识点
math_ Basic elementary function graph (power function / exponent / logarithm / trigonometry / inverse trigonometry)
Problem solving metauniverse, multi communication scheme in online games
Ansible automatic operation and maintenance
Golang code specification sorting
Mysql database: use the show profile command to analyze performance
模板函数与特化函数实现高效dynamicCast
VS2013如何让编写的程序在其它电脑上面也能运行
Discussion on distributed unique ID generation scheme
Summer rainbow comes for dinner