欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

第三章 文件过滤及内容编辑处理命令

程序员文章站 2023-11-17 16:53:16
3.1 cat cat命令作用: 1、查看文件内容 2、把多个文件合并成一个文件 3、编辑文件内容 4、结合>>和<

3.1 cat

cat命令作用:

1、查看文件内容

2、把多个文件合并成一个文件

3、编辑文件内容

4、结合>>和<<eof进行编辑

5、清空文件内容

-n参数 从1开始对所有输出的内容按行编号

-b参数 忽略显示空白行行号

-e参数 在每一行行尾显示$符号(即使是空行 结尾也是有结束标识的)

-s参数 当遇到有连续两行以上的空白行时,就替代为一行空白行

[root@oldboy ~]# cat test1.txt

test1

[root@oldboy ~]# cat test{,1}.txt   同时把test.txt和test1.txt同时读出 相当于两个文件合并成一个文件

my b is http:hahah

my c is http:hahah

my d is http:hahah

 

ay c is http:hahah

by c is http:hahah

cy c is http:hahah

 

oldboy

oldboy

oldboy.

000btti000000000000anji000ngingni.

shanghaibeijingtiaks00000dkasskjdhshdjslfjslajfhsjhdflkahdlfjaldf

shanghaibeijingtiaks00000dkass0000000kjdhshdjslfjslajfhsjhdflkahdlfjaldf

shanghaksjdasdashhhhhh0000000000000000hhhhhhhhhhhhhhjssssssssssssssshsasaasdasdasd

 

0.000000000000000000

test1

[root@oldboy ~]# cat test{,1}.txt >/tmp/2018.txt

[root@oldboy ~]#

[root@oldboy ~]#

[root@oldboy ~]# cat /tmp/2018.txt

my b is http:hahah

 

my c is http:hahah

 

my d is http:hahah

 

ay c is http:hahah

by c is http:hahah

cy c is http:hahah

 

oldboy

oldboy

oldboy.

000btti000000000000anji000ngingni.

shanghaibeijingtiaks00000dkasskjdhshdjslfjslajfhsjhdflkahdlfjaldf

shanghaibeijingtiaks00000dkass0000000kjdhshdjslfjslajfhsjhdflkahdlfjaldf

shanghaksjdasdashhhhhh0000000000000000hhhhhhhhhhhhhhjssssssssssssssshsasaasdasdasd

 

0.000000000000000000

test1

[root@oldboy ~]#

 

[root@oldboy ~]# cat > /tmp/2018.txt      编辑2018.txt文件内容

i am linux

2222222

3333333

^c

[root@oldboy ~]# cat /tmp/2018.txt             

i am linux

2222222

3333333

[root@oldboy ~]#

[root@oldboy ~]# cat >>/tmp/2018.txt<<eof     交互式进行编辑2018.txt文件内容

> hello word

> mingtian nihao

> eof

[root@oldboy ~]# cat /tmp/2018.txt

i am linux

2222222

3333333

hello word

mingtian nihao

[root@oldboy ~]#

cat  -n  参数的意思是显示行号  空行也会显示行号

[root@oldboy ~]# cat -n  /tmp/2018.txt

     1  i am linux

     2  2222222

     3  3333333

     4  hello word

     5  mingtian nihao

[root@oldboy ~]#

cat -b  参数是 不为空行做标记行号

[root@oldboy ~]# cat /tmp/2018.txt

i am linux

2222222

3333333

hello word

mingtian nihao

 

 

nishishuo

whoami

wo

ni r

[root@oldboy ~]# cat -b /tmp/2018.txt    cat -b   忽略显示空白行行号

     1  i am linux

     2  2222222

     3  3333333

     4  hello word

     5  mingtian nihao

 

 

     6  nishishuo

     7  whoami

     8  wo

     9  ni r

[root@oldboy ~]#

 

cat -e参数  

[root@oldboy ~]# cat /tmp/2018.txt

i am linux

2222222

3333333

hello word

mingtian nihao

 

 

nishishuo

whoami

wo

ni r

[root@oldboy ~]# cat -e /tmp/2018.txt          在每一行行尾显示$符号(包括空行)

i am linux$

2222222$

3333333$

hello word $

mingtian nihao$

$

$

nishishuo$

whoami$

wo $

ni r$

[root@oldboy ~]#

[root@oldboy ~]# cat /tmp/2018.txt   

i am linux

2222222

3333333

hello word

mingtian nihao

 

nishishuo

whoami

wo

ni r

[root@oldboy ~]# cat -s /tmp/2018.txt 

i am linux

2222222

3333333

hello word

mingtian nihao

nishishuo

whoami

wo

ni r

[root@oldboy ~]#

 

生产环境中,用grep进行过滤空行

[root@oldbody liangli]# grep -v  "^$" test.txt

1

2

3

4

5

6

7

hello word

[root@oldbody liangli]#

 

cat  -t   参数是区分tab键和空格

[root@oldboy ~]# cat /tmp/2018.txt

i       am linux

2222222

3333333

