数据流重定向和管道命令, grep, tr,sort, wc, cut,split,tee,sleep(shell 02)
主要內容
1.標準輸入輸出和錯誤
2.管道命令和 grep, tr,sort, wc, cut,split,tee,sleep
標準輸入輸出和錯誤
標準輸入(stdin) 是指令數(shù)據(jù)的輸入,代碼為0,使用<或者<<,默認是鍵盤
標準輸出(stdout)是指令執(zhí)行成功返回的結果,代碼為1,使用>或者>>,默認由屏幕顯示
標準錯誤輸出(stderr)是指令執(zhí)行失敗返回的錯誤信息,代碼為2,使用2>或者2>>,默認是屏幕
< 指定輸入的數(shù)據(jù)媒介來源
1> 將正確的內容 覆蓋輸出到指定的媒介
1>> 將正確的內容 追加到指定的媒介
2> 將錯誤信息覆蓋輸出到指定媒介
2>> 將錯誤信息追加輸出到指定媒介
?默認只能保存正確的
同時分類導出
[admin@localhost110 ~]$ rm -rf success fail result [admin@localhost110 ~]$ find /root a.txt 1>success 2>fail [admin@localhost110 ~]$ cat success /root a.txt [admin@localhost110 ~]$ cat fail find: “/root”: 權限不夠正確錯誤的均導入文件 [admin@localhost110 ~]$ find /root a.txt >result 2>&1 View Code2>&1 是標準錯誤拷貝了標準輸出的行為,也就是同樣被重定向到a.txt中,最終結果就是標準輸出和錯誤都被重定向到a.txt中
或者寫成 ?find /root a.txt 1>>a.txt ?2>a.txt
先寫錯誤,后寫正確的 所以 寫正確的時候要用追加?
?
[admin@localhost110 ~]$ cat result /root find: “/root”: 權限不夠 a.txt[admin@localhost110 ~]$ find /root a.txt >result1 2>&1 [admin@localhost110 ~]$ find /root a.txt >result find: “/root”: 權限不夠 [admin@localhost110 ~]$ find /root a.txt &>result2 View Code?
&表示[012]
grep [a-z] -n <a.txt
替換
小寫字母替換成大寫字母
tr '[a-z]' ?'[A-Z]'<a.txt >a1.txt
ls -al|tr ?'a-z' 'A-Z' 或者??ls -al|tr ?'[a-z]' '[A-Z]'
管道命令使用
命令通過管道符號|連接
能夠接收標準輸入(stdin),如tail/more/grep等
能夠接收來自于前一個指令的數(shù)據(jù)成為stdin進行處理 只能處理正確的輸出,不能處理錯誤的輸出
ls -8|grep ls
處理不了
grep [-cinv] 'key' filename 支持正則
-c : 計算出現(xiàn)要查找字符的行數(shù)(是統(tǒng)計幾行含有匹配的字符,并不是出現(xiàn)的次數(shù))
-o :找出所有的要查找字符的匹配,一個1行
-i: 忽略大小寫進行查找
-n: 輸出行號
-v: 顯示沒有該字符的行 取反
-s:不顯示不存在或無匹配文本的錯誤信息 忽略錯誤信息
-r: 遞歸遍歷查找(比如在目錄下遞歸查找文件內容)
-q:靜默模式,不輸出
?
[root@localhost110 ~]# cat log -n 1 php 2 ajax 3 java 4 python 5 nginx mysql 6 GO 7 PHP5 8 [root@localhost110 ~]# grep 'php' log php [root@localhost110 ~]# grep -n 'php' log 1:php [root@localhost110 ~]# grep -ni 'php' log 1:php 7:PHP5 [root@localhost110 ~]# grep -ci 'php' log 2 [root@localhost110 ~]# grep -inv 'php' log 2:ajax 3:java 4:python 5:nginx mysql 6:GO 8: [root@localhost110 ~]# grep -n [a-z] log 1:php 2:ajax 3:java 4:python 5:nginx mysql [root@localhost110 ~]# grep -nc [a-z] log 5 grep -n --color [a-z] log View Code?
?統(tǒng)計當前登錄用戶
[root@localhost110 ~]# w 01:53:08 up 18:15, 1 user, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.1.101 01:19 0.00s 0.24s 0.13s w [root@localhost110 ~]# w|grep -n root 3:root pts/0 192.168.1.101 01:19 0.00s 0.11s 0.00s w [root@localhost110 ~]# w|grep -nc root 1 不是root的 [root@localhost110 ~]# w|grep -v root 01:54:20 up 18:16, 1 user, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT View Code [root@centos1 ~]# cat 1.txt php 123php,php456 php [root@centos1 ~]# grep -o php 1.txt php php php php View Code [root@bogon sh]# echo 11|grep [0-9] 11 [root@bogon sh]# echo 11|grep [0-9] -q [root@bogon sh]# echo $? 0 [root@bogon sh]# echo abc|grep [0-9] [root@bogon sh]# echo $? 1 View Code目標字符的后n行
-A2 表示 目標字符的后2行也顯示出來 A表示after
-B2 表示 目標字符的前2行也顯示出來 B表示before
-C2 上下2行
在目錄下遍歷查找下面的文件
[root@bogon ~]# grep -r 'php' ./f ./f/t/1.txt:php5 ./f/t/1.txt:4php ./f/t/1.txt:js123php ./f/t/2.txt:php5 ./f/t/2.txt:4php ./f/t/2.txt:js123php ./f/a.php._mod:php ./f/a.php._mod:php2 ./f/a.php._mod:php4 View Codegrep中正則符號
* ? *前 任意數(shù)量個字符
. ? ?匹配單個任意字符(字母,數(shù)字,下劃線,空格)
? ?號之前的字符 0個或者1個 匹配時得 grep -E 或者 egrep
+ +1個或者多個 匹配時得 grep -E 或者 egrep
.* 貪婪匹配任意個數(shù)量的任意字符
? ?abc|ef 匹配 abc或者ef
?,+都得egrep 或者grep -E
?
[root@bogon ~]# cat 1.txt root rt rut roooooot [root@bogon ~]# grep 'ro*t' 1.txt root rt roooooot匹配指定個數(shù)的 比如匹配含有2個ou的 [root@bogon ~]# cat 1.txt rouououoooot rt rout rouout rouut [root@bogon ~]# grep 'r\(ou\)\{2\}' 1.txt rouououoooot rouout[root@bogon ~]# cat 1.txt rt rot root ro t [root@bogon ~]# grep 'ro?t' 1.txt [root@bogon ~]# grep -E 'ro?t' 1.txt rt rot [root@bogon ~]# egrep 'ro?t' 1.txt rt rot [root@bogon ~]# grep 'root|mysql' p.txt [root@bogon ~]# grep 'root\|mysql' p.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:#operator:/root:/sbin/nologin mysql:x:994:1003::/usr/local/mysql-5.6:/bin/bash [root@bogon ~]# grep -E 'mysql|root' p.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:#operator:/root:/sbin/nologin mysql:x:994:1003::/usr/local/mysql-5.6:/bin/bash [root@bogon ~]# egrep -E 'mysql|root' p.txt root:x:0:0:root:/root:/bin/bash operator:x:11:0:#operator:/root:/sbin/nologin mysql:x:994:1003::/usr/local/mysql-5.6:/bin/bash View Code?sort指令
sort [-fbknrtu] filename
-f 忽略大小寫
-b 忽略最前面的空格
-M 以月份英文字母排序
-n 使用數(shù)字排序
-r 逆向排序
-t 分隔符標識 默認是tab
-k 以第幾列來排序
?默認不區(qū)分大小寫
按照文件大小寫來排序
[root@localhost110 ~]# ls -l|sort -t ' ' -k 5 -n 總用量 1740 -rw-r--r--. 1 root root 6 10月 16 02:17 1 -rw-r--r--. 1 root root 6 10月 16 02:17 B.php -rw-r--r--. 1 root root 8 10月 16 01:59 a.php -rw-r--r--. 1 root root 11 10月 16 02:06 A.php -rw-r--r--. 1 root root 42 10月 16 01:42 log -rw-r--r--. 1 root root 140 10月 16 02:02 ab -rw-r--r--. 1 root root 143 10月 16 01:59 b.php -rwxrwxrwx. 1 root root 272 1月 27 2016 mysql_listen.sh -rw-r--r--. 1 root root 1112 10月 16 02:17 D.php -rw-------. 1 root root 1416 1月 13 2016 anaconda-ks.cfg drwxr-xr-x. 2 root root 4096 1月 13 2016 公共的 drwxr-xr-x. 2 root root 4096 1月 13 2016 模板 drwxr-xr-x. 2 root root 4096 1月 13 2016 視頻 drwxr-xr-x. 2 root root 4096 1月 13 2016 圖片 drwxr-xr-x. 2 root root 4096 1月 13 2016 文檔 drwxr-xr-x. 2 root root 4096 1月 13 2016 下載 drwxr-xr-x. 2 root root 4096 1月 13 2016 音樂 drwxr-xr-x. 2 root root 4096 1月 13 2016 桌面 -rw-r--r--. 1 root root 10033 1月 13 2016 install.log.syslog -rw-r--r--. 1 root root 46328 1月 13 2016 install.log -rwxr-xr-x. 1 root root 1640731 6月 7 09:40 composer.phar View Code設置顯示方式
export TIME_STYLE='+%Y-%m-%d %H:%M:%S'
wc指令
wc [-lwm] filename 統(tǒng)計功能
-l 統(tǒng)計行
-w 統(tǒng)計詞
-m 統(tǒng)計字符
結尾算一個字符
tee與>
[root@bogon sh]# cat 1.txt a b c [root@bogon sh]# cat 1.txt|tee 11.txt a b c [root@bogon sh]# cat 1.txt>111.txt [root@bogon sh]# cat 11.txt a b c [root@bogon sh]# cat 111.txt a b c?tee重定向并輸出
> 重定向 但影響正常輸出了
tee到的內容 也是只有正確的內容寫入
文件分割 split
[root@bogon ~]# seq 1 5 1 2 3 4 5[root@bogon test]# for i in `seq 1 6000`;do cat /etc/passwd >>1.log;done; [root@bogon test]# du -sh 1.log 11M 1.log [root@bogon test]# wc -l 1.log 228000 1.log?按照大小和按照行分割
1.按照行分割
split -l 10000 1.log [root@bogon test]# ls |xargs wc -l228000 1.log10000 xaa10000 xab10000 xac10000 xad10000 xae10000 xaf10000 xag10000 xah10000 xai10000 xaj10000 xak10000 xal10000 xam10000 xan10000 xao10000 xap10000 xaq10000 xar10000 xas10000 xat10000 xau10000 xav8000 xaw自定義命名 split -l 10000 1.log _ [root@bogon test]# ls 1.log _aa _ab _ac _ad _ae _af _ag _ah _ai _aj _ak _al _am _an _ao _ap _aq _ar _as _at _au _av _aw [root@bogon test]# ls _*|xargs -i mv {} {}.txt [root@bogon test]# ls 1.log _ab.txt _ad.txt _af.txt _ah.txt _aj.txt _al.txt _an.txt _ap.txt _ar.txt _at.txt _av.txt _aa.txt _ac.txt _ae.txt _ag.txt _ai.txt _ak.txt _am.txt _ao.txt _aq.txt _as.txt _au.txt _aw.txt-i 每個參數(shù)當一行 按照大小分割 [root@bogon test]# split -b 1M 1.log [root@bogon test]# du -sh * 11M 1.log 1.0M xaa 1.0M xab 1.0M xac 1.0M xad 1.0M xae 1.0M xaf 1.0M xag 1.0M xah 1.0M xai 1.0M xaj 648K xak View Code?字串截取 cut
cut -d ':' -f 1 ./a
cut -d ':' -f 1,3,4 ./a
cut -d ':' -f 1-3 ./a
cut -c 2-5 ./a
cut -c 1 ./a
cut -c 1,3 ./a
[root@bogon ~]# cat a a:b:c:d:e:f a1:b1:c1:d1:e1:f1 a2:b2:c2:d2:d2:f2 a3:b3:c3:d3:e3:f3 [root@bogon ~]# cut -d ':' -f 1 ./a a a1 a2 a3 [root@bogon ~]# cut -d ':' -f 1,3,4 ./a a:c:d a1:c1:d1 a2:c2:d2 a3:c3:d3 [root@bogon ~]# cut -d ':' -f 1-3 ./a a:b:c a1:b1:c1 a2:b2:c2 a3:b3:c3 [root@bogon ~]# cut -c 2-5 ./a :b:c 1:b1 2:b2 3:b3 [root@bogon ~]# cut -c 1 ./a a a a a [root@bogon ~]# cut -c 1,3 ./a ab a: a: a: View Code殺掉所有php的進程
ps -ef|grep php|grep -v grep|cut -c 9-15|xargs kill -9??
sleep?
Ctrl+z (休眠到后臺執(zhí)行) Ctrl+c(殺死)
jobs 查看 任務列表
fg 編號 調到前臺
調到后臺后執(zhí)行完畢在jobs里還有
fg到前臺 執(zhí)行完 jobs里才沒有
fg 不加編號 優(yōu)先回復 jobs里是+的任務
單引號,雙引號,反引號
[root@bogon sh]# echo $a modify_suffix.sh.tar.gz [root@bogon sh]# b="echo $a" [root@bogon sh]# echo $b echo modify_suffix.sh.tar.gz [root@bogon sh]# b=`echo $a` [root@bogon sh]# echo $b modify_suffix.sh.tar.gz View Code.=source 加載配置文件
. /etc/profile
source /etc/profile
還有 join/paste等
?
轉載于:https://www.cnblogs.com/HKUI/p/5989809.html
總結
以上是生活随笔為你收集整理的数据流重定向和管道命令, grep, tr,sort, wc, cut,split,tee,sleep(shell 02)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android-4
- 下一篇: 笔记(用Python做些事情)--变量(