日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据流重定向和管道命令, grep, tr,sort, wc, cut,split,tee,sleep(shell 02)

發布時間:2025/5/22 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据流重定向和管道命令, grep, tr,sort, wc, cut,split,tee,sleep(shell 02) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

主要內容

1.標準輸入輸出和錯誤

2.管道命令和 grep, tr,sort, wc, cut,split,tee,sleep

標準輸入輸出和錯誤

標準輸入(stdin) 是指令數據的輸入,代碼為0,使用<或者<<,默認是鍵盤
標準輸出(stdout)是指令執行成功返回的結果,代碼為1,使用>或者>>,默認由屏幕顯示
標準錯誤輸出(stderr)是指令執行失敗返回的錯誤信息,代碼為2,使用2>或者2>>,默認是屏幕

< 指定輸入的數據媒介來源
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 Code

2>&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等
能夠接收來自于前一個指令的數據成為stdin進行處理 只能處理正確的輸出,不能處理錯誤的輸出
ls -8|grep ls
處理不了
grep [-cinv] 'key' filename 支持正則
-c : 計算出現要查找字符的行數(是統計幾行含有匹配的字符,并不是出現的次數)

-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

?

?統計當前登錄用戶

[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 -A2 -n 'root' p.txt 1:root:x:0:0:root:/root:/bin/bash 2-bin:x:1:1:bin:/bin:/sbin/nologin 3-daemon:x:2:2:daemon:/sbin:/sbin/nologin -- 10:operator:x:11:0:operator:/root:/sbin/nologin 11-games:x:12:100:games:/usr/games:/sbin/nologin 12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin [root@bogon ~]# grep -B2 -n 'root' p.txt 1:root:x:0:0:root:/root:/bin/bash -- 8-halt:x:7:0:halt:/sbin:/sbin/halt 9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10:operator:x:11:0:operator:/root:/sbin/nologin [root@bogon ~]# grep -C2 -n 'root' p.txt 1:root:x:0:0:root:/root:/bin/bash 2-bin:x:1:1:bin:/bin:/sbin/nologin 3-daemon:x:2:2:daemon:/sbin:/sbin/nologin -- 8-halt:x:7:0:halt:/sbin:/sbin/halt 9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10:operator:x:11:0:operator:/root:/sbin/nologin 11-games:x:12:100:games:/usr/games:/sbin/nologin 12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin View Code

在目錄下遍歷查找下面的文件

[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 Code

grep中正則符號

* ?   *前 任意數量個字符
. ?   ?匹配單個任意字符(字母,數字,下劃線,空格)
?   ?號之前的字符 0個或者1個 匹配時得 grep -E 或者 egrep
+   +1個或者多個 匹配時得 grep -E 或者 egrep
.*   貪婪匹配任意個數量的任意字符
? ?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匹配指定個數的 比如匹配含有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 使用數字排序
-r 逆向排序
-t 分隔符標識 默認是tab
-k 以第幾列來排序

[root@localhost110 ~]# ls ab anaconda-ks.cfg a.php b.php composer.phar install.log install.log.syslog log mysql_listen.sh 公共的 模板 視頻 圖片 文檔 下載 音樂 桌面[root@localhost110 ~]# ls|sort ab anaconda-ks.cfg a.php b.php composer.phar install.log install.log.syslog log mysql_listen.sh [root@localhost110 ~]# ls|sort -r mysql_listen.sh log install.log.syslog install.log composer.phar b.php a.php anaconda-ks.cfg ab [root@localhost110 ~]# ls|sort ab anaconda-ks.cfg a.php A.php b.php B.php composer.phar D.php install.log install.log.syslog log mysql_listen.sh [root@bogon sh]# cat a.txt 11 10 0 8 7 2 1 4 5 3 6 9 2 2 3 3 [root@bogon sh]# cat a.txt |sort 0 1 10 11 2 2 2 3 3 3 4 5 6 7 8 9 [root@bogon sh]# cat a.txt |sort -n 0 1 2 2 2 3 3 3 4 5 6 7 8 9 10 11 [root@bogon sh]# cat a.txt |sort -nr 11 10 9 8 7 6 5 4 3 3 3 2 2 2 1 0 [root@bogon sh]# cat a.txt |sort -nr|uniq 11 10 9 8 7 6 5 4 3 2 1 0 [root@bogon sh]# cat a.txt |sort -nr|uniq -c 1 11 1 10 1 9 1 8 1 7 1 6 1 5 1 4 3 3 3 2 1 1 1 0 View Code

?默認不區分大小寫

按照文件大小寫來排序

[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'

[root@localhost110 ~]# ls -l|sort -k 6 總用量 1740 -rw-r--r--. 1 root root 10033 2016-01-13 17:42:57 install.log.syslog -rw-r--r--. 1 root root 46328 2016-01-13 17:48:20 install.log -rw-------. 1 root root 1416 2016-01-13 17:48:28 anaconda-ks.cfg drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 公共的 drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 模板 drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 視頻 drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 圖片 drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 文檔 drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 下載 drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 音樂 drwxr-xr-x. 2 root root 4096 2016-01-13 17:52:58 桌面 -rwxrwxrwx. 1 root root 272 2016-01-27 05:54:08 mysql_listen.sh -rwxr-xr-x. 1 root root 1640731 2016-06-07 09:40:58 composer.phar -rw-r--r--. 1 root root 42 2016-10-16 01:42:05 log -rw-r--r--. 1 root root 8 2016-10-16 01:59:03 a.php -rw-r--r--. 1 root root 143 2016-10-16 01:59:45 b.php -rw-r--r--. 1 root root 140 2016-10-16 02:02:19 ab -rw-r--r--. 1 root root 11 2016-10-16 02:06:07 A.php -rw-r--r--. 1 root root 6 2016-10-16 02:17:17 1 -rw-r--r--. 1 root root 6 2016-10-16 02:17:21 B.php -rw-r--r--. 1 root root 1112 2016-10-16 02:17:30 D.php View Code

wc指令
wc [-lwm] filename 統計功能
-l 統計行
-w 統計詞
-m 統計字符

[root@localhost110 ~]# wc -l log 8 log [root@localhost110 ~]# cat log|wc -l 8 [root@localhost110 ~]# cat log|wc -w 10 [root@localhost110 ~]# cat log -n 1 php 2 ajax 3 java 4 python 5 nginx mysql 6 GO p erlang 7 PHP5 8 [root@localhost110 ~]# cat log|wc -m 53 [root@localhost110 ~]# cat log1|wc -m 12 [root@localhost110 ~]# cat log1 -n 1 p h p 2 1 2 3 View Code [root@bogon sh]# cat 1.txt a b c [root@bogon sh]# wc 1.txt 2 3 6 1.txt 2行,3個單詞,6個字符 [root@bogon sh]# cat -A 1.txt a$ b c$ [root@bogon sh]# wc -l 1.txt|cut -d ' ' -f1 2 取出行數

結尾算一個字符

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 每個參數當一行 按照大小分割 [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 (休眠到后臺執行) Ctrl+c(殺死)
jobs 查看 任務列表
fg 編號 調到前臺
調到后臺后執行完畢在jobs里還有
fg到前臺 執行完 jobs里才沒有
fg 不加編號 優先回復 jobs里是+的任務

[root@bogon sh]# sleep 100 ^Z [1]+ 已停止 sleep 100 [root@bogon sh]# sleep 200 ^Z [2]+ 已停止 sleep 200 [root@bogon sh]# jobs [1]- 已停止 sleep 100 [2]+ 已停止 sleep 200 [root@bogon sh]# fg 1 sleep 100

單引號,雙引號,反引號

[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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。