hello word

mingtian nihao

 

 

nishishuo

whoami

wo

ni r

[root@oldboy ~]# cat -t /tmp/2018.txt

i^iam linux

2222222

3333333

hello word

mingtian nihao

 

 

nishishuo

whoami

wo

ni r

[root@oldboy ~]#

3.2 tac

反向显示文件内容(每行本文顺序没有改变)

[root@oldbody liangli]# cat /etc/rc.local

#!/bin/sh

#

# this script will be executed *after* all the other init scripts.

# you can put your own initialization stuff in here if you don't

# want to do the full sys v style init stuff.

 

touch /var/lock/subsys/local

>/etc/udev/rules.d/70-persistent-net.rules

[root@oldbody liangli]# tac /etc/rc.local

>/etc/udev/rules.d/70-persistent-net.rules

touch /var/lock/subsys/local

 

# want to do the full sys v style init stuff.

# you can put your own initialization stuff in here if you don't

# this script will be executed *after* all the other init scripts.

#

#!/bin/sh

[root@oldbody liangli]#

3.3 less、move

less  和more相反 内容一屏一屏的显示(按空格键) 回车的话是一行显示的  按b可以一次回退一屏

more  更多   按页一次一屏  内容一屏一屏的显示(按空格键)  回车的话是一行显示的  不能回退   具有和 vi编辑的一些小功能

                  = 键可以显示文本有多少行

                  /mysql   具有搜索的功能

                  v  键   可以进行编辑了

                  q        退出more             

[root@oldboy ~]# more -10 /etc/services          按10行进行显示

[root@oldboy ~]# more +10000 /etc/services     直接到10000行

[root@oldbody liangli]# ll /etc/ | more -10      用法

总用量 1720

drwxr-xr-x.  3 root root   4096 6月  15 00:28 abrt

drwxr-xr-x.  4 root root   4096 6月  15 00:30 acpi

-rw-r--r--.  1 root root     48 9月  28 23:11 adjtime

-rw-r--r--.  1 root root   1512 1月  12 2010 aliases

-rw-r--r--.  1 root root  12288 6月  15 00:33 aliases.db

drwxr-xr-x.  2 root root   4096 6月  15 00:30 alsa

drwxr-xr-x.  2 root root   4096 6月  15 00:29 alternatives

-rw-------.  1 root root    541 3月  30 2015 anacrontab

-rw-r--r--.  1 root root    148 5月  15 2009 asound.conf

--more--

less命令    分页查看文件  推荐

less -n   可以显示行号

[root@oldbody liangli]# ll /etc/ | less -n

      1 总用量 1720

      2 drwxr-xr-x.  3 root root   4096 6月  15 00:28 abrt

      3 drwxr-xr-x.  4 root root   4096 6月  15 00:30 acpi

      4 -rw-r--r--.  1 root root     48 9月  28 23:11 adjtime

      5 -rw-r--r--.  1 root root   1512 1月  12 2010 aliases

      6 -rw-r--r--.  1 root root  12288 6月  15 00:33 aliases.db

      7 drwxr-xr-x.  2 root root   4096 6月  15 00:30 alsa

      8 drwxr-xr-x.  2 root root   4096 6月  15 00:29 alternatives

      9 -rw-------.  1 root root    541 3月  30 2015 anacrontab

     10 -rw-r--r--.  1 root root    148 5月  15 2009 asound.conf

     11 -rw-r--r--.  1 root root      1 2月  20 2015 at.deny

     12 drwxr-x---.  3 root root   4096 6月  15 00:30 audisp

     13 drwxr-x---.  3 root root   4096 6月  15 00:30 audit

     14 drwxr-xr-x.  2 root root   4096 6月  15 00:30 bash_completion.d

     15 -rw-r--r--.  1 root root   2681 10月  2 2013 bashrc

     16 drwxr-xr-x.  2 root root   4096 10月 15 2014 blkid

     17 drwxr-xr-x.  2 root root   4096 6月  15 00:27 bonobo-activation

     18 -rw-r--r--.  1 root root   1780 10月 16 2009 cas.conf

     19 -rw-r--r--.  1 root root     27 8月   4 2015 centos-release

     20 drwxr-xr-x.  2 root root   4096 3月  10 2015 chkconfig.d

     21 drwxr-xr-x.  2 root root   4096 6月  15 00:29 compat-openmpi-psm-x86_64

:

3.4 head

显示文件内容头部 读取文件的前n行 默认是前10行 -n 数字(习惯-5 忽略-n) head -30 liangli.txt |tail -11   这条命令就是取数值20到30之间的数字 

-c  参数  显示文件的前五个字节

[root@oldbody liangli]# head -10 /etc/passwd

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

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

[root@oldbody liangli]#

[root@oldboy ~]# head -c 5 /etc/inittab

# ini[root@oldboy ~]#

[root@oldboy ~]#

将/etc/inittab文件后10行去掉

[root@oldboy ~]# head -n -10 /etc/inittab

# inittab is only used by upstart for the default runlevel.

#

