CUT可以对file(或stdin或pipe)的每行抽取出希望抽取的部分extract(cut out)
selected fields of each line of a file.
【用法】
cut
-bLIST [-n] [file]
cut
-cLIST [file]
cut
-fLIST [-dDELIM] [-s] [--output-delimiter=STRING] [file]
LIST
LIST是使用整数(按照升序)表示的需要抽取的位置:
使用,(逗号)或blank-character表示;
使用-表示。
例如:1,4,7,1-3,8;或者省略表示:-5,10 (等价于1-5,10),3- (等价于3-末尾)。
file
要抽取文件的路径名;如果没有指定或者使用-,则从stdin接受数据。
1.
按照字节byte抽取cut -bLIST [-n] [file]
a. 例如:cut
-b1-5 表示抽取每行的第1到第5个字节;
完整的例子:echo
"this is min" | cut -b1-6 返回this
i
b. cut -bLIST
-n: -n和-b结合使用防止multi-byte多字节字符被切割;
2.
按照字符抽取cut -cLIST [file]
例如:echo
"this is min" | cut -c1-6,返回this
i
3.
按照分割以后的域field抽取
cut -fLIST
[-dDELIM] [-s] [--output-delimiter=STRING] [file]
过程为:先对每行的字符串按照-dDELIM指定的分隔符DELIM(默认使用TAB)进行分割;然后按照LIST的值返回指定域的内容。
例如:
echo "this is
min" | cut -f1,3 返回this
min,按照默认的TAB进行分割,返回结果还是按照TAB。
echo
"this_is_min" | cut -f1,3 -d"_" 返回this_min,按照指定的_进行分割,返回结果使用_分割。
- 对于没有DELIM的行会完全返回,如果不希望返回可以使用-s;
- 默认返回的各个域仍按照-d指定的DELIM分割显示,如果希望结果中使用指定的分隔符,可以使用--output-delimiter=STRING指定。
例如:echo
"this_is_min" | cut -f1,3 -d"_" --output-delimiter="-" 返回结果为this-min
应用举例:
1
有一页电话号码薄文件contacts.txt,上面按顺序规则地写着”人名、家庭住址、电话、备注”等,假如使用;隔开。
如果希望抽取所有人的名字和其对应的电话号码,使用命令:
cat contacts.txt
| cut -f1,3 -d";"
2
要查看Linux下当前所有的用户:
cat /etc/passwd
| cut -f1,3 -d":"
第二列大于500的为创建的用户,小于500的为系统定义的用户。
例如:
###################################################################
## ckinstance.ksh
##
###################################################################
echo "`date`
"
echo "Oracle
Database(s) Status `hostname` "
namelst=`ps
-ef|grep ora_pmon|grep -v grep|awk '{print $8}'|cut -c10-15`
for name in
$namelst
do
if [ -z $name
];then
echo "Oracle
Instance - $name: Down"
else
echo "Oracle
Instance - $name: Up"
fi
done