【版权声明:转载请保留源:blog.csdn.net/gentleliu。Mail:shallnew at 163 dot com】

cut指挥类似至awk,从行内提取的信息,它是一个较弱的版本的功能awk。

cut命令格式为:cut [options] filename

当中options有:

-d  指定与空格和t a b键不同的域分隔符。类似于awk的“-F”。

-f field  指定剪切域数

-c list  指定剪切字符数。



首先我们来处理按:切割的password文件:

# cat passwd
root:x:0:0:root:/root:/bin/sh
proxy:x:13:13:proxy:/bin:/bin/sh
operator:x:37:37:Operator:/var:/bin/sh
ftp:x:83:83:ftp:/home/ftp:/bin/sh
nobody:x:99:99:nobody:/home:/bin/sh
sshd:x:103:99:Operator:/var:/bin/sh

按:切割,然后取第一列和第六列,-f能够指定获取的域数:

# cut -d: -f1,6 passwd
root:/root
proxy:/bin
operator:/var
ftp:/home/ftp
nobody:/home
sshd:/var

指定域数还能够这样写:

# cut -d: -f 1-4,6 passwd
root:x:0:0:/root
proxy:x:13:13:/bin
operator:x:37:37:/var
ftp:x:83:83:/home/ftp
nobody:x:99:99:/home
sshd:x:103:99:/var

awk的实现为:

# awk -F: 'BEGIN{OFS=":"}{print $1,$2,$3,$4,$6}' passwd
root:x:0:0:/root
proxy:x:13:13:/bin
operator:x:37:37:/var
ftp:x:83:83:/home/ftp
nobody:x:99:99:/home
sshd:x:103:99:/var

可是假设要指定多个字符来切割的话,cut就不行了。cut仅仅支持单个分隔字符,二awk支持多个。

比方我们以“bin”分分隔:

# awk -F"bin" 'BEGIN{OFS=":"}{print $1}' passwd
root:x:0:0:root:/root:/
proxy:x:13:13:proxy:/
operator:x:37:37:Operator:/var:/
ftp:x:83:83:ftp:/home/ftp:/
nobody:x:99:99:nobody:/home:/
sshd:x:103:99:Operator:/var:/

使用cut:

# cut -d"bin" -f1 passwd
cut: the delimiter must be a single character
Try 'cut --help' for more information.

这里能够看见cut命令的功能非常有限。

cut默认以tab键来切割,awk默认以空格或多个空格或tab键来切割:

# sed 's/:/\t/g' passwd | cut -f5
root
proxy
Operator
ftp
nobody
Operator
# sed 's/:/\t/g' passwd | awk '{print $5}'
root
proxy
Operator
ftp
nobody
Operator

cut能够剪切第随意字符。看似功能强大,但非常少有使用价值。

使用- c选项指定精确剪切数目。这样的方法需确切知道開始及结束字符。

通常不用这样的方法,除非在固定长度的域或文件名称上。

来看几个样例即可了:

# cut -c1,2,4 passwd
rot
prx
opr
ft:
noo
ssd
# cut -c5-9 passwd
:x:0:
y:x:1
ator:
x:83:
dy:x:
:x:10

版权声明:本文博主原创文章。博客,未经同意不得转载。假设你认为你的实际物品,请点击以下“最佳”。

shell文字过滤程序(十):cut命令的更多相关文章

  1. shell文字过滤程序(十一):paste命令

    [版权声明:转载请保留源:blog.csdn.net/gentleliu.Mail:shallnew at 163 dot com] 由于可以从字面上可以看出.paste指挥和cut相反的命令.cut ...

  2. Linux Shell脚本入门--cut命令

    Linux Shell脚本入门--cut命令 cut cut 命令可以从一个文本文件或者文本流中提取文本列. cut语法 [root@www ~]# cut -d'分隔字符' -f fields &l ...

  3. Linux Shell脚本编程--cut命令

    cut cut命令可以从一个文本文件或者文本流中提取文本列. cut语法 [root@www ~]# cut -d'分隔字符' -f fields <==用于有特定分隔字符 [root@www ...

  4. shell脚本--cut命令

    bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 1.1 选项说明 cut命令将行按指定的分隔符分割成多列,它的 ...

  5. linux C程序中获取shell脚本输出(如获取system命令输出)

    转载自 http://blog.csdn.net/hjxhjh/article/details/7909518 1. 前言 Unix 界有一句名言:“一行shell脚本胜过万行C程序”,虽然这句话有些 ...

  6. shell特殊符号cut命令 sort_wc_uniq命令 tee_tr_split命令 shell特殊符号

    shell特殊符号cut命令 特殊符号 *  通配符,任意个任意字符 ? 任意一个字符 # 注释字符 \  脱义字符 c=\$a\$b echo  $c | 管道符 cat  1.txt |less ...

  7. Linux centos7 shell特殊符号、cut命令、sort_wc_uniq命令、tee_tr_split命令、shell特殊符号

    一.shell特殊符号.cut命令 *任意字符 [root@davery ~]# ls /tmp/*.txt/tmp/1.txt /tmp/2.txt /tmp/q.txt[root@davery ~ ...

  8. [shell基础]——cut命令

    cut命令常见选项

  9. 《cut命令》-linux命令五分钟系列之十九

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...

随机推荐

  1. hprose rpc使用实例(同时有Java和Delphi客户端的例子)

    php server <?php require_once('src/Hprose.php'); function hello($name) { echo "Hello $name!& ...

  2. 为Delphi程序增加UAC功能(每个步骤都很详细)

    相关资料:http://bbs.csdn.net/topics/320071356# 操作方法: 在Source\VCL目录下应该有这样两个文件sample.manifest和WindowsXP.rc ...

  3. C语言char s[] 和 char *s的差别

    C语言char s[] 和 char *s的差别,以下这个回答解说的非常清晰. The difference here is that char *s = "Hello world" ...

  4. .Net程序猿乐Android开发---(4)注册页面布局

    接下来我们介绍的登陆页面布局,在本节中,我们看一下注册页面布局,页面布局大同小异,来一起熟悉下基本控件的使用方法. 效果图: 1.加入注冊页面 右键选中layout目录,加入注冊页面.例如以下图 点击 ...

  5. hdu1028(整数划分问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 整数划分问题 整数划分 --- 一个老生长谈的问题: 描述 整数划分是一个经典的问题.请写一个程 ...

  6. _00021 尼娜抹微笑伊拉克_谁的的最离奇的异常第二阶段 Jedis pool.returnResource(jedis)

    笔者博文:妳那伊抹微笑 博客地址:http://blog.csdn.net/u012185296 博文标题:_00021 妳那伊抹微笑_谁的异常最诡异第二期之 Jedis pool.returnRes ...

  7. linux+nginx+mysql+php

    LNMP(linux+nginx+mysql+php)服务器环境配置   一.简介 Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为 “engine X”, 是一个高性能的 ...

  8. Ucan23操作系统项目地址

    期间耽误了近半年的时间.在昨天最终完毕了Ucan23OS, 项目托管在GitHub上,地址为: https://github.com/howardking/UCAN23OS 以下为操作系统的执行截图 ...

  9. Effective C++:条款38:通过一个复杂的模具has-a要么“基于一些实现”

    (一) public继承是"is-a"关联,"has-a"或"依据某物实现出(is-implemented-in-terms-of)"的意思 ...

  10. Gnu Linux--Ubuntu系统清理项整理

    /*********************************************************************  * Author  : Samson  * Date   ...