笔记簿
ᴄᴏᴅɪɴɢ ɪs ᴀʀᴛ
首页
关于
搜索
登录
注册
Shell 工具cut - shell中基础工具cut
#### cut cut的工作就是“剪”,具体的说就是在文件中负责剪切数据用的。cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段输出。 ##### 1.基本语法 ```text cut [选项参数] filename 说明:默认分隔符是制表符 ``` ##### 2.选项参数 | 选项参数 | 功能 | | ------------ | ------------ | | -f | 列号,提取第几列 | | -d | 分隔符,按照指定分隔符分割列 | |-c|字符范围,不依赖分隔符来区分列,而是通过字符范围(行首为0)来进行字符安提取。“n-”表示从第n个字符到行尾;“n-m”从第n个字符到第m个字符;“-m”表示从第1个字符到第m个字符。 ##### 3.案例 ```text 数据准备 lynn@promptness Downloads % touch cut.txt lynn@promptness Downloads % vim cut.txt lynn@promptness Downloads % cat cut.txt aa bb cc dd ee ff gg hh ``` ```text 切割cut.txt第一列 lynn@promptness Downloads % cut -d " " -f 1 cut.txt aa cc ee gg ``` ```text 切割cut.txt第二、三列 lynn@promptness Downloads % cut -d " " -f 2,3 cut.txt bb dd ff hh ``` ```text 在cut.txt文件中切割出dd lynn@promptness Downloads % cat cut.txt | grep "dd" | cut -d " " -f 1 cc ``` ```text 选取系统PATH变量值,第2个“:”开始后的所有路径 lynn@promptness Downloads % echo $PATH /Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin lynn@promptness Downloads % echo $PATH | cut -d : -f 2- /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin ``` ```text 切割ifconfig 后打印的IP地址 lynn@promptness Downloads % ifconfig en0|grep "inet"|cut -d " " -f 2 192.168.43.60 ```