81437716 2020-09-13
èæ¯
æ¬æ主è¦æ¥æºäºå¨ä¹åå¬å¸çå°ç»åé¨çä¸ä¸ªå°å享ï¼æ´çæä¸ç¯æç« poåºæ¥ãé¢ç®å« “Shell å©åå¼åæçæå”ï¼æ´åé¢çåºè¯¥æ¯å«“å½ä»¤è¡”æåå¼åæçï¼è¿é并没æè®²å° Shell ç¼ç¨ï¼èæ¯ä¸»è¦ä»ç» Linux æè Mac ä¸å¸¸ç¨çä¸äºåºæ¬å·¥å·å½ä»¤æ¥å¸®å©å¤çä¸äºæ¥å¸¸äºå¡ã
éè¿æ¬æçä»ç»ï¼ä½ åºè¯¥å¯¹ç¸å³å½ä»¤æä¸ä¸ªåæ¥çäºè§£ï¼ç¥éæ¯å¦ç¨ä»ä¹å½ä»¤å¯ä»¥å®æææ ·çæä½ï¼ è³äºå·ä½çåæ°ï¼ä¸éè¦å»æå°è诵ï¼çå°éè¦ç¨å°çæ¶åï¼åå» cmd --help æè man cmdï¼ç¨å¾å¤äºï¼å¸¸ç¨çå½ä»¤ä¹å°±èªç¶è®°ä½äºã
æ¬æé¦åä»ç»äº Linux/Mac ä¸ä¸äºå¸¸ç¨çå½ä»¤è¡å·¥å·ï¼ç¶åç¨å·ä½ç示ä¾éè¿°äºå¸¸ç¨çå½ä»¤ç¨æ³ï¼æåéè¿ä¸ä¸¤ä¸ªæ¡ä¾æ¥è¯´æè¿äºå·¥å·ç强大ä¹å¤ï¼
Mac ç¯å¢
Shell åºç¡å½ä»¤
which/whereisï¼ å¸¸ç¨ whatisï¼ manï¼ --help
➜ .oh-my-zsh git:(master)$ whereis ls /bin/ls➜ .oh-my-zsh git:(master)$ which ls ls: aliased to ls -G
åºæ¬æ件ç®å½æä½
rmï¼ mkdirï¼ mvï¼ cpï¼ cdï¼ lsï¼ lnï¼ fileï¼ statï¼ wc(-l/w/c)ï¼ headï¼ moreï¼ tailï¼ cat...
å©å¨ 管é: |
Shell ææ¬å¤ç
è¿éå°±æ¯éè¿æ¡ä¾è®²äºä¸ä¸12个å½ä»¤ç大è´ç¨æ³ååæ°ï¼å¯ä»¥éè¿ç¹å»å³è¾¹çç®å½(æå客æç®å½ï¼å¬ä¼å·ä¸æ¨æ)ç´è¾¾ä½ æ³è¦äºè§£çå½ä»¤ã
find, grep, xargs, cut, paste, comm join, sort, uniq, tr, sed, awk
find
示ä¾
find ./ -name "*.json" find . -maxdepth 7 -name "*.json" -type f find . -name "*.log.gz" -ctime +7 -size +1M -delete (atime/ctime/mtime) find . -name "*.scala" -atime -7 -exec du -h {} \;
grep
示ä¾
grep 'partner' ./*.scala -l grep -e 'World' -e 'first' -i -R ./ (-e: or)
ç¸å³å½ä»¤: grep -z / zgrep / zcat xx | grep
xargs
示ä¾
echo "helloworldhellp" | cut -c1-10 cut -dï¼ -f2-8 csu.db.export.csv
cut
示ä¾
echo "helloworldhellp" | cut -c1-10cut -dï¼ -f2-8 csu.db.export.csv
paste
示ä¾
➜ Documents$ cat file1 1 11 2 22 3 33 4 44 ➜ Documents$ cat file2 one 1 two 2 three 3 one1 4 ➜ Documents$ paste -d, file1 file2 1 11, one 1 2 22, two 2 3 33, three 3 4 44, one1 4 ➜ Documents$ paste -s -d: file1 file2 a 11:b bb:3 33:4 44 one 1:two 2:three 3:one1 4
join
类似sqlä¸ç ...inner join ...on ...ï¼ -t åé符ï¼é»è®¤ä¸ºç©ºæ ¼ætab
➜ Documents$ cat j1 1 11 2 22 3 33 4 44 5 55 ➜ Documents$ cat j2 one 1 0 one 2 1 two 4 2 three 5 3 one1 5 4 ➜ Documents$ join -1 1 -2 3 j1 j2 1 11 one 2 2 22 two 4 3 33 three 5 4 44 one1 5
comm
示ä¾
➜ Documents$ seq 1 5 >file11 ➜ Documents$ seq 2 6 >file22 ➜ Documents$ cat file11 1 2 3 4 5 ➜ Documents$ cat file22 2 3 4 5 6 ➜ Documents$ comm file11 file22 1 2 3 4 5 6 ➜ Documents$ comm -1 file11 file22 2 3 4 5 6 ➜ Documents$ comm -2 file11 file22 1 2 3 4 5 ➜ Documents$ comm -23 file11 file22 1
ç¸å³å½ä»¤ diff(类似git diff)
sort
示ä¾
➜ Documents$ cat file2 one 1 two 2 three 3 one1 4 ➜ Documents$ sort file2one 1 one1 4 three 3 two 2 ➜ Documents$ sort -b -k2 -r file2one1 4 three 3 two 2 one 1
uniq
示ä¾
➜ Documents$ cat file4 11 22 33 11 11 ➜ Documents$ sort file4 | uniq -c 3 11 1 22 1 33 ➜ Documents$ sort file4 | uniq -d 11 ➜ Documents$ sort file4 | uniq -u 22 33 ➜ Documents$ cat file3 one 1 two 1 three 3 one1 4 ➜ Documents$ uniq -c -f 1 file3 2 one 1 1 three 3 1 one1 4
注æï¼uniqæ¯è¾ç¸é»çæ¯å¦éå¤ï¼ä¸è¬ä¸sortèç¨
tr
示ä¾
➜ Documents$ echo '1111234444533hello' | tr '[1-3]' '[a-c]' aaaabc44445cchello➜ Documents$ echo '1111234444533hello' | tr -d '[1-3]' 44445hello➜ Documents$ echo '1111234444533hello' | tr -dc '[1-3]' 11112333➜ Documents$ echo '1111234444533hello' | tr -s '[0-9]' 123453hello➜ Documents$ echo 'helloworld' | tr '[:lower:]' '[:upper:]' HELLOWORLD
sed
示ä¾
➜ Documents$ cat file2 one 1 two 2 three 3 one1 4 ➜ Documents$ sed "2,3d" file2 one 1 one1 4 ➜ Documents$ sed '/one/d' file2 two 2 three 3 ➜ Documents$ sed 's/one/111/g' file2 111 1 two 2 three 3 1111 4 #å°oneæ¿æ¢æ111 并å°å«ætwoçè¡å é¤ ➜ Documents$ sed -e 's/one/111/g' -e '/two/d' file2 111 1 three 3 1111 4 # ()æ è®°(转ä¹), \1 å¼ç¨ ➜ Documents$ sed 's/\([0-9]\)/\1.html/g' file2 one 1.html two 2.html three 3.html one1.html 4.html # ä¸ä¸é¢ä¸æ · & æ è®°å¹éçå符➜ Documents$ sed 's/[0-9]/&.html/g' file2 one 1.html two 2.html three 3.html one1.html 4.html ➜ Documents$ cat mobile.csv"13090246026" "18020278026" "18520261021" "13110221022" ➜ Documents$ sed 's/\([0-9]\{3\}\)[0-9]\{4\}/\1xxxx/g' mobile.csv "130xxxx6026" "180xxxx8026" "185xxxx1021" "131xxxx1022"
awk
è¯æ³: awk 'BEGIN{ commands } pattern{ commands } END{ commands }'ï¼ æµç¨å¦ä¸:
示ä¾
➜ Documents$ cat file5 11 11 aa cc 22 22 bb 33 33 d 11 11 11 11 #è¡å·ï¼ åæ°éï¼ ç¬¬3å ➜ Documents$ awk '{print NR"("NF"):"ï¼ $3}' file5 1(4): aa 2(3): bb 3(3): d 4(2): 5(2): #å符串åå²ï¼ æå°1ï¼2å ➜ Documents$ awk -F"xxxx" '{print $1ï¼ $2}' mobile.csv "130 6026" "180 8026" "185 1021" "131 1022" #æ·»å 表达å¼➜ Documents$ awk '$1>=22 {print NR":"ï¼ $3}' file5 2: bb3: d#ç´¯å 1å°36ï¼å¥æ°ï¼å¶æ° ➜ Documents$ seq 36 | awk 'BEGIN{sum=0; print "question:"} {print $1" +"; sum+=$1} END{print "="; print sum}' | xargs | sed 's/+ =/=/' question: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 19 + 20 + 21 + 22 + 23 + 24 + 25 + 26 + 27 + 28 + 29 + 30 + 31 + 32 + 33 + 34 + 35 + 36 = 666 ➜ Documents$ seq 36 | awk 'BEGIN{sum=0; print "question:"} $1 % 2 ==1 {print $1" +"; sum+=$1} END{print "="; print sum}' | xargs | sed 's/+ =/=/' question: 1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 + 21 + 23 + 25 + 27 + 29 + 31 + 33 + 35 = 324 ➜ Documents$ seq 36 | awk 'BEGIN{sum=0; print "question:"} $1 % 2 !=1 {print $1" +"; sum+=$1} END{print "="; print sum}' | xargs | sed 's/+ =/=/' question: 2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20 + 22 + 24 + 26 + 28 + 30 + 32 + 34 + 36 = 342
å¶ä»é«çº§è¯æ³ï¼for, while çï¼ åç§å½æ°çï¼æ¬èº«awkæ¯ä¸ä¸ªå¼ºå¤§çè¯è¨ï¼å¯ä»¥ææ¡ä¸äºåºæ¬çç¨æ³ã
å®éåºç¨
æ¥å¿ç»è®¡åæ
ä¾å¦æ¿å°ä¸ä¸ªnginxæ¥å¿æ件ï¼å¯ä»¥åå¾å¤äºæï¼æ¯å¦çåªäºè¯·æ±æ¯èæ¶æä¹çè¿èè¿è¡ä¼åï¼æ¯å¦çæ¯å°æ¶ç"PV"æ° ççã
➜ Documents$ head -n5 std.nginx.log 106.38.187.225 - - [20/Feb/2017:03:31:01 +0800] www.tanglei.name "GET /baike/208344.html HTTP/1.0" 301 486 "-" "Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322) 360JK yunjiankong 975382" "106.38.187.225, 106.38.187.225" - 0.000 106.38.187.225 - - [20/Feb/2017:03:31:02 +0800] www.tanglei.name "GET /baike/208344.html HTTP/1.0" 301 486 "-" "Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322) 360JK yunjiankong 975382" "106.38.187.225, 106.38.187.225" - 0.000 10.130.64.143 - - [20/Feb/2017:03:31:02 +0800] stdbaike.bdp.cc "POST /baike/wp-cron.php?doing_wp_cron=1487532662.2058920860290527343750 HTTP/1.1" 200 182 "-" "WordPress/4.5.6; http://www.tanglei.name/baike" "10.130.64.143" 0.205 0.205 10.130.64.143 - - [20/Feb/2017:03:31:02 +0800] www.tanglei.name "GET /external/api/login-status HTTP/1.0" 200 478 "-" "-" "10.130.64.143" 0.003 0.004 10.130.64.143 - - [20/Feb/2017:03:31:02 +0800] www.tanglei.name "GET /content_util/authorcontents?count=5&offset=0&israndom=1&author=9 HTTP/1.0" 200 11972 "-" "-" "10.130.64.143" 0.013 0.013
ä¸é¢æ¯nginxçä¸ä¸ªæ¡ä¾ï¼ ä¾å¦å¸ææ¾å°top 10 请æ±çpath:
head -n 10000 std.nginx.log | awk '{print $8 ", " $10}' | grep ',404' | sort | uniq -c | sort -nr -k1 | head -n 10 #orhead -n 10000 std.nginx.log | awk '$10==404 {print $8}' |sort | uniq -c | sort -nr -k1 | head -n 10
å½ç¶ï¼ä½ å¯è½ä¸æ¬¡ä¸ä¼ç´æ¥å¤çæåï¼ä¸è¬ä¼åå°æ¿ä¸é¨åæ°æ®è¿è¡å¤ççé»è¾æ¯å¦æ£å¸¸ï¼ æèä½ å¯ä»¥ç¼åä¸äºä¸é´ç»æ.
cat std.nginx.log | awk '{print $8 "," $10}' | grep ',404' >404.log sort 404.log | uniq -c | sort -nr -k1 | head -n 10
åæ¯å¦æ¯å°æ¶è¯·æ±æ°éï¼è¯·æ±èæ¶çç
➜ Documents$ head -n 100000 std.nginx.log | awk -F: '{print $1 $2}' | cut -f3 -d/ | uniq -c 8237 201703 15051 201704 16083 201705 18561 201706 22723 201707 19345 201708
å¶ä»å®éæ¡ä¾ ip block
æ¡ä¾: dbæ°æ®è®¢æ£
èæ¯: å 为ææå¡bugï¼å¯¼è´æå¥å°dbçå¾çè·¯å¾ä¸å¯¹ï¼éè¦å°å½¢å¦(å®å¨éè¦å·²ç»å°æææ°æ®æ¿æ¢) https://www.tanglei.name/upload/photos/129630//internal-public/shangtongdai/2017-02-19-abcdefg-eb85-4c24-883e-hijklmn.jpg æ¿æ¢æ http://www.tanglei.me/internal-public/shangtongdai/2017-02-19-abcdefg-eb85-4c24-883e-hijklmn.jpgï¼å 为mysqlçdbè²ä¼¼ä¸æ¯æç´æ¥æ£åçæ¿æ¢ï¼æ以ä¸è½å¤å¾æ¹ä¾¿çè¿è¡åsqlè¿è¡æ¿æ¢(å°±ç®æ¯æï¼ç´æ¥æ¹ä¹æé£é©çï¼è¿æ¯åå¤ä»½åä¿®æ¹ç个“åæ诔)ã
å½ç¶å°æ°æ®å¯¼åºï¼ç¶åå python çèæ¬å¤çä¹æ¯ä¸ç§è§£å³æ¹æ¡ï¼ä½å¦æç¨ä¸é¢çå½ä»¤è¡å¤çï¼åªéè¦å åç§å³å¯å®æã
æ¥éª¤:
select id, photo_url_1, photo_url_2, photo_url_3 from somedb.sometable where photo_url_1 like 'https://www.tanglei.name/upload/photos/%//internal-public/%' or photo_url_2 like 'https://www.tanglei.name/upload/photos/%//internal-public/%' or photo_url_3 like 'https://www.tanglei.name/upload/photos/%//internal-public/%';
#æµè¯æ¯å¦OK head -n 5 customers.csv | sed 's|https://www.tanglei.name/upload/photos/[0-9]\{1,\}/|http://www.tanglei.me|g' # ç´æ¥æ¿æ¢åæä»¶ï¼ å¯ä»¥sed -i ".bak" æ¿æ¢æ¶ä¿çåå§å¤ä»½æ件 sed -i "" 's|https://www.tanglei.name/upload/photos/[0-9]\{1,\}/|http://www.tanglei.me|g' customers.csv
awk -Fï¼ '{print "update sometable set photo_url_1 = " $2, ", photo_url_2 = " $3, ", photo_url_3 = " $4, " where id = " $1 ";" }' customers.csv > customer.sql #ç¶åæ§è¡sql å³å¯
å¶ä»
èæ¹å¼: éè¦å¯playç¯å¢ï¼æ¢ãæ°æ¹å¼ç´æ¥å½ä»¤è¡è§£å³ã
sbt "project site" consoleQuick import play.api.libs._val sec = "secret...secret" var uid = "10086" Crypto.sign(s"uid=$uid"ï¼ sec.getBytes("UTF-8")) + s"-uid=$uid" ➜ Documents$ ~/stdcookie.sh 97522 918xxxxdf64abcfcxxxxc465xx7554dxxxx21e-uid=97522 ➜ Documents$ cat ~/stdcookie.sh#!/bin/bash ## cannot remove this line uid=$1 hash=`echo -n "uid=$uid" | openssl dgst -sha1 -hmac "secret...secret"` echo "$hash-uid=$uid"
ç»è®¡æç« åè¯é¢ç: ä¸é¢æ¡ä¾ç»è®¡äºå·æ®å°±èæ¼è®²åæä¸è¯é¢æé«ç10个è¯ã
➜ Documents$ head -n3 chuanpu.txt Chief Justice Robertsï¼ President Carterï¼ President Clintonï¼ President Bushï¼ President Obamaï¼ fellow Americans and people of the worldï¼ thank you. Weï¼ the citizens of Americaï¼ are now joined in a great national effort to rebuild our country and restore its promise for all of our people. Together we will determine the course of America and the world for manyï¼ many years to come. ➜ Documents$ cat chuanpu.txt | tr -dc 'a-zA-Z ' | xargs -n 1 | sort | uniq -c | sort -nr -k1 | head -n 20 65 the 63 and 48 of 46 our 42 will 37 to 21 We 20 is 18 we 17 America 15 a 14 all 13 in 13 for 13 be 13 are 10 your 10 not 10 And 10 American