# adding other configuration here will have no effect on your system.

#

# system initialization is started by /etc/init/rcs.conf

#

# individual runlevels are started by /etc/init/rc.conf

#

# ctrl-alt-delete is handled by /etc/init/control-alt-delete.conf

#

# terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,

# with configuration in /etc/sysconfig/init.

#

# for information on how to write upstart event handlers, or how

# upstart works, see init(5), init(8), and initctl(8).

#

[root@oldboy ~]#

显示多个文件

[root@oldbody liangli]# head /etc/passwd /etc/inittab

==> /etc/passwd <==

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

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

 

==> /etc/inittab <==

# inittab is only used by upstart for the default runlevel.

#

# adding other configuration here will have no effect on your system.

#

# system initialization is started by /etc/init/rcs.conf

#

# individual runlevels are started by /etc/init/rc.conf

#

# ctrl-alt-delete is handled by /etc/init/control-alt-delete.conf

#

[root@oldbody liangli]#

3.5 tail    

显示文件内容尾部 读取文件的后n行 默认情况下为后10行 -n 数字 习惯-5   忽略-n  

-f  参数 实时输出日志的动态变化 假如现在在一个窗口中输入 tail -f test.txt    然后在另一个窗口下echo 1111>> test.txt  会看到相应的1111的输出

-f  参数 就是test.txt这个文件事先可以不存在 但是我会等着这个文件的创建后在及时输出相应这个文件的echo内容

tailf 命令和  tail -f  的作用一样  但是 tailf  是一个单独的命令

[root@oldbody liangli]# tail -5 /etc/passwd

saslauth:x:499:76:saslauthd user:/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:privilege-separated ssh:/var/empty/sshd:/sbin/nologin

tcpdump:x:72:72::/:/sbin/nologin

user1:x:500:500::/home/user1:/bin/bash

[root@oldbody liangli]#

3.6 cut 

从文本中提取一段文字并输出

cut  -b参数   按字节进行切割   字节切割 不会对中文生效

cut  -c   按字符进行切割   字符切割  可以切割中文

cut -d  参数   指定分隔符 d和f一起用   默认情况下cut命令以tab键作为分隔符

[root@oldboy ~]# cat test.txt

i am oldboy my qq is 1234567

[root@oldboy ~]# cut -b 3 test.txt     第3个字节、字符、字段切割

a

[root@oldboy ~]# cut -b 3-4 test.txt    从第3到4个字节、字符、字段进行切割

am

[root@oldboy ~]#

[root@oldboy ~]# cut -b -4 test.txt      从第一到4个字节、字符、字段进行切割

i am

[root@oldboy ~]# cut -b 4- test.txt       从第4个字节、字符、字段进行切割

m oldboy my qq is 1234567

[root@oldboy ~]#

cut  -c   按字符进行切割   一个中文字符占2个字节

[root@oldboy ~]# cat test.txt                         

i am oldboy my qq is 1234567

i清明节放假

[root@oldboy ~]# cut -b -4 test.txt

i am

i清

cut -d  参数   指定分隔符 d和f一起用   默认情况下cut命令以tab键作为分隔符   cut命令不能支持多个字符作为分隔符

[root@oldboy ~]# head -1 /etc/passwd

root:x:0:0:root:/root:/bin/bash

[root@oldboy ~]# head -1 /etc/passwd |cut -d : -f1

root

[root@oldboy ~]# head -1 /etc/passwd |cut -d : -f2

x

[root@oldboy ~]# head -1 /etc/passwd |cut -d : -f3

0

[root@oldboy ~]# head -1 /etc/passwd |cut -d : -f4

0

[root@oldboy ~]#

 

[root@oldbody ~]# cat /etc/passwd | cut -d : -f 1-3

root:x:0

bin:x:1

daemon:x:2

adm:x:3

lp:x:4

sync:x:5

shutdown:x:6

halt:x:7

mail:x:8

uucp:x:10

operator:x:11

games:x:12

gopher:x:13

ftp:x:14

nobody:x:99

dbus:x:81

vcsa:x:69

abrt:x:173

haldaemon:x:68

ntp:x:38

saslauth:x:499

postfix:x:89

sshd:x:74

tcpdump:x:72

user1:x:500

[root@oldbody ~]#

[root@oldboy ~]# cat -t test.txt

this^iis^itab^iline

 

this is ni hao

[root@oldboy ~]# cut -f 2-3 test.txt

is      tab

 

this is ni hao

[root@oldboy ~]#

[root@oldboy ~]# cat test.txt                

this    is      tab     line

 

this is ni hao

[root@oldboy ~]# cut  -d ' '  -f 2-3 test.txt   一个空格

this    is      tab     line

 

is ni

[root@oldboy ~]#

[root@oldboy ~]# cut  -d '  '  -f 2-3 test.txt      两个空格

cut: 分界符必须是单个字符

请尝试执行"cut --help"来获取更多信息。

[root@oldboy ~]#

3.7 spilt

按照指定的行数或大小分割文件

-l  指定分割文件后最大行数

