当前位置:网站首页>Use of shell cut command

Use of shell cut command

2022-04-23 17:06:00 Magic Flute love

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 .cut After that, the original value was not changed

Basic usage

cut [ Option parameters ]  filename

explain : The default separator is tab (\t)

Option parameter description

Option parameters function
-f Column number , Extract which column
-d Separator , Splits columns according to the specified separator

Case practice

## Data preparation 
[bd@localServer ~]$ touch cut.txt
[bd@localServer ~]$ vim cut.txt

fu xia
zhou men
wo  wo
lai  lai
le  le

###  cutting cut.txt First column 
[bd@localServer ~]$ cut -d " " -f 1 cut.txt 
fu
zhou
wo
lai
le

###  cutting cut.txt second 、 The three column 
[bd@localServer ~]$ cut -d " " -f 2,3 cut.txt 
xia
men
 wo
 lai
 le

###  stay cut.txt Cut out in file zhou
[bd@localServer ~]$ cat cut.txt | grep "zhou" | cut -d " " -f 1
zhou

###  Selection system PATH A variable's value , The first 2 individual “:” All paths after start :

[bd@localServer ~]$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/bd/bin

 
####  Among them 2- It means take 2 And everything after that 
[bd@localServer ~]$ echo $PATH | cut -d : -f 2-
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/bd/bin

###  cutting ifconfig  Post print IP Address 
[bd@localServer ~]$ ifconfig eth0 | grep "inet addr" | cut -d : -f 2 | cut -d " " -f 1
192.168.1.102

版权声明
本文为[Magic Flute love]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231704450963.html