1. 程式人生 > >第三章 檔案過濾及內容編輯處理命令

第三章 檔案過濾及內容編輯處理命令

3.1 cat

cat命令作用:

1、檢視檔案內容

2、把多個檔案合併成一個檔案

3、編輯檔案內容

4、結合>>和<<EOF進行編輯

5、清空檔案內容

-n引數 從1開始對所有輸出的內容按行編號

-b引數 忽略顯示空白行行號

-E引數 在每一行行尾顯示$符號(即使是空行 結尾也是有結束標識的)

-s引數 當遇到有連續兩行以上的空白行時,就替代為一行空白行

[[email protected] ~]# cat test1.txt

test1

[[email protected] ~]# 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

[
[email protected]
~]# cat test{,1}.txt >/tmp/2018.txt [[email protected] ~]# [[email protected] ~]# [[email protected] ~]# 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 [
[email protected]
~]# [[email protected] ~]# cat > /tmp/2018.txt 編輯2018.txt檔案內容 I am Linux 2222222 3333333 ^C [[email protected] ~]# cat /tmp/2018.txt I am Linux 2222222 3333333 [[email protected] ~]# [[email protected] ~]# cat >>/tmp/2018.txt<<EOF 互動式進行編輯2018.txt檔案內容 > hello word > mingtian nihao > EOF [
[email protected]
~]# cat /tmp/2018.txt I am Linux 2222222 3333333 hello word mingtian nihao [[email protected] ~]# cat -n 引數的意思是顯示行號 空行也會顯示行號 [[email protected] ~]# cat -n /tmp/2018.txt 1 I am Linux 2 2222222 3 3333333 4 hello word 5 mingtian nihao [[email protected] ~]# cat -b 引數是 不為空行做標記行號 [[email protected] ~]# cat /tmp/2018.txt I am Linux 2222222 3333333 hello word mingtian nihao nishishuo whoami wo ni r [[email protected] ~]# 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 [[email protected] ~]# cat -E引數 [[email protected] ~]# cat /tmp/2018.txt I am Linux 2222222 3333333 hello word mingtian nihao nishishuo whoami wo ni r [[email protected] ~]# cat -E /tmp/2018.txt 在每一行行尾顯示$符號(包括空行) I am Linux$ 2222222$ 3333333$ hello word $ mingtian nihao$ $ $ nishishuo$ whoami$ wo $ ni r$ [[email protected] ~]# [[email protected] ~]# cat /tmp/2018.txt I am Linux 2222222 3333333 hello word mingtian nihao nishishuo whoami wo ni r [[email protected] ~]# cat -s /tmp/2018.txt I am Linux 2222222 3333333 hello word mingtian nihao nishishuo whoami wo ni r [[email protected] ~]# 生產環境中,用grep進行過濾空行 [[email protected] liangli]# grep -v "^$" test.txt 1 2 3 4 5 6 7 hello word [[email protected] liangli]# cat -T 引數是區分tab鍵和空格 [[email protected] ~]# cat /tmp/2018.txt I am Linux 2222222 3333333 hello word mingtian nihao nishishuo whoami wo ni r [[email protected] ~]# cat -T /tmp/2018.txt I^Iam Linux 2222222 3333333 hello word mingtian nihao nishishuo whoami wo ni r [[email protected] ~]#

3.2 tac

反向顯示檔案內容(每行本文順序沒有改變)