-a  指定后缀长度,默认2个字母

-d  使用数字后缀

-b  指定分割文件的最大字节数

[root@oldboy ~]# wc -l /etc/inittab

26 /etc/inittab

[root@oldboy ~]# split -l 10 /etc/inittab new_

[root@oldboy ~]#

[root@oldboy ~]# ls new_*

new_aa  new_ab  new_ac

[root@oldboy ~]# head new_aa  new_ab  new_ac

==> new_aa <==

# inittab is only used by upstart for the default runlevel.

#

# adding other configuration here will have no effect on your system.

#

# system initialization is started by /etc/init/rcs.conf

#

# individual runlevels are started by /etc/init/rc.conf

#

# ctrl-alt-delete is handled by /etc/init/control-alt-delete.conf

#

 

==> new_ab <==

# terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,

# with configuration in /etc/sysconfig/init.

#

# for information on how to write upstart event handlers, or how

# upstart works, see init(5), init(8), and initctl(8).

#

# default runlevel. the runlevels used are:

#   0 - halt (do not set initdefault to this)

#   1 - single user mode

#   2 - multiuser, without nfs (the same as 3, if you do not have networking)

 

==> new_ac <==

#   3 - full multiuser mode

#   4 - unused

#   5 - x11

#   6 - reboot (do not set initdefault to this)

#

id:3:initdefault:

-a  指定生成文件后缀长度

[root@oldboy ~]# split -l 10 -a 4 /etc/inittab new2_

[root@oldboy ~]# wc -l new2_*

 10 new2_aaaa

 10 new2_aaab

  6 new2_aaac

 26 总用量

[root@oldboy ~]#

-d  使用数字后缀

[root@oldboy ~]# split -l 10 -d  /etc/inittab new3_  

[root@oldboy ~]# wc -l new3_*                     

 10 new3_00

 10 new3_01

  6 new3_02

 26 总用量

[root@oldboy ~]#

-b  指定分割大小

[root@oldboy 2016]# ll -h

总用量 80k

-rw-r--r-- 1 root root 77k 9月  24 20:54 keykey.txt

[root@oldboy 2016]#

[root@oldboy 2016]# split -b 20k keykey.txt

[root@oldboy 2016]# ll -h

总用量 160k

-rw-r--r-- 1 root root 77k 9月  24 20:54 keykey.txt

-rw-r--r-- 1 root root 20k 9月  24 20:54 xaa

-rw-r--r-- 1 root root 20k 9月  24 20:54 xab

-rw-r--r-- 1 root root 20k 9月  24 20:54 xac

-rw-r--r-- 1 root root 17k 9月  24 20:54 xad

[root@oldboy 2016]#

3.8 paste

合并文件 能将文件按照行与行进行合并,中间使用tab隔开

-d 参数 指定合并的分隔符 默认是tab

-s 参数  每个文件占用一行

[root@oldboy 2016]# cat num.txt

1

2

3

[root@oldboy 2016]# cat newaa

ming tian ni hao

hello word

[root@oldboy 2016]# cat num.txt newaa   读取多个文件

1

2

3

ming tian ni hao

hello word

[root@oldboy 2016]# paste num.txt newaa

1       ming tian ni hao

2       hello word

3

[root@oldboy 2016]#

-d  指定分隔符

[root@oldboy 2016]# paste -d : num.txt newaa

1:ming tian ni hao

2:hello word

3:

[root@oldboy 2016]#

-s  参数 每个文件占用一行  

[root@oldboy 2016]# paste -s  num.txt newaa 

1       2       3

ming tian ni hao        hello word

[root@oldboy 2016]#

[root@oldbody liangli]# cat a.txt

1

2

3

4

5

6

7

8

9

10

[root@oldbody liangli]# cat b.txt

hello word

 

wuhan

shanghai

[root@oldbody liangli]# paste a.txt   

1

2

3

4

5

6

7

8

9

10

[root@oldbody liangli]# paste -s a.txt

1       2       3       4       5       6       7       8       9       10

[root@oldbody liangli]# paste -s b.txt

hello word              wuhan   shanghai

[root@oldbody liangli]#

3.9 sort

文本排序

-n  参数 按照数值排序

-r  参数 倒叙排序 从大到小排序

-u  参数  可以压缩重复行(可以压缩不相连的行)

-t  参数 指定分隔符

-k  参数 指定区域

[root@oldboy 2016]# cat oldboy.txt

10.0.0.4

10.0.0.4

10.0.0.4

10.0.0.5

10.0.0.4

10.0.0.3

10.0.0.7

10.0.0.8

默认按照ascii码排序 比较原则是从首字符向后 升序 从小到大

[root@oldboy 2016]# sort oldboy.txt

10.0.0.3

10.0.0.4

10.0.0.4

10.0.0.4

10.0.0.4

10.0.0.5

10.0.0.7

10.0.0.8

[root@oldboy 2016]#

-n  参数 按照数值排序

-r  从大到小排序

[root@oldboy 2016]# sort -r  oldboy.txt

