1. 程式人生 > >《鳥哥的Linux私房菜》之管道命令的使用

《鳥哥的Linux私房菜》之管道命令的使用

1、cut
    -d 按照其後面的字串分割
    -f 分割後取哪一個位置的分割項
    -c 按照字元分割後取哪一個位置到哪一個位置

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/soft/jdk1.8.0_144/bin:/root/soft/hadoop-2.7.2/bin:/root/soft/hadoop-2.7.2/sbin:/root/bin


echo $PATH | cut -d ':' -f2
/usr/local/bin
按照:分割後取第二項(shell的下標從1開始)


echo $PATH | cut -c 5-
/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/soft/jdk1.8.0_144/bin:/root/soft/hadoop-2.7.2/bin:/root/soft/hadoop-2.7.2/sbin:/root/bin
按照字元分割後取第5位及其後面的字元

echo $PATH | cut -c 5-10
/local
按照字元分割後取5,6,7,8,9,10位

2、grep
grep [-acinv] '' filename
-c 計算出待搜尋字串出現的次數
-i 忽略大小寫
-n 順便輸出行號
-v 反向選擇

    [
[email protected]
~]# cat 1.txt aaaaaaaaaaa AAAAAA bbbb bbb ccccccc ddddddddd eeeeeeeeee ffffffffffff ggggggggg GGGGGGGG [[email protected] ~]# grep -c 'a' 1.txt 1 [[email protected] ~]# grep -ic 'a' 1.txt 2 3、sort sort [-fbMnrtuk] [file or stdin] [
[email protected]
~]# cat 1.txt | sort -t ':' -k 2 ffffffffffff:1 AAAAAA:10000 bbbb:2 GGGGGGGG:22 bbb:33 ccccccc:4 ggggggggg:4 vvvv:7 eeeeeeeeee:9 ddddddddd:90 xxxx:98 aaaaaaaaaaa:99 [[email protected] ~]# cat 1.txt | sort -t ':' -k 2 -c [[email protected] ~]# [[email protected] ~]# cat 1.txt | sort -t ':' -k 2 -n ffffffffffff:1 bbbb:2 ccccccc:4 ggggggggg:4 vvvv:7 eeeeeeeeee:9 GGGGGGGG:22 bbb:33 ddddddddd:90 xxxx:98 aaaaaaaaaaa:99 AAAAAA:10000