shell -- cut用法

cut命令可以从一个文本文件或者文本流中提取文本列。

cut语法
[root@www ~]# cut -d '分隔字符' -f fields <==用于有特定分隔字符
[root@www ~]# cut -c 字符区间               <==用于排列整齐的信息
选项与参数:
-d  :后面接分隔字符。与 -f 一起使用;
-f  :依据 -d 的分隔字符将一段信息分割成为数段,用 -f 取出第几段的意思;
-c  :以字符 (characters) 的单位取出固定字符区间;

PATH 变量如下
[root@www ~]# echo $PATH
/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/games
# 1 | 2       | 3   | 4       | 5            | 6            | 7

将 PATH 变量取出,我要找出第五个路径。
#echo $PATH | cut -d ':' -f 5
/usr/local/bin
 
将 PATH 变量取出,我要找出第三和第五个路径。
#echo $PATH | cut -d ':' -f 3,5
/sbin:/usr/local/bin

将 PATH 变量取出,我要找出第三到最后一个路径。
echo $PATH | cut -d ':' -f 3-
/sbin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/games
 
将 PATH 变量取出,我要找出第一到第三个路径。
#echo $PATH | cut -d ':' -f 1-3
/bin:/usr/bin:/sbin:

将 PATH 变量取出,我要找出第一到第三,还有第五个路径。
#echo $PATH | cut -d ':' -f 1-3,5
/bin:/usr/bin:/sbin:/usr/local/bin

实用例子:只显示/etc/passwd的用户和shell
#cat /etc/passwd | cut -d ':' -f 1,7
root:/bin/bash
daemon:/bin/sh
bin:/bin/sh

截取指定个数的字符
$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -c 2-5 ` // 截取第2到第5个字符
$ echo $a  
oot:

$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -c -2 `   // 截取前两个字符
$ echo $a  
ro

$ a=`echo root:x:0:0:root:/root:/bin/bash | cut -c 2- `    // 截取第2个以后的
$ echo $a  
oot:x:0:0:root:/root:/bin/bash

指定文件,最后一个参数是文件名
$ > cat cut_test.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

$ > cut -d : -f 1,6 ./cut_test.txt
root:/root
bin:/bin
daemon:/sbin
adm:/var/adm

或者用cut -d : -f 1,6 cut_test.txt 和 cat cut_test.txt | cut -d : -f 1,6 都可以

posted @ 2015-01-22 14:24  音弛  阅读(496)  评论(0编辑  收藏  举报