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

边栏推荐
- [from database deletion to running] JDBC conclusion (finish the series in one day!! run as soon as you finish learning!)
- Detailed explanation of cookies and sessions
- JS教程之使用 ElectronJS、VueJS、SQLite 和 Sequelize ORM 从 A 到 Z 创建多对多 CRUD 应用程序
- [Li Kou brush questions] 11 Container holding the most water //42 Rain water connection
- JS教程之Electron.js设计强大的多平台桌面应用程序的好工具
- Common properties of XOR and addition
- 若依打包如何分离jar包和资源文件?
- IAR engineering adapts gd32 chip
- Net基于girdview控件实现删除与编辑行数据
- 基于Kubebuilder开发Operator(入门使用)
猜你喜欢

The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance

11 introduction to CNN

SAP OData development tutorial - from getting started to improving (including segw, rap and CDP)

When a programmer is disturbed 10 times a day, the consequences are amazing!

Beijing University and Tencent jointly build angel4.0, and the self-developed in-depth learning framework "River map" is integrated into the ecology

首例猪心移植细节全面披露:患者体内发现人类疱疹病毒,死后心脏重量翻倍,心肌细胞纤维化丨团队最新论文...

神经网络“炼丹炉”内部构造长啥样?牛津大学博士小姐姐用论文解读

了解下常见的函数式接口

Arduino UNO + DS1302简单获取时间并串口打印

Structure the graduation project of actual combat camp
随机推荐
Net基于girdview控件实现删除与编辑行数据
LeetCode Algorithm 24. 两两交换链表中的节点
Arduino uno + DS1302 simple time acquisition and serial port printing
Beijing University and Tencent jointly build angel4.0, and the self-developed in-depth learning framework "River map" is integrated into the ecology
最小二乘系统辨识课 中篇:递归最小二乘
【力扣刷题】11.盛最多水的容器//42.接雨水
【毕业季】致毕业生的一句话:天高任鸟飞,海阔凭鱼跃
今年高考英语AI得分134,复旦武大校友这项研究有点意思
100+ data science interview questions and answers Summary - basic knowledge and data analysis
[chat in 5] eight years after graduation, I have been pursuing my dream
R语言广义线性模型函数GLM、glm函数构建逻辑回归模型(Logistic regression)、分析模型是否过离散(Overdispersion)、使用残差偏差与二项式模型中的剩余自由度的比率评估
Failed to upload hyperf framework using alicloud OSS
Codeforces Round #802 (Div. 2)
Handwritten numeral recognition, run your own picture with the saved model
Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system
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
股票开户优惠链接,我如何才能得到?在线开户安全么?
[time complexity and space complexity]
Pybullet robot simulation environment construction 5 Robot pose visualization
Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)