10.0.0.8

10.0.0.7

10.0.0.5

10.0.0.4

10.0.0.4

10.0.0.4

10.0.0.4

10.0.0.3

[root@oldboy 2016]#

-u  参数  可以压缩重复行(可以压缩不相连的行)

[root@oldboy 2016]# sort -u  oldboy.txt 

10.0.0.3

10.0.0.4

10.0.0.5

10.0.0.7

10.0.0.8

[root@oldboy 2016]#

其实-u  就是uniq 我们也可以用uniq参数进行相应的实现(必须压缩相连行)

[root@oldboy 2016]# uniq oldboy.txt

10.0.0.4

10.0.0.5

10.0.0.4

10.0.0.3

10.0.0.7

10.0.0.8

[root@oldboy 2016]#

-rn   先按照数值排序   然后按照数值倒叙排序

[root@oldboy 2016]# sort -rn oldboy.txt 

10.0.0.8

10.0.0.7

10.0.0.5

10.0.0.4

10.0.0.4

10.0.0.4

10.0.0.4

10.0.0.3

[root@oldboy 2016]#

默认按照第一列排序

[root@oldboy 2016]# sort oldboy.txt

10.0.0.3 c

10.0.0.4 g

10.0.0.4 k

10.0.0.4 r

10.0.0.4 y

10.0.0.5 a

10.0.0.7 f

10.0.0.8 d

[root@oldboy 2016]#

-t  指定分隔符

-k  指定区域

[root@oldboy 2016]# sort -k oldboy.txt

sort: 区块起始处的编号无效:在"oldboy.txt" 处的计数无效

[root@oldboy 2016]# sort -k2 oldboy.txt    默认以空格作为分隔符的 k2就是第二列排序

10.0.0.5 a

10.0.0.3 c

10.0.0.8 d

10.0.0.7 f

10.0.0.4 g

10.0.0.4 k

10.0.0.4 r

10.0.0.4 y

[root@oldboy 2016]#

中间是tab键

[root@oldboy 2016]# cat oldboy.txt

10.0.0.4        r

10.0.0.4        g

10.0.0.4        d

10.0.0.5        a

10.0.0.4        k

10.0.0.3        c

10.0.0.7        f

10.0.0.8        d

[root@oldboy 2016]# sort -k2 oldboy.txt     默认也可以以tab作为空格符

10.0.0.5        a

10.0.0.3        c

10.0.0.4        d

10.0.0.8        d

10.0.0.7        f

10.0.0.4        g

10.0.0.4        k

10.0.0.4        r

[root@oldboy 2016]#

[root@oldboy 2016]# sort -k2 oldboy.txt 

10.0.0.3:c

10.0.0.4:d

10.0.0.4:g

10.0.0.4:k

10.0.0.4:r

10.0.0.5:a

10.0.0.7:f

10.0.0.8:d

[root@oldboy 2016]# sort -t: -k2 oldboy.txt

10.0.0.5:a

10.0.0.3:c

10.0.0.4:d

10.0.0.8:d

10.0.0.7:f

10.0.0.4:g

10.0.0.4:k

10.0.0.4:r

[root@oldboy 2016]#

3.10 join

按两个文件的相同字段合并

使用join合并文件的要求是2个文件必须是sort排序后的

[root@oldbody liangli]# cat a.txt

key1 25

now2 25

route3 24

[root@oldbody liangli]# cat b.txt  

key1 nan

route3 lvu

now2 nan

[root@oldbody liangli]# join a.txt b.txt

key1 25 nan

join: 文件2 没有被正确排序

route3 24 lvu

[root@oldbody liangli]# sort a.txt >a.txtn

[root@oldbody liangli]# sort b.txt >b.txtn

[root@oldbody liangli]# join a.txtn b.txtn

key1 25 nan

now2 25 nan

route3 24 lvu

[root@oldbody liangli]#

3.11 uniq

去除重复行

-c 参数 去除重复行 并计算每行出现的次数

[root@oldbody liangli]# cat c.txt

10.0.0.4

10.0.0.4

10.0.0.4

10.0.0.4

[root@oldbody liangli]# uniq c.txt

10.0.0.4

[root@oldbody liangli]# uniq -c c.txt    参数-c显示相应行出现的次数

      4 10.0.0.4

[root@oldbody liangli]#

[root@oldbody liangli]# uniq c.txt       uniq只能对相邻的重复行进行去重操作

10.0.0.4

10.0.0.3

10.0.0.4

10.0.0.6

 

10.0.0.4

10.0.0.5

10.0.0.4

[root@oldbody liangli]# sort c.txt

 

10.0.0.3

10.0.0.4

10.0.0.4

10.0.0.4

10.0.0.4

10.0.0.4

10.0.0.4

10.0.0.5

10.0.0.6

[root@oldbody liangli]# sort c.txt | uniq

 

10.0.0.3

10.0.0.4

10.0.0.5

10.0.0.6

[root@oldbody liangli]# sort c.txt | uniq  -c

      1

      1 10.0.0.3

      6 10.0.0.4

      1 10.0.0.5

      1 10.0.0.6

