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

边栏推荐
- 理想路径问题
- 【力扣刷题】二分查找:4. 寻找两个正序数组的中位数
- 6 custom layer
- JS教程之使用 ElectronJS、VueJS、SQLite 和 Sequelize ORM 从 A 到 Z 创建多对多 CRUD 应用程序
- Mono 的一些实例方法
- "C language" question set of ⑩
- Quickly get started with federal learning -- the practice of Tencent's self-developed federal learning platform powerfl
- Big talk Domain Driven Design -- presentation layer and others
- Keepalived 实现 Redis AutoFailover (RedisHA)
- IAR工程适配GD32芯片
猜你喜欢

What is the process of switching C # read / write files from user mode to kernel mode?

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

Exquisite makeup has become the "soft power" of camping, and the sales of vipshop outdoor beauty and skin care products have surged

(1) Keras handwritten numeral recognition and recognition of self written numbers

IAR工程适配GD32芯片

C# 读写文件从用户态切到内核态,到底是个什么流程?

Angel 3.2.0 new version released! Figure the computing power is strengthened again

2 三种建模方式

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

Handwritten numeral recognition, run your own picture with the saved model
随机推荐
Detailed explanation of cookies and sessions
Swiftui retrieves the missing list view animation
IAR工程适配GD32芯片
Keepalived 实现 Redis AutoFailover (RedisHA)1
R语言plotly可视化:小提琴图、多分类变量小提琴图、分组(grouped)小提琴图、分裂的分组小提琴图、每个小提琴图内部分为两组数据、每个分组占小提琴图的一半、自定义小提琴图的调色板、抖动数据点
JS教程之使用 ElectronJS 桌面应用程序打印贴纸/标签
How to separate jar packages and resource files according to packaging?
【207】Apache崩溃的几个很可能的原因,apache崩溃几个
TCP拥塞控制详解 | 1. 概述
若依打包如何分离jar包和资源文件?
牛客编程题--必刷101之动态规划(一文彻底了解动态规划)
01 backpack DP
C. Inversion Graph
Cuckoo filter for Chang'an chain transaction
当一个程序员一天被打扰 10 次,后果很惊人!
我把它当副业月入3万多,新手月入过万的干货分享!
若依微服务特殊字符串被过滤的解决办法
Redis 迁移(操作流程建议)1
大话领域驱动设计——表示层及其他
Redis Guide (8): principle and implementation of Qianfan Jingfa distributed lock