標籤:shell 常用命令
《Linux Shell指令碼攻略》 筆記
第二章:常用命令1、cat cat -s //多個空白行壓縮成一個 cat *.txt | tr -s ‘\n‘ //移除空白行 cat -n //加行號2、find沿著檔案階層向下遍曆,匹配合格檔案,並執行相應的操作。eg:find ./ ! -name "*.txt" -print[[email protected] program_test]# find ./ -type f -name "*.swp" -delete
3、xargs 將標準輸入資料轉換成命令列參數。[[email protected] program_test]# cat out.txt | xargs //將單行轉化為多行
[[email protected] program_test]# cat out.txt | xargs -n 3 //將單行轉化為多行,每行的個數為3.
//統計.c和.cpp的檔案的程式碼數. xargs -0 將‘\0‘作為定界符.[[email protected] program_test]# find . -type f -name "*.c*" -print0 | xargs -0 wc -l
10 ./main/cos_value.c
10 ./main/sin_value.c
5 ./main/haha.c
15 ./main/main.c
8 ./hello.cpp
8 ./sin.c
32 ./review2.cpp
24 ./review5.cpp
7 ./hello.c
119 total
4.tr命令(translate的簡寫) 可對來自標準輸入的字元進行替換、刪除及壓縮。即:將一組字元變為另一組字元。1)替換echo “HELLO” | tr [A-Z] [a-z]2)刪除[[email protected] program_test]# echo "hello 123 world 456" | tr -d ‘0-9‘hello world 3)壓縮字元[[email protected] program_test]# echo "GNU is not UNIX" | tr -s ‘ ‘
GNU is not UNIX//綜合舉例 [[email protected] program_test]# cat sum.txt
1
2
3
4
5
[[email protected] program_test]# cat sum.txt | echo $[ $(tr ‘\n‘ ‘+‘) 0 ]
15
5、
md5校正[[email protected] program_test]#
md5sum out.txt > out.txt.md5
[[email protected] program_test]# cat out.txt.md5
fd46d559bf0c90170fef3da3c3de4c67 out.txt
//eg:[[email protected] program_test]# find ./ -type f -name "*.txt" -print0 | xargs -0 md5sum >> curr_dir.md546e17910faf509df48dbfba9729ef018 ./banana.txt
c1dbbf63209a5580c052dc557510e7fb ./11.txt
a80ddf02fa3a86c14066204e4bf2dbb9 ./multiline.txt
[[email protected] program_test]# md5sum -c curr_dir.md5
./banana.txt: OK
./11.txt: OK
./multiline.txt: OK
6、sort排序//sort 按第2列排序[[email protected] program_test]# sort -k 2 sort.txt
4 bad 5000
3 linux 50
1 mac 2000
2 winxp 100//sort 逆序排序
[[email protected] program_test]# sort -r sort.txt
4 bad 5000
3 linux 50
2 winxp 100
1 mac 2000
//綜合舉例:統計字串中每個字元出現的次數[[email protected] program_test]# ./total_cnts.sh
AHEBHAAA
4A1B1E2H
[[email protected] program_test]# cat total_cnts.sh
INPUT="AHEBHAAA"
output=$(echo $INPUT | sed ‘s/[^.]/&\n/g‘ | sed ‘/^$/d‘ | sort | uniq -c | tr -d ‘ \n‘)
echo $INPUT
echo $output
[釋義]: sed ‘s/[^.]/&\n/g‘ //任意一個字元後面都加上\n拆分如下:[[email protected] program_test]# input="ahebhaaa"
[[email protected] program_test]# echo $input | sed ‘s/[^.]/&\n/g‘
a
h
e
b
h
a
a
a
sed ‘/^$/d‘ //刪除空行uniq -c //統計各行文本出現的次數.tr -d ‘\n ‘ //刪除分行符號以及空格字元
7、臨時檔案命名[[email protected] program_test]# temp_file="/tmp/var.$$"
[[email protected] program_test]# echo $temp_file
/tmp/
var.16565
銘毅天下
轉載請標明出處,原文地址:http://blog.csdn.net/laoyang360/article/details/42364751
如果感覺本文對您有協助,請點擊‘頂’支援一下,您的支援是我堅持寫作最大的動力,謝謝!
《Linux Shell指令碼攻略》 筆記 第二章:常用命令