当前位置:网站首页>Knowing these commands allows you to master shell's own tools
Knowing these commands allows you to master shell's own tools
2022-06-26 16:24:00 【Think hard of Xiao Zhao】

The previous article was case driven , Sum up Sell The basic grammar in , This article takes you through case driven learning Sell The built-in tool commands in .
Cut
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 .
Basic usage
cut [ Option parameters ] filename
# The default separator is tab
Parameter description
- -f : Column number , Extract which column
- -d : Separator , Splits columns according to the specified separator
Case study :
First go to linux In the system , Prepare the data

Case study 1: cutting cat.txt First column
cut -f 1 -d " " cat.txt

Case study 2: cutting cat.txt second 、 The three column
cut -f 2,3 -d " " cat.txt

Case study 3: stay cat.txt Cut out in file lisi
cat cat.txt | grep "lisi" | cut -f 1 -d " "

Sed
sed Is a flow editor , It processes one line at a time . When dealing with , Store the currently processed rows in a temporary buffer , be called “ Mode space ”, Then use sed Command processing buffer contents , After processing , Send the contents of the buffer to the screen . Next line , This is repeated , Until the end of the file . The contents of the document have not changed , Unless you use redirected storage output .
Basic usage
sed [ Option parameters ] ‘command’ filename
Parameter description
- -e : Directly in command line mode sed Action editor
Command function
- a : newly added ,a You can use a string after , On the next line
- d : Delete
- s : Find and replace
Case study :
Prepare the data

Case study 1: take “hello wawa” This word is inserted into sed.txt Second elements , Print
sed '2a hello wawa' sed.txt

ps: The file hasn't changed
Case study 2: Delete sed.txt All files contain wo The line of
sed '/wo/d' sed.txt

Case study 3: take sed.txt In file wo Replace with ni
sed 's/wo/ni/g' sed.txt

Awk
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 .
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
Parameter description
- -F : Specify the input file separator
- -v : Assign a user-defined variable
Case study :
Prepare the data
sudo cp /etc/passwd ./
Search for passwd Document to root All lines at the beginning of the keyword , And output the 7 Column .
awk -F: '/^root/{print $7}' passwd

awk Built in variables for
- FILENAME file name
- NR Number of records read
- NF The number of fields in the browsing record ( After cutting , Number of columns )
Case study :
Statistics passwd file name , Line number of each line , Columns per row
awk -F: '{print "filename:" FILENAME ", linenumber:" NR ",columns:" NF}' passwd

Sort
sort The order is in Linux It's very useful , It sorts the files , And output the sorting result standard .
Basic grammar
sort( Options )( Parameters )
Parameter description
- -n Sort by the size of the value
- -r In reverse order
- -t Set the separator used for sorting
- -k Specify the columns to sort
Case study :
Prepare the data

according to “:” The third column after splitting is sorted in reverse order
sort -t : -nrk 3 sort.sh

This sharing ends here , I hope that's helpful .

边栏推荐
- 长安链交易防重之布谷鸟过滤器
- Oilfield exploration problems
- Supplement the short board - Open Source im project openim about initialization / login / friend interface document introduction
- [Li Kou brush question] monotone stack: 84 The largest rectangle in the histogram
- 神经网络“炼丹炉”内部构造长啥样?牛津大学博士小姐姐用论文解读
- Mono 的一些实例方法
- 100+数据科学面试问题和答案总结 - 基础知识和数据分析
- 10 tf. data
- Acid of redis
- [from deleting the database to running] the end of MySQL Foundation (the first step is to run.)
猜你喜欢

IAR engineering adapts gd32 chip
![[from database deletion to running] JDBC conclusion (finish the series in one day!! run as soon as you finish learning!)](/img/75/2fb1a4e6215e404df34849e9e4f21a.png)
[from database deletion to running] JDBC conclusion (finish the series in one day!! run as soon as you finish learning!)

牛客小白月赛50

Cuckoo filter for Chang'an chain transaction

振动式液量检测装置

无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!...
![[Li Kou brush question] monotone stack: 84 The largest rectangle in the histogram](/img/75/440e515c82b5613b117728ba760786.png)
[Li Kou brush question] monotone stack: 84 The largest rectangle in the histogram

Beijing University and Tencent jointly build angel4.0, and the self-developed in-depth learning framework "River map" is integrated into the ecology
![[graduation season] a word for graduates: the sky is high enough for birds to fly, and the sea is wide enough for fish to leap](/img/b6/21e51fa7f79d4a4b950f061703f0fb.png)
[graduation season] a word for graduates: the sky is high enough for birds to fly, and the sea is wide enough for fish to leap

架构实战营毕业设计
随机推荐
Learn about common functional interfaces
Ideal path problem
What is the difference between stm32f1 and gd32f1?
Redis 迁移(操作流程建议)
The details of the first pig heart transplantation were fully disclosed: human herpes virus was found in the patient, the weight of the heart doubled after death, and myocardial cell fibrosis
固件供应链公司Binarly获得WestWave Capital和Acrobator Ventures的360万美元投资
基於Kubebuilder開發Operator(入門使用)
Oilfield exploration problems
H5 close the current page, including wechat browser (with source code)
(DFS search) acwing 2005 horseshoe
day10每日3题(3):数组中的字符串匹配
mha 切换(操作流程建议)
Kept to implement redis autofailover (redisha)
The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance
基于STM32+华为云IOT设计的云平台监控系统
Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system
Redis顺序排序命令
了解下常见的函数式接口
[from deleting the database to running] the end of MySQL Foundation (the first step is to run.)
《软件工程》期末重点复习笔记