Linux重修笔记------cut

来源:互联网 发布:开淘宝店铺挣钱吗 编辑:程序博客网 时间:2024/03/28 17:26

环境debian7 weekly

算是那个实验1的一个狗尾续貂

之前看到个叫做linux大棚的博客,好东西~~

 

Cut命令

-b (byte)字节

-c (characters)字符

-d 自定义分割符号,默认为转义字符

-f (fields)

-n 取消分割多字节字符,与b一起用的

 

root@debian:~# ps  PID TTY          TIME CMD 4062 pts/0    00:00:00 bash 4110 pts/0    00:00:00 ps $ ps|cut -b 3root@debian:~# ps|cut -b 3P011

这里第三个字符是P!!

P.S.对于字符数是怎么数的,可不是输出时候输出第一个你看得到的为准,r这个列的位置才是第一个字符



$ ps |cut -b 3-5,7root@debian:~# ps|cut -b 3-5,7PIDT062p113p114p $ ps |cut -b 7,3-5root@debian:~# ps|cut -b 7,3-5PIDT062p117p118p $ ps |cut -b 7,3-5 //出错了 $ ps |cut -b 3-PID TTY          TIME CMD062 pts/0    00:00:00 bash123 pts/0    00:00:00 ps124 pts/0    00:00:00 cut $ ps |cut -b -5  PID 4062 4125 4126 $ ps |cut -b -5,5-   PID TTY          TIME CMD 4062 pts/0    00:00:00 bash 4127 pts/0    00:00:00 ps 4128 pts/0    00:00:00 cut


然后对中文

root@debian:/home/xyz# cat file_ch.txtGoogle从2005年其就开始举办这样的全球性活动.简单地用一句话概括一下,就是Google出钱给学生为开源项目写代码,而这个项目是在学生暑假期间举    行,被选择上并成功完成的学生最终能够获得5000美刀的奖金。当       然,Google的这个项目最想得到的是:提供给学生机会参与到真实的软件开发中,在项目结束后能够有所收获,并且还能继续投入到开源中,为开源社区做贡献。 root@debian:/home/xyz# cat file_ch.txt|cut -b 1G�� root@debian:/home/xyz# cat file_ch.txt|cut -c 1G��root@debian:/home/xyz# cat file_ch.txt|cut -c 2o��root@debian:/home/xyz# cat file_ch.txt|cut -c 1,2Go��root@debian:/home/xyz# cat file_ch.txt|cut -c 1,2,3Goo简当root@debian:/home/xyz# cat file_ch.txt|cut -c 3o��

对比发现c能够读取中文,那是因为c是读取字符,b是字节,汉字包含两个字节

 

 

n的用法

$ cat file_ch.txt | cut -nb 1,2,3  

debian7环境中与-c 几乎没差的样子,在centos有待检验

 

补充:在GB2312编码中一个汉字占2个字节,而在UTF-8中,一个汉字要占3个字节”

Debian7默认是utf-8,有些是gb的,怎么更改呢?百度了一下再chinaunix找到,可行

将~/.bashrc 如果有以下的export则改为export LANG=zh_CN.UTF-8export LC_CTYPE="zh_CN.UTF-8" 这样启动终端的时候就是选UTF-8的编码了,然后设置/etc/environment[liang@localhost ~]$ cat /etc/environmentLANGUAGE="zh_CN:zh:en_US:en"LC_ALL=zh_CN.UTF-8 LANG=zh_CN[liang@localhost ~]$

-b -c一般只用来读取固定格式的文件,对于不固定格式的就用-d -f ,那如果f的域数字调转会怎样呢?

root@debian:/home/xyz# cat /etc/passwd |head -n5root:x:0:0:root:/root:/bin/bashdaemon:x:1:1:daemon:/usr/sbin:/bin/shbin:x:2:2:bin:/bin:/bin/shsys:x:3:3:sys:/dev:/bin/shsync:x:4:65534:sync:/bin:/bin/sync
 

root@debian:~# cat /etc/passwd |head -n5| cut -d: -f1,4-5root:0:rootdaemon:1:daemonbin:2:binsys:3:syssync:65534:sync

root@debian:~# cat /etc/passwd |head -n5| cut -d: -f4-5,1root:0:rootdaemon:1:daemonbin:2:binsys:3:syssync:65534:sync

 -f 后面的参数顺序不影响其输出排序


root@debian:~# cat /etc/passwd |head -n5| cut -d: -f 4-
0:root:/root:/bin/bash1:daemon:/usr/sbin:/bin/sh2:bin:/bin:/bin/sh3:sys:/dev:/bin/sh65534:sync:/bin:/bin/sync
 

root@debian:~# cat /etc/passwd |head -n5| cut -d: -f -4root:x:0:0daemon:x:1:1bin:x:2:2sys:x:3:3sync:x:4:65534
这是包括第四个域的哦

如果用空格来进行分割,空格加上单引号

$ ls -l | cut -d’ ‘ -f 1

对于转义字符分割:

可以先用一个命令查看一下究竟用哪个

$ sed -n l file_ch.txt

第二个参数是小写L。中文会变成utf码,然后回车是$,TAB\t


还有一点,cut的d不支持正则~~~~所以其最大缺点就是不能处理多个空格的分隔

原创粉丝点击