[root@oldbody liangli]#

3.12 wc

统计文件的行数,单词数,字节数

wc   生厕所显示   

-l  (lines) 总行数     

-l  字符数  指的是精确的字符数目

[root@oldbody liangli]# wc /etc/inittab

 26 149 884 /etc/inittab

[root@oldbody liangli]# wc -l /etc/inittab     l表示总行数

26 /etc/inittab

[root@oldbody liangli]# wc -w /etc/inittab     w表示单词数

149 /etc/inittab

[root@oldbody liangli]# wc -c /etc/inittab      c表示字节数

[root@oldbody liangli]# wc -l /etc/inittab      -l表示最长行的长度

[root@oldbody liangli]# wc -m /etc/inittab      -m表示字符数

884 /etc/inittab

[root@oldbody liangli]#

78 /etc/inittab

[root@oldbody liangli]#

884 /etc/inittab

[root@oldbody liangli]#

[root@oldbody ~]# cat -n /etc/services | tail -1

 10774  iqobject        48619/udp               # iqobject

[root@oldbody ~]# wc -l /etc/services

10774 /etc/services

[root@oldbody ~]#

sshd服务有1,说明该服务是存活的

[root@oldbody ~]# ps -ef | grep "/sshd"

root       1637      1  0 20:27 ?        00:00:00 /usr/sbin/sshd

root       2474   2414  0 22:48 pts/0    00:00:00 grep /sshd

[root@oldbody ~]# ps -ef | grep "/sshd"  | grep -v grep

root       1637      1  0 20:27 ?        00:00:00 /usr/sbin/sshd

[root@oldbody ~]# ps -ef | grep "/sshd"  | grep -v grep | wc -l

1

[root@oldbody ~]#

[root@oldbody ~]# netstat -lntup | grep sshd

tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   listen      1637/sshd          

tcp        0      0 :::22                       :::*                        listen      1637/sshd          

[root@oldbody ~]# netstat -lntup | grep sshd | wc -l

2

[root@oldbody ~]#

3.13 iconv

转换文件的编码格式

linux系统是utf-8的编码,而win7系统是gb2312的编码,从英文的角度来讲,二者没有区别,但是win编辑的中文字符到linux系统中就会有乱码,需要先转码在处理

使用-f参数指定文件原来的编码为gb2312 使用-t参数指定将要转换的编码utf-8

[root@oldbody ~]# iconv -f gb2312 -t utf-8 test.txt

3.14 dos2unix

把windows平台的格式转换成unix平台的格式  

先安装  yum -y install dos2unix

windows换行符  \r\n

linux换行符 \n

dos2unix 后面接文件

3.15 diff

比较两个文件异同(可以比较目录内的文件不同)只能同时比较两个文件

diff默认显示格式有如下三种提示

a   是add增加的意思

c   是改变

d   删除

[root@oldbody liangli]# cat a.txt

1

2

3

4

5

6

[root@oldbody liangli]# cat b.txt 

4

5

6

7

8

[root@oldbody liangli]# diff a.txt b.txt

1,3d0

< 1

< 2

< 3

6a4,5

> 7

> 8

[root@oldbody liangli]#

d/a前面的数字是文本1的行号,字母后面的文本2的行号,其中<打头的行属于文件1,以>打头的行属于文件2

[root@oldbody liangli]# diff -y a.txt b.txt

1                                                             <

2                                                             <

3                                                             <

4                                                               4

5                                                               5

6                                                               6

                                                              > 7

                                                              > 8

[root@oldbody liangli]#

[root@oldbody liangli]# diff -y -w 30 a.txt b.txt        -w参数指定宽度

1             <

2             <

3             <

4               4

5               5

6               6

              > 7

              > 8

[root@oldbody liangli]#

[root@oldbody liangli]# diff -c a.txt b.txt           -c参数可以上下文输出

*** a.txt       2018-09-30 17:56:06.289845188 +0800

--- b.txt       2018-09-30 17:56:50.296847597 +0800

***************

*** 1,6 ****

- 1

- 2

- 3

  4

  5

  6

--- 1,5 ----

  4

  5

  6

+ 7

+ 8

[root@oldbody liangli]#

[root@oldbody liangli]# diff -u a.txt b.txt      -u参数 使用统一格式输出

--- a.txt       2018-09-30 17:56:06.289845188 +0800

+++ b.txt       2018-09-30 17:56:50.296847597 +0800

@@ -1,6 +1,5 @@

-1

-2

-3

 4

 5

 6

+7

+8

[root@oldbody liangli]#

比较两个目录

[root@oldbody liangli]# tree

.

├── a

│   ├── 1

│   ├── 1.txt

│   ├── 2

│   ├── 2.txt

│   ├── 3

│   └── 3.txt

├── a.txt

├── b

│   ├── 2

│   ├── 2.txt

│   ├── 3

│   ├── 3.txt

│   ├── 4

│   └── 4.txt

└── b.txt

 

