Shell

推荐列表 站点导航

当前位置:首页 > 脚本编程 > Shell >

shell cut 命令用法

来源:网络整理  作者:wy  发布时间:2020-12-24 11:33
本文介绍了shell编程中cut命令的用法,有需要的朋友参考下,希望对大家有所帮助。...

在shell编程中,cut命令与awk有着差不多的功能,下面为大家详细介绍cut命令的用法。

例1
 

复制代码 代码示例:

$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -d : -f 1,5` 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
root:root 
 

把 root:x:0:0:root:/root:/bin/bash 重定向到cut命令里,-d表示分隔符,这里使用冒号: 作为分隔符,-f 表示字段,选择了第1,和第5个字段,

例 2,只打印第一个字段field
 

复制代码 代码示例:

$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -d :  -f 1`
[root@jbxue ~] /cygdrive/d 
$ echo $a 
root 

例 3,打印第一个字段以后的所有字段,包含第一个字段
 

复制代码 代码示例:

[root@jbxue ~] /cygdrive/d 
$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -d : -f 1-` 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
root:x:0:0:root:/root:/bin/bash 
 
[root@jbxue ~] /cygdrive/d 
$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -d : -f 3-`   // 打印第3个字段后的所有字段,包含第三个字段 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
0:0:root:/root:/bin/bash 

例4 ,截取第2到第4个字段
 

复制代码 代码示例:

[root@jbxue ~] /cygdrive/d 
$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -d : -f 2-4` 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
x:0:0 

例 5 截取指定个数的字符
 

复制代码 代码示例:

[root@jbxue ~] /cygdrive/d 
$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -c 2-5 ` // 截取第2到第5个字符 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
oot: 
 
[root@jbxue ~] /cygdrive/d 
$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -c 2-7 `  // 截取第2到第7个字符 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
oot:x: 
 
[root@jbxue ~] /cygdrive/d 
$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -c -2 `   // 截取前两个字符 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
ro 
 
[root@jbxue ~] /cygdrive/d 
$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -c 2- `    // 截取第2个以后的 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
oot:x:0:0:root:/root:/bin/bash 

例 6 指定文件,最后一个参数是文件名
 

复制代码 代码示例:

$ cat pass.txt 
root:x:0:0:root:/root:/bin/bash 
bin:x:1:1:bin:/bin:/sbin/nologin 
daemon:x:2:2:daemon:/sbin:/sbin/nologin 
adm:x:3:4:adm:/var/adm:/sbin/nologin 
[root@jbxue ~] /cygdrive/d 
$ cut -d : -f 1-3 ./pass.txt 
root:x:0 
bin:x:1 
daemon:x:2 
adm:x:3

相关热词: shell 命令

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://www.juheyunku.com/jiaob/shell/8510.shtml

最新文章
PHP生成奖状 PHP生成奖状

时间:2021-01-06

python实现输入五个数并求 python实现输入五个数并求

时间:2021-01-06

php检测网站是否正常打开 php检测网站是否正常打开

时间:2021-01-05

python怎么右对齐 python怎么右对齐

时间:2021-01-05

Python3爬虫入门之Python3的安 Python3爬虫入门之Python3的安

时间:2021-01-05

如何用PHP接收http请求头信 如何用PHP接收http请求头信

时间:2021-01-05

数据库怎么连接用php写的 数据库怎么连接用php写的

时间:2021-01-05

php后缀怎么打开 php后缀怎么打开

时间:2021-01-05

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

shell cut 命令用法

2020-12-24 编辑:wy

在shell编程中,cut命令与awk有着差不多的功能,下面为大家详细介绍cut命令的用法。

例1
 

复制代码 代码示例:

$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -d : -f 1,5` 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
root:root 
 

把 root:x:0:0:root:/root:/bin/bash 重定向到cut命令里,-d表示分隔符,这里使用冒号: 作为分隔符,-f 表示字段,选择了第1,和第5个字段,

例 2,只打印第一个字段field
 

复制代码 代码示例:

$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -d :  -f 1`
[root@jbxue ~] /cygdrive/d 
$ echo $a 
root 

例 3,打印第一个字段以后的所有字段,包含第一个字段
 

复制代码 代码示例:

[root@jbxue ~] /cygdrive/d 
$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -d : -f 1-` 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
root:x:0:0:root:/root:/bin/bash 
 
[root@jbxue ~] /cygdrive/d 
$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -d : -f 3-`   // 打印第3个字段后的所有字段,包含第三个字段 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
0:0:root:/root:/bin/bash 

例4 ,截取第2到第4个字段
 

复制代码 代码示例:

[root@jbxue ~] /cygdrive/d 
$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -d : -f 2-4` 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
x:0:0 

例 5 截取指定个数的字符
 

复制代码 代码示例:

[root@jbxue ~] /cygdrive/d 
$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -c 2-5 ` // 截取第2到第5个字符 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
oot: 
 
[root@jbxue ~] /cygdrive/d 
$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -c 2-7 `  // 截取第2到第7个字符 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
oot:x: 
 
[root@jbxue ~] /cygdrive/d 
$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -c -2 `   // 截取前两个字符 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
ro 
 
[root@jbxue ~] /cygdrive/d 
$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -c 2- `    // 截取第2个以后的 
 
[root@jbxue ~] /cygdrive/d 
$ echo $a 
oot:x:0:0:root:/root:/bin/bash 

例 6 指定文件,最后一个参数是文件名
 

复制代码 代码示例:

$ cat pass.txt 
root:x:0:0:root:/root:/bin/bash 
bin:x:1:1:bin:/bin:/sbin/nologin 
daemon:x:2:2:daemon:/sbin:/sbin/nologin 
adm:x:3:4:adm:/var/adm:/sbin/nologin 
[root@jbxue ~] /cygdrive/d 
$ cut -d : -f 1-3 ./pass.txt 
root:x:0 
bin:x:1 
daemon:x:2 
adm:x:3

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://www.juheyunku.com/jiaob/shell/8510.shtml

相关文章

风云图片

推荐阅读

返回Shell频道首页