当前位置:网站首页>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 .

边栏推荐
- Tsinghua's "magic potion" is published in nature: reversing stem cell differentiation, and the achievements of the Nobel Prize go further. Netizen: life can be created without sperm and eggs
- day10每日3题(1):逐步求和得到正数的最小值
- C language reading data
- JS教程之使用 ElectronJS 桌面应用程序打印贴纸/标签
- day10每日3题(2):统计最大组的数目
- Angel 3.2.0 new version released! Figure the computing power is strengthened again
- day10每日3题(3):数组中的字符串匹配
- 6 custom layer
- [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
- 无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!...
猜你喜欢

Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's Mae

Supplement the short board - Open Source im project openim about initialization / login / friend interface document introduction

Structure the graduation project of actual combat camp

基于STM32+华为云IOT设计的云平台监控系统

The first batch in the industry! Tencent cloud security and privacy computing products based on angel powerfl passed CFCA evaluation

对话长安马自达高层,全新产品将在Q4发布,空间与智能领跑日系

Dialogue with the senior management of Chang'an Mazda, new products will be released in Q4, and space and intelligence will lead the Japanese system

心情不好,我就这样写代码
![[Li Kou brush questions] 11 Container holding the most water //42 Rain water connection](/img/45/1e712300ea655856762394fba09066.png)
[Li Kou brush questions] 11 Container holding the most water //42 Rain water connection

Oilfield exploration problems
随机推荐
了解下常见的函数式接口
IAR engineering adapts gd32 chip
JS教程之使用 ElectronJS 桌面应用程序打印贴纸/标签
Développer un opérateur basé sur kubebuilder (démarrer)
架构实战营毕业设计
大话领域驱动设计——表示层及其他
油田勘探问题
网页课程设计大作业——华山旅游网
Arduino uno + DS1302 simple time acquisition and serial port printing
Cookie和Session详解
100+数据科学面试问题和答案总结 - 基础知识和数据分析
01 backpack DP
C# 读写文件从用户态切到内核态,到底是个什么流程?
Solution for filtering by special string of microservice
Tsinghua's "magic potion" is published in nature: reversing stem cell differentiation, and the achievements of the Nobel Prize go further. Netizen: life can be created without sperm and eggs
6 custom layer
11 introduction to CNN
Ten thousand words! In depth analysis of the development trend of multi-party data collaborative application and privacy computing under the data security law
牛客小白月赛50
基于Kubebuilder开发Operator(入门使用)