8 directories, 8 files

[root@oldbody liangli]# diff a b

only in a: 1

only in a: 1.txt

common subdirectories: a/2 and b/2

common subdirectories: a/3 and b/3

only in b: 4

only in b: 4.txt

[root@oldbody liangli]#

3.16 vimdiff  

可视化对比工具 后面可以接4个文件进行同时对比

[root@oldbody liangli]# vimdiff a.txt b.txt

还有 2 个文件等待编辑

  1                                          |  ------------------------------------------

  2                                          |  ------------------------------------------

  3                                          |  ------------------------------------------

  4                                          |  4

  5                                          |  5

  6                                          |  6

  -------------------------------------------|  7                                        

  -------------------------------------------|  8                                         

  ~                                          |  ~                                        

  ~                                          |  ~                                        

  ~                                          |  ~                                         

  ~                                          |  ~                                        

  ~                                          |  ~                                        

  ~                                          |  ~                                        

  ~                                          |  ~                                        

  ~                                          |  ~                                        

  ~                                          |  ~                                        

a.txt                      1,1           全部 b.txt                     1,1           全部

"b.txt" 5l, 10c

退出的话,需要执行2次vim的操作:q

3.17 rev

反向读取文件内容

[root@oldboy ~]# echo 123456|rev

654321

[root@oldbody liangli]# cat a.txt

1 2 3 4 5 6 7 8 9 10

[root@oldbody liangli]# rev a.txt

01 9 8 7 6 5 4 3 2 1

[root@oldbody liangli]#

3.18 tr   

替换或删除字符

-d 参数 删除字符

-s 参数 将连续的字符压缩成一个

-c 参数 取反的意思

[root@oldboy ~]# cat person.txt

101,oldboy,ceo

102,zhangyao,cto

103,alex,coo

104,yy,cfo

105,feixue,cio

011111111

0111111100

111

11111

1111111

[root@oldboy ~]# tr 'abc' 'abc' < person.txt     将所有的小写abc转换成大写abc 注意 是一一对应的

101,oldboy,ceo

102,zhangyao,cto

103,alex,coo

104,yy,cfo

105,feixue,cio

011111111

0111111100

111

11111

1111111

[root@oldboy ~]#

[root@oldboy ~]# tr '[a-z]' '[a-z]' < person.txt       

101,oldboy,ceo

102,zhangyao,cto

103,alex,coo

104,yy,cfo

105,feixue,cio

011111111

0111111100

111

11111

1111111

[root@oldboy ~]#

[root@oldboy ~]# tr '[0-9]' '[a-z]' < person.txt    注意一一对应

bab,oldboy,ceo

bac,zhangyao,cto

bad,alex,coo

bae,yy,cfo

baf,feixue,cio

abbbbbbbb

abbbbbbbaa

bbb

bbbbb

bbbbbbb

[root@oldboy ~]#

-d  参数  删除的功能    

[root@oldboy ~]# cat person.txt

101,oldboy,ceo

102,zhangyao,cto

103,alex,coo

104,yy,cfo

105,feixue,cio

011111111

0111111100

111

11111

1111111

[root@oldboy ~]# tr -d 0 < person.txt

11,oldboy,ceo

12,zhangyao,cto

13,alex,coo

14,yy,cfo

15,feixue,cio

11111111

1111111

111

11111

1111111

[root@oldboy ~]#

凡是在文件中出现的o l d b o y字符都会被删除掉,而不是只删除oldboy字符串

[root@oldbody liangli]# cat a.txt           

oldboyoldbyonihaowoshiliang

[root@oldbody liangli]# tr -d 'oldboy' < a.txt

nihawshiiang

[root@oldbody liangli]#

 

也可以将换行符删除掉

[root@oldboy ~]# tr -d '\n'< person.txt    

101,oldboy,ceo102,zhangyao,cto103,alex,coo104,yy,cfo105,feixue,cio0111111110111111100111111111111111[root@oldboy ~]#

[root@oldboy ~]# tr '\n' '='< person.txt      

101,oldboy,ceo=102,zhangyao,cto=103,alex,coo=104,yy,cfo=105,feixue,cio=011111111=0111111100=111=11111=1111111=[root@oldboy ~]#

 

-s参数将连续的字符压缩成一个

[root@oldbody liangli]# echo 'oooolllddbbboyyyyy'

oooolllddbbboyyyyy

[root@oldbody liangli]# echo 'oooolllddbbboyyyyy' | tr -s oldboy

oldboy

[root@oldbody liangli]#

3.19 od

用于输出文件的八进制、十六进制或者其他格式编码的字节 如od /bin/ls

3.20 tee

多重定向  比如 一边把相应的结果输入到屏幕 一边把结果输入到保存的

文件中

-a 参数 追加的意思

[root@oldboy ~]# ls

2016                   key_2018-09-19.tar.gz      new2_aaac   test.txt

a                      key_2018-09-20.tar.gz      new3_00     text2018.txt

anaconda-ks.cfg        liangli                    new3_01     text2018.txt.back

