第三章 文件過濾及內容編輯處理命令

来源:https://www.cnblogs.com/key-oldboy/archive/2018/09/30/9733397.html
-Advertisement-
Play Games

3.1 cat cat命令作用: 1、查看文件內容 2、把多個文件合併成一個文件 3、編輯文件內容 4、結合>>和<<EOF進行編輯 5、清空文件內容 -n參數 從1開始對所有輸出的內容按行編號 -b參數 忽略顯示空白行行號 -E參數 在每一行行尾顯示$符號(即使是空行 結尾也是有結束標識的) -s ...


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


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 現在是北京時間2018/9/29 21:25:05 我在加班,寫一個記錄,開啟我的新生活! ...
  • 按照的是中文的visual studio,用起來很不方便,因為程式員的都是英文版,平時交流時也是英文的名字 轉換語言時發現只有中文和跟隨windows系統的設置 官方給的文檔看的不是很清楚 查閱資料後總結下步驟: 1、進入到設置--選項--區域設置,發現沒有英文語言 2、進入工具--獲取工具和功能 ...
  • Steeltoe里的分散式追蹤功能與 "Spring Cloud Sleuth" 一樣,支持在日誌中記錄追蹤數據,或者上傳到遠端的服務,比如Zipkin。 Logging 在Steeltoe中使用日誌時需要引入其特有的日誌包 。 之後還需在應用程式啟動時加入日誌提供器。 接下來,引入追蹤包 。 然後 ...
  • .Net平臺下相容.NET Standard 2.0,一個實現以Lambda表達式轉轉換標準SQL語句,使用強類型操作數據的輕量級ORM工具,在減少魔法字串同時,通過靈活的Lambda表達式組合,實現業務數據查詢的多樣性。 ...
  • 0.簡介 Abp 框架為我們自帶了審計日誌功能,審計日誌可以方便地查看每次請求介面所耗的時間,能夠幫助我們快速定位到某些性能有問題的介面。除此之外,審計日誌信息還包含有每次調用介面時客戶端請求的參數信息,客戶端的 IP 與客戶端使用的瀏覽器。有了這些數據之後,我們就可以很方便地復現介面產生 BUG ...
  • 一、ASPX 登陸界面驗證碼 1、登陸驗證碼圖片和輸入驗證碼框 2、js $(function () { $("#loginBtn").click(function () { var Pwd = $("#PwdTbx").val(); var md5pwd = $.md5(Pwd); $("#Pwd ...
  • 1.字體顏色 1.1顏色及對應數字 顏色表 前景 背景 顏色 30 40 黑色 31 41 紅色 32 42 綠色 33 43 黃色 34 44 藍色 35 45 紫紅色 36 46 青藍色 37 47 白色 1.2 其他 0 OFF 1高亮顯示 4 underline 7 反白顯示 8 不可見 1 ...
  • kubernetes(通常簡稱為K8S),是一個用於管理在容器中運行的應用的容器編排工具。 Kubernetes不僅有你所需要的用來支持複雜容器應用的所有東西,它還是市面上最方便開發和運維的框架。 Kubernetes的工作原理是通過將容器分組來把一個應用程式拆分成多個邏輯單元,以方便管理和發現。它 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...