{"id":10285,"date":"2022-08-19T10:02:51","date_gmt":"2022-08-19T02:02:51","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=10285"},"modified":"2022-08-19T17:05:37","modified_gmt":"2022-08-19T09:05:37","slug":"linux-shell-text-commonly-tools","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-shell-text-commonly-tools.html","title":{"rendered":"Linux Shell\u5904\u7406\u6587\u672c\u6700\u5e38\u7528\u7684\u5de5\u5177\u5927\u76d8\u70b9"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u6587\u5c06\u4ecb\u7ecdLinux\u4e0b\u4f7f\u7528Shell\u5904\u7406\u6587\u672c\u65f6\u6700\u5e38\u7528\u7684\u5de5\u5177\uff1afind\u3001grep\u3001xargs\u3001sort\u3001uniq\u3001tr\u3001cut\u3001paste\u3001wc\u3001sed\u3001awk\uff1b\u63d0\u4f9b\u7684\u4f8b\u5b50\u548c\u53c2\u6570\u90fd\u662f\u6700\u5e38\u7528\u548c\u6700\u4e3a\u5b9e\u7528\u7684\uff0c\u6211\u5bf9shell\u811a\u672c\u4f7f\u7528\u7684\u539f\u5219\u662f\u547d\u4ee4\u5355\u884c\u4e66\u5199\uff0c\u5c3d\u91cf\u4e0d\u8981\u8d85\u8fc72\u884c\uff1b\u5982\u679c\u6709\u66f4\u4e3a\u590d\u6742\u7684\u4efb\u52a1\u9700\u6c42\uff0c\u8fd8\u662f\u8003\u8651python\u5427\uff01<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
find\u6587\u4ef6\u67e5\u627e<\/strong><\/div>\n

\u67e5\u627etxt\u548cpdf\u6587\u4ef6<\/strong>
\n\u627etxt\u548cpdf\u6587\u4ef6<\/p>\n

find . \\( -name \"*.txt\" -o -name \"*.pdf\" \\) -print<\/pre>\n

\u6b63\u5219\u65b9\u5f0f\u67e5\u627e.txt\u548cpdf<\/p>\n

find . -iregex  \".*\\(\\.txt|\\.pdf\\)___FCKpd___1quot;    #-iregex\uff1a\u5ffd\u7565\u5927\u5c0f\u5199\u7684\u6b63\u5219<\/span><\/pre>\n

\u5426\u5b9a\u53c2\u6570,\u67e5\u627e\u6240\u6709\u975etxt\u6587\u672c<\/p>\n

find . ! -name \"*.txt\" -print<\/pre>\n

\u6307\u5b9a\u641c\u7d22\u6df1\u5ea6<\/strong>
\n\u6253\u5370\u51fa\u5f53\u524d\u76ee\u5f55\u7684\u6587\u4ef6\uff08\u6df1\u5ea6\u4e3a1\uff09<\/p>\n

find . -maxdepth 1 -type f<\/pre>\n

\u5b9a\u5236\u641c\u7d22<\/strong>
\n\u6309\u7c7b\u578b\u641c\u7d22\uff1a
\n-type f\u6587\u4ef6\/l\u7b26\u53f7\u94fe\u63a5<\/p>\n

find . -type d -print     \/\/\u53ea\u5217\u51fa\u6240\u6709\u76ee\u5f55<\/span><\/pre>\n

\u6309\u65f6\u95f4\u641c\u7d22\uff1a
\n-atime \u8bbf\u95ee\u65f6\u95f4 \uff08\u5355\u4f4d\u662f\u5929\uff0c\u5206\u949f\u5355\u4f4d\u5219\u662f-amin\uff0c\u4ee5\u4e0b\u7c7b\u4f3c\uff09
\n-mtime \u4fee\u6539\u65f6\u95f4 \uff08\u5185\u5bb9\u88ab\u4fee\u6539\uff09
\n-ctime \u53d8\u5316\u65f6\u95f4 \uff08\u5143\u6570\u636e\u6216\u6743\u9650\u53d8\u5316\uff09<\/p>\n

\u6700\u8fd17\u5929\u88ab\u8bbf\u95ee\u8fc7\u7684\u6240\u6709\u6587\u4ef6\uff1a<\/p>\n

find . -atime 7 -type f -print<\/pre>\n

\u6309\u5927\u5c0f\u641c\u7d22\uff1a
\nw\u5b57 k M G\uff0c\u5bfb\u627e\u5927\u4e8e2k\u7684\u6587\u4ef6<\/p>\n

find . -type f -size +2k<\/pre>\n

\u6309\u6743\u9650\u67e5\u627e\uff1a<\/p>\n

find . -type f -perm 644 -print      \/\/\u627e\u5177\u6709\u53ef\u6267\u884c\u6743\u9650\u7684\u6240\u6709\u6587\u4ef6<\/span><\/pre>\n

\u6309\u7528\u6237\u67e5\u627e\uff1a<\/p>\n

find . -type f -user weber -print    \/\/ \u627e\u7528\u6237weber\u6240\u62e5\u6709\u7684\u6587\u4ef6<\/span><\/pre>\n

\u627e\u5230\u540e\u7684\u540e\u7eed\u52a8\u4f5c<\/strong>
\n\u5220\u9664\u5f53\u524d\u76ee\u5f55\u4e0b\u6240\u6709\u7684swp\u6587\u4ef6\uff1a<\/p>\n

find . -type f -name \"*.swp\" -delete<\/pre>\n

\u6267\u884c\u52a8\u4f5c\uff08\u5f3a\u5927\u7684exec\uff09<\/p>\n

find . -type f -user root -exec chown weber {} \\;   \/\/\u5c06\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6743\u53d8\u66f4\u4e3aweber<\/span><\/pre>\n

\u6ce8\uff1a{}\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u5b57\u7b26\u4e32\uff0c\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u5339\u914d\u7684\u6587\u4ef6\uff0c{}\u4f1a\u88ab\u66ff\u6362\u6210\u76f8\u5e94\u7684\u6587\u4ef6\u540d\uff1b
\neg\uff1a\u5c06\u627e\u5230\u7684\u6587\u4ef6\u5168\u90fdcopy\u5230\u53e6\u4e00\u4e2a\u76ee\u5f55\uff1a<\/p>\n

find . -type f -mtime +10 -name \"*.txt\" -exec cp {} OLD \\;<\/pre>\n

\u7ed3\u5408\u591a\u4e2a\u547d\u4ee4
\ntips: \u5982\u679c\u9700\u8981\u540e\u7eed\u6267\u884c\u591a\u4e2a\u547d\u4ee4\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u547d\u4ee4\u5199\u6210\u4e00\u4e2a\u811a\u672c,\u7136\u540e -exec \u8c03\u7528\u65f6\u6267\u884c\u811a\u672c\u5373\u53ef\uff1b<\/p>\n

-exec .\/commands.sh {} \\;<\/pre>\n

-print\u7684\u5b9a\u754c\u7b26,\u9ed8\u8ba4\u4f7f\u7528'\\n'\u4f5c\u4e3a\u6587\u4ef6\u7684\u5b9a\u754c\u7b26\uff1b
\n-print\u5728\u6bcf\u4e00\u4e2a\u8f93\u51fa\u540e\u4f1a\u6dfb\u52a0\u4e00\u4e2a\u56de\u8f66\u6362\u884c\u7b26\uff0c\u800c-print0\u5219\u4e0d\u4f1a\uff0c\u4f7f\u7528'\\0'\u4f5c\u4e3a\u6587\u4ef6\u7684\u5b9a\u754c\u7b26\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u641c\u7d22\u5305\u542b\u7a7a\u683c\u7684\u6587\u4ef6\uff1b
\n\u5f53\u524d\u76ee\u5f55\u4e0b\u6587\u4ef6\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\uff08\u5305\u62ec\u9690\u85cf\u6587\u4ef6\uff09\uff0c\u6587\u4ef6\u540d\u4e0d\u4e3a\".\"\uff1a<\/p>\n

find . -maxdepth 1 ! -name \".\" -print0 | xargs -0 du -b | sort -nr | head -10 | nl<\/pre>\n
grep\u6587\u672c\u641c\u7d22<\/strong><\/div>\n

grep match_patten file \/\/ \u9ed8\u8ba4\u8bbf\u95ee\u5339\u914d\u884c
\n\u5e38\u7528\u53c2\u6570
\n-o \u53ea\u8f93\u51fa\u5339\u914d\u7684\u6587\u672c\u884c VS -v \u53ea\u8f93\u51fa\u6ca1\u6709\u5339\u914d\u7684\u6587\u672c\u884c
\n-c \u7edf\u8ba1\u6587\u4ef6\u4e2d\u5305\u542b\u6587\u672c\u7684\u6b21\u6570<\/p>\n

grep -c \"text\" filename<\/pre>\n

-n \u6253\u5370\u5339\u914d\u7684\u884c\u53f7
\n-i \u641c\u7d22\u65f6\u5ffd\u7565\u5927\u5c0f\u5199
\n-l \u53ea\u6253\u5370\u6587\u4ef6\u540d
\n\u5728\u591a\u7ea7\u76ee\u5f55\u4e2d\u5bf9\u6587\u672c\u9012\u5f52\u641c\u7d22(\u7a0b\u5e8f\u5458\u641c\u4ee3\u7801\u7684\u6700\u7231\uff09\uff1a<\/p>\n

grep \"class\" . -R -n<\/pre>\n

\u5339\u914d\u591a\u4e2a\u6a21\u5f0f<\/p>\n

grep -e \"class\" -e \"vitural\" file<\/pre>\n

grep\u8f93\u51fa\u4ee5\\0\u4f5c\u4e3a\u7ed3\u5c3e\u7b26\u7684\u6587\u4ef6\u540d\uff1a\uff08-z\uff09<\/p>\n

grep \"test\" file* -lZ| xargs -0 rm<\/pre>\n
xargs\u547d\u4ee4\u884c\u53c2\u6570\u8f6c\u6362<\/strong><\/div>\n

xargs \u80fd\u591f\u5c06\u8f93\u5165\u6570\u636e\u8f6c\u5316\u4e3a\u7279\u5b9a\u547d\u4ee4\u7684\u547d\u4ee4\u884c\u53c2\u6570\uff1b\u8fd9\u6837\uff0c\u53ef\u4ee5\u914d\u5408\u5f88\u591a\u547d\u4ee4\u6765\u7ec4\u5408\u4f7f\u7528\u3002\u6bd4\u5982grep\uff0c\u6bd4\u5982find\uff1b
\n\u5c06\u591a\u884c\u8f93\u51fa\u8f6c\u5316\u4e3a\u5355\u884c\u8f93\u51fa<\/p>\n

cat file.txt| xargs<\/pre>\n

\\n \u662f\u591a\u884c\u6587\u672c\u95f4\u7684\u5b9a\u754c\u7b26
\n\u5c06\u5355\u884c\u8f6c\u5316\u4e3a\u591a\u884c\u8f93\u51fa<\/p>\n

cat single.txt | xargs -n 3     -n\uff1a\u6307\u5b9a\u6bcf\u884c\u663e\u793a\u7684\u5b57\u6bb5\u6570<\/span><\/pre>\n

xargs\u53c2\u6570\u8bf4\u660e
\n-d \u5b9a\u4e49\u5b9a\u754c\u7b26 \uff08\u9ed8\u8ba4\u4e3a\u7a7a\u683c \u591a\u884c\u7684\u5b9a\u754c\u7b26\u4e3a \\n\uff09
\n-n \u6307\u5b9a\u8f93\u51fa\u4e3a\u591a\u884c
\n-I {} \u6307\u5b9a\u66ff\u6362\u5b57\u7b26\u4e32\uff0c\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5728xargs\u6269\u5c55\u65f6\u4f1a\u88ab\u66ff\u6362\u6389,\u7528\u4e8e\u5f85\u6267\u884c\u7684\u547d\u4ee4\u9700\u8981\u591a\u4e2a\u53c2\u6570\u65f6
\neg\uff1a<\/p>\n

cat file.txt | xargs -I {} .\/command.sh -p {} -1<\/pre>\n

-0\uff1a\u6307\u5b9a\\0\u4e3a\u8f93\u5165\u5b9a\u754c\u7b26
\neg\uff1a\u7edf\u8ba1\u7a0b\u5e8f\u884c\u6570<\/p>\n

find source_dir\/ -type f -name \"*.cpp\" -print0 |xargs -0 wc -l<\/pre>\n
sort\u6392\u5e8f<\/strong><\/div>\n

\u5b57\u6bb5\u8bf4\u660e\uff1a
\n-n \u6309\u6570\u5b57\u8fdb\u884c\u6392\u5e8f VS -d \u6309\u5b57\u5178\u5e8f\u8fdb\u884c\u6392\u5e8f
\n-r \u9006\u5e8f\u6392\u5e8f
\n-k N \u6307\u5b9a\u6309\u7b2cN\u5217\u6392\u5e8f
\neg\uff1a<\/p>\n

sort -nrk 1 data.txtsort -bd data  \/\/\u5ffd\u7565\u50cf\u7a7a\u683c\u4e4b\u7c7b\u7684\u524d\u5bfc\u7a7a\u767d\u5b57\u7b26<\/span><\/pre>\n

uniq \u6d88\u9664\u91cd\u590d\u884c
\n\u6d88\u9664\u91cd\u590d\u884c<\/p>\n

sort unsort.txt | uniq<\/pre>\n

\u7edf\u8ba1\u5404\u884c\u5728\u6587\u4ef6\u4e2d\u51fa\u73b0\u7684\u6b21\u6570<\/p>\n

sort unsort.txt | uniq -c<\/pre>\n

\u627e\u51fa\u91cd\u590d\u884c<\/p>\n

sort unsort.txt | uniq -d<\/pre>\n

\u53ef\u6307\u5b9a\u6bcf\u884c\u4e2d\u9700\u8981\u6bd4\u8f83\u7684\u91cd\u590d\u5185\u5bb9\uff1a-s\u5f00\u59cb\u4f4d\u7f6e -w\u6bd4\u8f83\u5b57\u7b26\u6570<\/p>\n

\u7528tr\u8fdb\u884c\u8f6c\u6362<\/strong><\/div>\n

\u901a\u7528\u7528\u6cd5<\/p>\n

echo 12345 | tr '0-9' '9876543210'  \/\/\u52a0\u89e3\u5bc6\u8f6c\u6362\uff0c\u66ff\u6362\u5bf9\u5e94\u5b57\u7b26<\/span>  \r\ncat text| tr '\\t' ' '               \/\/\u5236\u8868\u7b26\u8f6c\u7a7a\u683c<\/span><\/pre>\n

tr\u5220\u9664\u5b57\u7b26<\/p>\n

cat file | tr -d '0-9' \/\/ \u5220\u9664\u6240\u6709\u6570\u5b57<\/pre>\n

-c\u6c42\u8865\u96c6<\/p>\n

cat file | tr -c '0-9' \/\/\u83b7\u53d6\u6587\u4ef6\u4e2d\u6240\u6709\u6570\u5b57  cat file | tr -d -c '0-9 \\n'  \/\/\u5220\u9664\u975e\u6570\u5b57\u6570\u636e<\/span><\/pre>\n

tr\u538b\u7f29\u5b57\u7b26
\ntr -s \u538b\u7f29\u6587\u672c\u4e2d\u51fa\u73b0\u7684\u91cd\u590d\u5b57\u7b26\uff1b\u6700\u5e38\u7528\u4e8e\u538b\u7f29\u591a\u4f59\u7684\u7a7a\u683c<\/p>\n

cat file | tr -s ' '<\/pre>\n

\u5b57\u7b26\u7c7b
\ntr\u4e2d\u53ef\u7528\u5404\u79cd\u5b57\u7b26\u7c7b\uff1a
\nalnum\uff1a\u5b57\u6bcd\u548c\u6570\u5b57
\nalpha\uff1a\u5b57\u6bcd
\ndigit\uff1a\u6570\u5b57
\nspace\uff1a\u7a7a\u767d\u5b57\u7b26
\nlower\uff1a\u5c0f\u5199
\nupper\uff1a\u5927\u5199
\ncntrl\uff1a\u63a7\u5236\uff08\u975e\u53ef\u6253\u5370\uff09\u5b57\u7b26
\nprint\uff1a\u53ef\u6253\u5370\u5b57\u7b26<\/p><\/blockquote>\n

\u4f7f\u7528\u65b9\u6cd5\uff1atr [:class:] [:class:]<\/p>\n

eg: tr '[:lower:]' '[:upper:]'<\/pre>\n
cut\u6309\u5217\u5207\u5206\u6587\u672c<\/strong><\/div>\n

\u622a\u53d6\u6587\u4ef6\u7684\u7b2c2\u5217\u548c\u7b2c4\u5217\uff1a<\/p>\n

cut -f2,4 filename<\/pre>\n

\u53bb\u6587\u4ef6\u9664\u7b2c3\u5217\u7684\u6240\u6709\u5217\uff1a<\/p>\n

cut -f3 --complement filename<\/pre>\n

-d \u6307\u5b9a\u5b9a\u754c\u7b26\uff1a<\/p>\n

cat -f2 -d\";\" filename<\/pre>\n

cut\u53d6\u7684\u8303\u56f4
\nN- \u7b2cN\u4e2a\u5b57\u6bb5\u5230\u7ed3\u5c3e
\n-M \u7b2c1\u4e2a\u5b57\u6bb5\u4e3aM
\nN-M N\u5230M\u4e2a\u5b57\u6bb5<\/p>\n

cut\u53d6\u7684\u5355\u4f4d
\n-b \u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d
\n-c \u4ee5\u5b57\u7b26\u4e3a\u5355\u4f4d
\n-f \u4ee5\u5b57\u6bb5\u4e3a\u5355\u4f4d\uff08\u4f7f\u7528\u5b9a\u754c\u7b26\uff09<\/p>\n

cut -c1-5 file          \/\/\u6253\u5370\u7b2c\u4e00\u52305\u4e2a\u5b57\u7b26<\/span> \r\ncut -c-2 file           \/\/\u6253\u5370\u524d2\u4e2a\u5b57\u7b26<\/span><\/pre>\n
paste\u6309\u5217\u62fc\u63a5\u6587\u672c<\/strong><\/div>\n

\u5c06\u4e24\u4e2a\u6587\u672c\u6309\u5217\u62fc\u63a5\u5230\u4e00\u8d77;<\/p>\n

cat file112cat file2colinbookpaste file1 file21 colin2 book<\/pre>\n

\u9ed8\u8ba4\u7684\u5b9a\u754c\u7b26\u662f\u5236\u8868\u7b26\uff0c\u53ef\u4ee5\u7528-d\u6307\u660e\u5b9a\u754c\u7b26
\npaste file1 file2 -d \",\"
\n1,colin
\n2,book<\/p>\n

wc\u7edf\u8ba1\u884c\u548c\u5b57\u7b26\u7684\u5de5\u5177<\/strong><\/div>\n
wc -l file         \/\/\u7edf\u8ba1\u884c\u6570<\/span>\r\nwc -w file         \/\/\u7edf\u8ba1\u5355\u8bcd\u6570<\/span>\r\nwc -c file         \/\/\u7edf\u8ba1\u5b57\u7b26\u6570<\/span><\/pre>\n
sed\u6587\u672c\u66ff\u6362\u5229\u5668<\/strong><\/div>\n

\u9996\u5904\u66ff\u6362<\/p>\n

seg 's\/text\/replace_text\/' file     \/\/\u66ff\u6362\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u5904\u5339\u914d\u7684text<\/span><\/pre>\n

\u5168\u5c40\u66ff\u6362<\/p>\n

seg 's\/text\/replace_text\/g' file<\/pre>\n

\u9ed8\u8ba4\u66ff\u6362\u540e\uff0c\u8f93\u51fa\u66ff\u6362\u540e\u7684\u5185\u5bb9\uff0c\u5982\u679c\u9700\u8981\u76f4\u63a5\u66ff\u6362\u539f\u6587\u4ef6,\u4f7f\u7528-i\uff1a<\/p>\n

seg -i 's\/text\/repalce_text\/g' file<\/pre>\n

\u79fb\u9664\u7a7a\u767d\u884c\uff1a<\/p>\n

sed '\/^$\/d' file<\/pre>\n

\u53d8\u91cf\u8f6c\u6362
\n\u5df2\u5339\u914d\u7684\u5b57\u7b26\u4e32\u901a\u8fc7\u6807\u8bb0&\u6765\u5f15\u7528.<\/p>\n

echo this is en example | seg 's\/\\w+\/[&]\/g'___FCKpd___37gt;[this]  [is] [en] [example]<\/pre>\n

\u5b50\u4e32\u5339\u914d\u6807\u8bb0
\n\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u62ec\u53f7\u5185\u5bb9\u4f7f\u7528\u6807\u8bb0 \\1 \u6765\u5f15\u7528<\/p>\n

sed 's\/hello\\([0-9]\\)\/\\1\/'<\/pre>\n

\u53cc\u5f15\u53f7\u6c42\u503c
\nsed\u901a\u5e38\u7528\u5355\u5f15\u53f7\u6765\u5f15\u7528\uff1b\u4e5f\u53ef\u4f7f\u7528\u53cc\u5f15\u53f7\uff0c\u4f7f\u7528\u53cc\u5f15\u53f7\u540e\uff0c\u53cc\u5f15\u53f7\u4f1a\u5bf9\u8868\u8fbe\u5f0f\u6c42\u503c\uff1a<\/p>\n

sed 's\/$var\/HLLOE\/'<\/pre>\n

\u5f53\u4f7f\u7528\u53cc\u5f15\u53f7\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5728sed\u6837\u5f0f\u548c\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u53d8\u91cf\uff1b<\/p>\n

p=pattenr=replacedecho \"line con a patten\" | sed \"s\/$p\/$r\/g\"___FCKpd___40gt;line con a replaced<\/pre>\n

\u5176\u5b83\u793a\u4f8b
\n\u5b57\u7b26\u4e32\u63d2\u5165\u5b57\u7b26\uff1a\u5c06\u6587\u672c\u4e2d\u6bcf\u884c\u5185\u5bb9\uff08PEKSHA\uff09 \u8f6c\u6362\u4e3a PEK\/SHA<\/p>\n

sed 's\/^.\\{3\\}\/&\\\/\/g' file<\/pre>\n
awk\u6570\u636e\u6d41\u5904\u7406\u5de5\u5177<\/strong><\/div>\n

awk\u811a\u672c\u7ed3\u6784<\/p>\n

awk 'BEGIN{ statements } statements2 END{ statements }'<\/pre>\n

\u5de5\u4f5c\u65b9\u5f0f
\n1.\u6267\u884cbegin\u4e2d\u8bed\u53e5\u5757\uff1b
\n2.\u4ece\u6587\u4ef6\u6216stdin\u4e2d\u8bfb\u5165\u4e00\u884c\uff0c\u7136\u540e\u6267\u884cstatements2\uff0c\u91cd\u590d\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u76f4\u5230\u6587\u4ef6\u5168\u90e8\u88ab\u8bfb\u53d6\u5b8c\u6bd5\uff1b
\n3.\u6267\u884cend\u8bed\u53e5\u5757\uff1b<\/p>\n

print \u6253\u5370\u5f53\u524d\u884c
\n\u4f7f\u7528\u4e0d\u5e26\u53c2\u6570\u7684print\u65f6\uff0c\u4f1a\u6253\u5370\u5f53\u524d\u884c;<\/p>\n

echo -e \"line1\\nline2\" | awk 'BEGIN{print \"start\"} {print } END{ print \"End\" }'<\/pre>\n

print \u4ee5\u9017\u53f7\u5206\u5272\u65f6\uff0c\u53c2\u6570\u4ee5\u7a7a\u683c\u5b9a\u754c;<\/p>\n

echo | awk ' {var1 = \"v1\" ; var2 = \"V2\"; var3=\"v3\"; \\print var1, var2 , var3; }'___FCKpd___43gt;v1 V2 v3<\/pre>\n

\u4f7f\u7528-\u62fc\u63a5\u7b26\u7684\u65b9\u5f0f\uff08\"\"\u4f5c\u4e3a\u62fc\u63a5\u7b26\uff09;<\/p>\n

echo | awk ' {var1 = \"v1\" ; var2 = \"V2\"; var3=\"v3\"; \\print var1\"-\"var2\"-\"var3; }'___FCKpd___44gt;v1-V2-v3<\/pre>\n

\u7279\u6b8a\u53d8\u91cf\uff1a NR NF $0 $1 $2
\nNR:\u8868\u793a\u8bb0\u5f55\u6570\u91cf\uff0c\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u5bf9\u5e94\u5f53\u524d\u884c\u53f7\uff1b
\nNF:\u8868\u793a\u5b57\u6bb5\u6570\u91cf\uff0c\u5728\u6267\u884c\u8fc7\u7a0b\u603b\u5bf9\u5e94\u5f53\u524d\u884c\u7684\u5b57\u6bb5\u6570\uff1b
\n$0:\u8fd9\u4e2a\u53d8\u91cf\u5305\u542b\u6267\u884c\u8fc7\u7a0b\u4e2d\u5f53\u524d\u884c\u7684\u6587\u672c\u5185\u5bb9\uff1b
\n$1:\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u7684\u6587\u672c\u5185\u5bb9\uff1b
\n$2:\u7b2c\u4e8c\u4e2a\u5b57\u6bb5\u7684\u6587\u672c\u5185\u5bb9\uff1b<\/p>\n

echo -e \"line1 f2 f3\\n line2 \\n line 3\" | awk '{print NR\":\"$0\"-\"$1\"-\"$2}'<\/pre>\n

\u6253\u5370\u6bcf\u4e00\u884c\u7684\u7b2c\u4e8c\u548c\u7b2c\u4e09\u4e2a\u5b57\u6bb5\uff1a<\/p>\n

awk '{print $2, $3}' file<\/pre>\n

\u7edf\u8ba1\u6587\u4ef6\u7684\u884c\u6570\uff1a<\/p>\n

awk ' END {print NR}' file<\/pre>\n

\u7d2f\u52a0\u6bcf\u4e00\u884c\u7684\u7b2c\u4e00\u4e2a\u5b57\u6bb5\uff1a<\/p>\n

echo -e \"1\\n 2\\n 3\\n 4\\n\" | awk 'BEGIN{num = 0 ;  print \"begin\";} {sum += $1;} END {print \"==\"; print sum }'<\/pre>\n

\u4f20\u9012\u5916\u90e8\u53d8\u91cf<\/p>\n

var=1000 echo | awk '{print vara}' vara=$var # \u8f93\u5165\u6765\u81eastdinawk '{print vara}' vara=$var file # \u8f93\u5165\u6765\u81ea\u6587\u4ef6<\/pre>\n

\u7528\u6837\u5f0f\u5bf9awk\u5904\u7406\u7684\u884c\u8fdb\u884c\u8fc7\u6ee4
\nawk 'NR < 5' #\u884c\u53f7\u5c0f\u4e8e5
\nawk 'NR==1,NR==4 {print}' file #\u884c\u53f7\u7b49\u4e8e1\u548c4\u7684\u6253\u5370\u51fa\u6765
\nawk '\/linux\/' #\u5305\u542blinux\u6587\u672c\u7684\u884c\uff08\u53ef\u4ee5\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u6307\u5b9a\uff0c\u8d85\u7ea7\u5f3a\u5927\uff09
\nawk '!\/linux\/' #\u4e0d\u5305\u542blinux\u6587\u672c\u7684\u884c<\/p>\n

\u8bbe\u7f6e\u5b9a\u754c\u7b26
\n\u4f7f\u7528-F\u6765\u8bbe\u7f6e\u5b9a\u754c\u7b26\uff08\u9ed8\u8ba4\u4e3a\u7a7a\u683c\uff09<\/p>\n

awk -F: '{print $NF}' \/etc\/passwd<\/pre>\n

\u8bfb\u53d6\u547d\u4ee4\u8f93\u51fa
\n\u4f7f\u7528getline\uff0c\u5c06\u5916\u90e8shell\u547d\u4ee4\u7684\u8f93\u51fa\u8bfb\u5165\u5230\u53d8\u91cfcmdout\u4e2d\uff1b<\/p>\n

echo | awk '{\"grep root \/etc\/passwd\" | getline cmdout; print cmdout }'<\/pre>\n

\u5728awk\u4e2d\u4f7f\u7528\u5faa\u73af<\/p>\n

for(i=0;i<10;i++){print $i;}\r\nfor(i in array){print array[i];}<\/pre>\n

\u4ee5\u9006\u5e8f\u7684\u5f62\u5f0f\u6253\u5370\u884c\uff1a(tac\u547d\u4ee4\u7684\u5b9e\u73b0\uff09<\/p>\n

seq 9| \\awk '{lifo[NR] = $0; lno=NR} \\END{ for(;lno>-1;lno--){print lifo[lno];}} '\r\n<\/pre>\n

awk\u5b9e\u73b0head\u3001tail\u547d\u4ee4<\/p>\n

head:\r\n  awk 'NR< =10{print}' filename<\/pre>\n
tail:\r\n  awk '{buffer[NR%10] = $0;} END{for(i=0;i<11;i++){ \\  print buffer[i %10]} } ' filename<\/pre>\n

\u6253\u5370\u6307\u5b9a\u5217
\nawk\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n

ls -lrt | awk '{print $6}'<\/pre>\n

cut\u65b9\u5f0f\u5b9e\u73b0<\/p>\n

ls -lrt | cut -f6<\/pre>\n

\u6253\u5370\u6307\u5b9a\u6587\u672c\u533a\u57df
\n\u786e\u5b9a\u884c\u53f7<\/p>\n

seq 100| awk 'NR==4,NR==6{print}'<\/pre>\n

\u786e\u5b9a\u6587\u672c
\n\u6253\u5370\u5904\u4e8estart_pattern \u548cend_pattern\u4e4b\u95f4\u7684\u6587\u672c\uff1b<\/p>\n

awk '\/start_pattern\/, \/end_pattern\/' filename<\/pre>\n
eg: seq 100 | awk '\/13\/,\/15\/'cat \/etc\/passwd| awk '\/mai.*mail\/,\/news.*news\/'<\/pre>\n

awk\u5e38\u7528\u5185\u5efa\u51fd\u6570
\nindex(string,search_string):\u8fd4\u56desearch_string\u5728string\u4e2d\u51fa\u73b0\u7684\u4f4d\u7f6e
\nsub(regex,replacement_str,string):\u5c06\u6b63\u5219\u5339\u914d\u5230\u7684\u7b2c\u4e00\u5904\u5185\u5bb9\u66ff\u6362\u4e3areplacement_str;
\nmatch(regex,string):\u68c0\u67e5\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5426\u80fd\u591f\u5339\u914d\u5b57\u7b26\u4e32\uff1b
\nlength(string)\uff1a\u8fd4\u56de\u5b57\u7b26\u4e32\u957f\u5ea6<\/p>\n

echo | awk '{\"grep root \/etc\/passwd\" | getline cmdout; print length(cmdout) }'<\/pre>\n

printf \u7c7b\u4f3cc\u8bed\u8a00\u4e2d\u7684printf\uff0c\u5bf9\u8f93\u51fa\u8fdb\u884c\u683c\u5f0f\u5316<\/p>\n

seq 10 | awk '{printf \"->%4s\\n\", $1}'<\/pre>\n

\u8fed\u4ee3\u6587\u4ef6\u4e2d\u7684\u884c\u3001\u5355\u8bcd\u548c\u5b57\u7b26
\n1. \u8fed\u4ee3\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u884c
\nwhile \u5faa\u73af\u6cd5<\/p>\n

while read line;doecho $line;done < file.txt\u6539\u6210\u5b50shell:cat file.txt | (while read line;do echo $line;done)<\/pre>\n

awk\u6cd5\uff1a<\/p>\n

cat file.txt| awk '{print}'<\/pre>\n

2.\u8fed\u4ee3\u4e00\u884c\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5355\u8bcd<\/p>\n

for word in $line;do echo $word;done<\/pre>\n

3. \u8fed\u4ee3\u6bcf\u4e00\u4e2a\u5b57\u7b26
\n${string:start_pos:num_of_chars}\uff1a\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u4e00\u4e2a\u5b57\u7b26\uff1b(bash\u6587\u672c\u5207\u7247\uff09
\n${#word}:\u8fd4\u56de\u53d8\u91cfword\u7684\u957f\u5ea6<\/p>\n

for((i=0;i< ${#word};i++))doecho ${word:i:1);done<\/pre>\n
\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.yunweipai.com\/archives\/6074.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/linux-shell-text-commonly-tools.html<\/a>\u7f16\u8f91\uff1a\u5cb3\u6c38\uff0c\u5ba1\u6838\u5458\uff1a\u5f20\u5b8f\u5b87<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb \u672c\u6587\u5c06\u4ecb\u7ecdLinux\u4e0b\u4f7f\u7528Shell\u5904\u7406\u6587\u672c\u65f6\u6700\u5e38\u7528\u7684\u5de5\u5177\uff1afind\u3001grep\u3001xargs\u3001sort\u3001u […]<\/p>\n","protected":false},"author":63,"featured_media":10431,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-10285","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/10285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=10285"}],"version-history":[{"count":42,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/10285\/revisions"}],"predecessor-version":[{"id":250495,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/10285\/revisions\/250495"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/10431"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=10285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=10285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=10285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}