[[email protected] 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

[[email protected] 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

[[email protected] liangli]#

3.3 less、move

less  和more相反 內容一屏一屏的顯示(按空格鍵) 回車的話是一行顯示的  按b可以一次回退一屏

more  更多   按頁一次一屏  內容一屏一屏的顯示(按空格鍵)  回車的話是一行顯示的  不能回退   具有和 vi編輯的一些小功能

                  = 鍵可以顯示文字有多少行

                  /mysql   具有搜尋的功能

                  v  鍵   可以進行編輯了

                  q        退出more             

[[email protected] ~]# more -10 /etc/services          按10行進行顯示

[[email protected] ~]# more +10000 /etc/services     直接到10000行

[[email protected] 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   可以顯示行號

[[email protected] 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  引數  顯示檔案的前五個位元組

[[email protected] 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

[[email protected] liangli]#

[[email protected] ~]# head -c 5 /etc/inittab

# ini[[email protected] ~]#

[[email protected] ~]#

將/etc/inittab檔案後10行去掉

[[email protected] ~]# 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).

#

[[email protected] ~]#

顯示多個檔案

[[email protected] 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

#

[[email protected] 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  是一個單獨的命令

[[email protected] 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

[[email protected] liangli]#

3.6 cut 

從文字中提取一段文字並輸出

cut  -b引數   按位元組進行切割   位元組切割 不會對中文生效

cut  -c   按字元進行切割   字元切割  可以切割中文

cut -d  引數   指定分隔符 d和f一起用   預設情況下cut命令以tab鍵作為分隔符

[[email protected] ~]# cat test.txt

I am oldboy my qq is 1234567

[[email protected] ~]# cut -b 3 test.txt     第3個位元組、字元、欄位切割

a

[[email protected] ~]# cut -b 3-4 test.txt    從第3到4個位元組、字元、欄位進行切割

am

[[email protected] ~]#

[[email protected] ~]# cut -b -4 test.txt      從第一到4個位元組、字元、欄位進行切割

I am

[[email protected] ~]# cut -b 4- test.txt       從第4個位元組、字元、欄位進行切割

m oldboy my qq is 1234567

[[email protected] ~]#

cut  -c   按字元進行切割   一箇中文字元佔2個位元組

[[email protected] ~]# cat test.txt                         

I am oldboy my qq is 1234567

i清明節放假

[[email protected] ~]# cut -b -4 test.txt

I am

i清

cut -d  引數   指定分隔符 d和f一起用   預設情況下cut命令以tab鍵作為分隔符   cut命令不能支援多個字元作為分隔符

[[email protected] ~]# head -1 /etc/passwd

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

[[email protected] ~]# head -1 /etc/passwd |cut -d : -f1

root

[[email protected] ~]# head -1 /etc/passwd |cut -d : -f2

x

[[email protected] ~]# head -1 /etc/passwd |cut -d : -f3

0

[[email protected] ~]# head -1 /etc/passwd |cut -d : -f4

0

[[email protected] ~]#

 

[[email protected] ~]# 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

[[email protected] ~]#

[[email protected] ~]# cat -T test.txt

this^Iis^Itab^Iline

 

this is ni hao

[[email protected] ~]# cut -f 2-3 test.txt

is      tab

 

this is ni hao

[[email protected] ~]#

[[email protected] ~]# cat test.txt                

this    is      tab     line

 

this is ni hao

[[email protected] ~]# cut  -d ' '  -f 2-3 test.txt   一個空格

this    is      tab     line

 

is ni

[[email protected] ~]#

[[email protected] ~]# cut  -d '  '  -f 2-3 test.txt      兩個空格

cut: 分界符必須是單個字元

請嘗試執行"cut --help"來獲取更多資訊。

[[email protected]dboy ~]#

3.7 spilt

按照指定的行數或大小分割檔案

-l  指定分割檔案後最大行數

-a  指定字尾長度,預設2個字母

-d  使用數字字尾

-b  指定分割檔案的最大位元組數

[[email protected] ~]# wc -l /etc/inittab

26 /etc/inittab

[[email protected] ~]# split -l 10 /etc/inittab new_

[[email protected] ~]#

[[email protected] ~]# ls new_*

new_aa  new_ab  new_ac

[[email protected] ~]# 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  指定生成檔案字尾長度

[[email protected] ~]# split -l 10 -a 4 /etc/inittab new2_

[[email protected] ~]# wc -l new2_*

 10 new2_aaaa

 10 new2_aaab

  6 new2_aaac

 26 總用量

[[email protected] ~]#

-d  使用數字字尾

[[email protected] ~]# split -l 10 -d  /etc/inittab new3_  

[[email protected] ~]# wc -l new3_*                     

 10 new3_00

 10 new3_01

  6 new3_02

 26 總用量

[[email protected] ~]#

-b  指定分割大小

[[email protected] 2016]# ll -h

總用量 80K

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

[[email protected] 2016]#

[[email protected] 2016]# split -b 20K keykey.txt

[[email protected] 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

[[email protected] 2016]#

3.8 paste

合併檔案 能將檔案按照行與行進行合併,中間使用tab隔開

-d 引數 指定合併的分隔符 預設是Tab

-s 引數  每個檔案佔用一行

[[email protected] 2016]# cat num.txt

1

2

3

[[email protected] 2016]# cat newaa

ming tian ni hao

hello word

[[email protected] 2016]# cat num.txt newaa   讀取多個檔案

1

2

3

ming tian ni hao

hello word

[[email protected] 2016]# paste num.txt newaa

1       ming tian ni hao

2       hello word

3

[[email protected] 2016]#

-d  指定分隔符

[[email protected] 2016]# paste -d : num.txt newaa

1:ming tian ni hao

2:hello word

3:

[[email protected] 2016]#

-s  引數 每個檔案佔用一行  

[[email protected] 2016]# paste -s  num.txt newaa 

1       2       3

ming tian ni hao        hello word

[[email protected] 2016]#

[[email protected] liangli]# cat a.txt

1

2

3

4

5

6

7

8

9

10

[[email protected] liangli]# cat b.txt

hello word

 

wuhan

shanghai

[[email protected] liangli]# paste a.txt   

1

2

3

4

5

6

7

8

9

10

[[email protected] liangli]# paste -s a.txt

1       2       3       4       5       6       7       8       9       10

[[email protected] liangli]# paste -s b.txt

hello word              wuhan   shanghai

[[email protected] liangli]#

3.9 sort

文字排序

-n  引數 按照數值排序

-r  引數 倒敘排序 從大到小排序

-u  引數  可以壓縮重複行(可以壓縮不相連的行)

-t  引數 指定分隔符

-k  引數 指定區域

[[email protected] 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碼排序 比較原則是從首字元向後 升序 從小到大

[[email protected] 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

[[email protected] 2016]#

-n  引數 按照數值排序

-r  從大到小排序

[[email protected] 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

[[email protected] 2016]#

-u  引數  可以壓縮重複行(可以壓縮不相連的行)

[[email protected] 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

[[email protected] 2016]#

其實-u  就是uniq 我們也可以用uniq引數進行相應的實現(必須壓縮相連行)

[[email protected] 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

[[email protected] 2016]#

-rn   先按照數值排序   然後按照數值倒敘排序

[[email protected] 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

[[email protected] 2016]#

預設按照第一列排序

[[email protected] 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

[[email protected] 2016]#

-t  指定分隔符

-k  指定區域

[[email protected] 2016]# sort -k oldboy.txt

sort: 區塊起始處的編號無效:在"oldboy.txt" 處的計數無效

[[email protected] 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

[[email protected] 2016]#

中間是tab鍵

[[email protected] 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

[[email protected] 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

[[email protected] 2016]#

[[email protected] 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

[[email protected] 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

[[email protected] 2016]#

3.10 join

按兩個檔案的相同欄位合併

使用join合併檔案的要求是2個檔案必須是sort排序後的

[[email protected] liangli]# cat a.txt

key1 25

now2 25

route3 24

[[email protected] liangli]# cat b.txt  

key1 nan

route3 lvu

now2 nan

[[email protected] liangli]# join a.txt b.txt

key1 25 nan

join: 檔案2 沒有被正確排序

route3 24 lvu

[[email protected] liangli]# sort a.txt >a.txtn

[[email protected] liangli]# sort b.txt >b.txtn

[[email protected] liangli]# join a.txtn b.txtn

key1 25 nan

now2 25 nan

route3 24 lvu

[[email protected] liangli]#

3.11 uniq

去除重複行

-c 引數 去除重複行 並計算每行出現的次數

[[email protected] liangli]# cat c.txt

10.0.0.4

10.0.0.4

10.0.0.4

10.0.0.4

[[email protected] liangli]# uniq c.txt

10.0.0.4

[[email protected] liangli]# uniq -c c.txt    引數-c顯示相應行出現的次數

      4 10.0.0.4

[[email protected] liangli]#

[[email protected] 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

[[email protected] 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

[[email protected] liangli]# sort c.txt | uniq

 

10.0.0.3

10.0.0.4

10.0.0.5

10.0.0.6

[[email protected] 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

[[email protected] liangli]#

3.12 wc

統計檔案的行數,單詞數,位元組數

wc   生廁所顯示   

-l  (lines) 總行數     

-L  字元數  指的是精確的字元數目

[[email protected] liangli]# wc /etc/inittab

 26 149 884 /etc/inittab

[[email protected] liangli]# wc -l /etc/inittab     l表示總行數

26 /etc/inittab

[[email protected] liangli]# wc -w /etc/inittab     w表示單詞數

149 /etc/inittab

[[email protected] liangli]# wc -c /etc/inittab      c表示位元組數

[[email protected] liangli]# wc -L /etc/inittab      -L表示最長行的長度

[[email protected] liangli]# wc -m /etc/inittab      -m表示字元數

884 /etc/inittab

[[email protected] liangli]#

78 /etc/inittab

[[email protected] liangli]#

884 /etc/inittab

[[email protected] liangli]#

[[email protected] ~]# cat -n /etc/services | tail -1

 10774  iqobject        48619/udp               # iqobject

[[email protected] ~]# wc -l /etc/services

10774 /etc/services

[[email protected] ~]#

sshd服務有1,說明該服務是存活的

[[email protected] ~]# 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

[[email protected] ~]# ps -ef | grep "/sshd"  | grep -v grep

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

[[email protected] ~]# ps -ef | grep "/sshd"  | grep -v grep | wc -l

1

[[email protected] ~]#

[[email protected] ~]# 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          

[[email protected] ~]# netstat -lntup | grep sshd | wc -l

2

[[email protected] ~]#

3.13 iconv

轉換檔案的編碼格式

Linux系統是UTF-8的編碼,而Win7系統是GB2312的編碼,從英文的角度來講,二者沒有區別,但是Win編輯的中文字元到Linux系統中就會有亂碼,需要先轉碼在處理

使用-f引數指定檔案原來的編碼為gb2312 使用-t引數指定將要轉換的編碼utf-8

[[email protected] ~]# 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   刪除

[[email protected] liangli]# cat a.txt

1

2

3

4

5

6

[[email protected] liangli]# cat b.txt 

4

5

6

7

8

[[email protected] liangli]# diff a.txt b.txt

1,3d0

< 1

< 2

< 3

6a4,5

> 7

> 8

[[email protected] liangli]#

d/a前面的數字是文字1的行號,字母后面的文字2的行號,其中<打頭的行屬於檔案1,以>打頭的行屬於檔案2

[[email protected] liangli]# diff -y a.txt b.txt

1                                                             <

2                                                             <

3                                                             <

4                                                               4

5                                                               5

6                                                               6

                                                              > 7

                                                              > 8

[[email protected] liangli]#

[[email protected] liangli]# diff -y -W 30 a.txt b.txt        -W引數指定寬度

1             <

2             <

3             <

4               4

5               5

6               6

              > 7

              > 8

[ro[email protected] liangli]#

[[email protected] 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

[[email protected] liangli]#

[[email protected] 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

[[email protected] liangli]#

比較兩個目錄

[[email protected] 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

[[email protected] 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

[[email protected] liangli]#

3.16 vimdiff  

視覺化對比工具 後面可以接4個檔案進行同時對比

[[email protected] 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

反向讀取檔案內容

[[email protected] ~]# echo 123456|rev

654321

[[email protected] liangli]# cat a.txt

1 2 3 4 5 6 7 8 9 10

[[email protected] liangli]# rev a.txt

01 9 8 7 6 5 4 3 2 1

[[email protected] liangli]#

3.18 tr   

替換或刪除字元

-d 引數 刪除字元

-s 引數 將連續的字元壓縮成一個

-c 引數 取反的意思

[[email protected] ~]# cat person.txt

101,oldboy,CEO

102,zhangyao,CTO

103,Alex,COO

104,yy,CFO

105,feixue,CIO

011111111

0111111100

111

11111

1111111

[[email protected] ~]# 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

[[email protected] ~]#

[[email protected] ~]# 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

[[email protected] ~]#

[[email protected] ~]# 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

[[email protected] ~]#

-d  引數  刪除的功能    

[[email protected] ~]# cat person.txt

101,oldboy,CEO

102,zhangyao,CTO

103,Alex,COO

104,yy,CFO

105,feixue,CIO

011111111

0111111100

111

11111

1111111

[[email protected] ~]# 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

[[email protected] ~]#

凡是在檔案中出現的o l d b o y字元都會被刪除掉,而不是隻刪除oldboy字串

[[email protected] liangli]# cat a.txt           

oldboyoldbyonihaowoshiliang

[[email protected] liangli]# tr -d 'oldboy' < a.txt

nihawshiiang

[[email protected] liangli]#

 

也可以將換行符刪除掉

[[email protected] ~]# tr -d '\n'< person.txt    

101,oldboy,CEO102,zhangyao,CTO103,Alex,COO104,yy,CFO105,feixue,CIO0111111110111111100111111111111111[[email protected] ~]#

[[email protected] ~]# tr '\n' '='< person.txt      

101,oldboy,CEO=102,zhangyao,CTO=103,Alex,COO=104,yy,CFO=105,feixue,CIO=011111111=0111111100=111=11111=1111111=[[email protected] ~]#

 

-s引數將連續的字元壓縮成一個

[[email protected] liangli]# echo 'oooolllddbbboyyyyy'

oooolllddbbboyyyyy

[[email protected] liangli]# echo 'oooolllddbbboyyyyy' | tr -s oldboy

oldboy

[[email protected] liangli]#

3.19 od

用於輸出檔案的八進位制、十六進位制或者其他格式編碼的位元組 如od /bin/ls

3.20 tee

多重定向  比如 一邊把相應的結果輸入到螢幕 一邊把結果輸入到儲存的

檔案中

-a 引數 追加的意思

[[email protected] ~]# 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

[[email protected] ~]# 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

[[email protected] ~]#

[[email protected] ~]# 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

[[email protected] ~]#

-a 引數  追加的意思 不加引數-a 覆蓋/tmp/tee.txt檔案內容

[[email protected] ~]# wc -l /tmp/tee.txt

48 /tmp/tee.txt

[[email protected] ~]# 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

[[email protected] ~]# wc -l /tmp/tee.txt       

96 /tmp/tee.txt

[[email protected] ~]#

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           撤銷