d065                   liangli1                   new3_02     text.txt

data                   liangli123.txt             new_aa      wuhan20181.txt

f043                   liangli_2018-09-18.tar.gz  new_ab      wuhan20182.txt

f044                   liangli.tar.gz             new_ac      wuhan20183.txt

f055                   lihao                      oldboy      wuhan20184.txt

install.log            lihao_2018-09-18.tar.gz    person.txt  xargs

install.log.syslog     md5.log                    test        xiaomi2.txt

key                    new2_aaaa                  test1.txt   xiaomi3.txt

key_2018-09-17.tar.gz  new2_aaab                  test.hard   xiaomi.txt

[root@oldboy ~]# ls | tee /tmp/tee.txt

2016

a

anaconda-ks.cfg

d065

data

f043

f044

f055

install.log

install.log.syslog

key

key_2018-09-17.tar.gz

key_2018-09-19.tar.gz

key_2018-09-20.tar.gz

liangli

liangli1

liangli123.txt

liangli_2018-09-18.tar.gz

liangli.tar.gz

lihao

lihao_2018-09-18.tar.gz

md5.log

new2_aaaa

new2_aaab

new2_aaac

new3_00

new3_01

new3_02

new_aa

new_ab

new_ac

oldboy

person.txt

test

test1.txt

test.hard

test.txt

text2018.txt

text2018.txt.back

text.txt

wuhan20181.txt

wuhan20182.txt

wuhan20183.txt

wuhan20184.txt

xargs

xiaomi2.txt

xiaomi3.txt

xiaomi.txt

[root@oldboy ~]#

[root@oldboy ~]# cat /tmp/tee.txt

2016

a

anaconda-ks.cfg

d065

data

f043

f044

f055

install.log

install.log.syslog

key

key_2018-09-17.tar.gz

key_2018-09-19.tar.gz

key_2018-09-20.tar.gz

liangli

liangli1

liangli123.txt

liangli_2018-09-18.tar.gz

liangli.tar.gz

lihao

lihao_2018-09-18.tar.gz

md5.log

new2_aaaa

new2_aaab

new2_aaac

new3_00

new3_01

new3_02

new_aa

new_ab

new_ac

oldboy

person.txt

test

test1.txt

test.hard

test.txt

text2018.txt

text2018.txt.back

text.txt

wuhan20181.txt

wuhan20182.txt

wuhan20183.txt

wuhan20184.txt

xargs

xiaomi2.txt

xiaomi3.txt

xiaomi.txt

[root@oldboy ~]#

-a 参数  追加的意思 不加参数-a 覆盖/tmp/tee.txt文件内容

[root@oldboy ~]# wc -l /tmp/tee.txt

48 /tmp/tee.txt

[root@oldboy ~]# ls | tee  -a /tmp/tee.txt

2016

a

anaconda-ks.cfg

d065

data

f043

f044

f055

install.log

install.log.syslog

key

key_2018-09-17.tar.gz

key_2018-09-19.tar.gz

key_2018-09-20.tar.gz

liangli

liangli1

liangli123.txt

liangli_2018-09-18.tar.gz

liangli.tar.gz

lihao

lihao_2018-09-18.tar.gz

md5.log

new2_aaaa

new2_aaab

new2_aaac

new3_00

new3_01

new3_02

new_aa

new_ab

new_ac

oldboy

person.txt

test

test1.txt

test.hard

test.txt

text2018.txt

text2018.txt.back

text.txt

wuhan20181.txt

wuhan20182.txt

wuhan20183.txt

wuhan20184.txt

xargs

xiaomi2.txt

xiaomi3.txt

xiaomi.txt

[root@oldboy ~]# wc -l /tmp/tee.txt       

96 /tmp/tee.txt

[root@oldboy ~]#

3.21 vi、vim

在tech目录下输入vi oldboy.txt  visual interface(可视界面) i  进入编辑状态  i am studying linux   按esc :wq  输入命令cat oldboy.txt查看oldboy.txt里面的内容 vi相当于windows的记事本 简单

vim相当于复杂的编辑器 功能复杂,高亮 自动缩进(写shell/python脚本用) :q不想保存退出  :q!  强制退出

vim的三种模式

1、普通模式

用vim命令打开一个文件,默认的状态就是普通文件,在这个模式中,不能进行编辑输入操作,但可以按“上下左右”键来移动光标,也可以执行一些操作命令进行如删除、复制、粘贴等之类的工作

2、编辑模式

在普通模式下按i进入编辑模式,可以看到窗口左下角有插入的标记“insert”或“插入”

3、命令模式

在普通模式下,输入:或者/或者?时,光标会自动定位在那一行,在这个模式中,可以执行保存,退出,搜索,显示行号等相关操作

 

vim命令的重要参数选项及说明

普通模式:

跳至开头  按gg就行

跳至末尾  按g就行

行首      0(数字0)

行末         $

按:        输入set nu 开启行号

ngg        移动到第n行,如11gg

dd         删除当前一行

yy         复制  按p就是粘贴

u           撤销