Posted on:
Last modified:
-d 按照字典排序(默认)
-n 按照数字大小排序
-f 忽略大小写
-g 按照浮点数排序
-M 按照 Jan Feb 等排序
-r 逆序
-k 指定第 k 列排序 -t 指定分隔符
-b 忽略行首空格字符
-m 把两个已经排序的文件合并
uniq 命令假定文件是已经排序的,因此基本要接着 sort 使用
sort unsorted.txt | uniq
删除重复行sort unsorted.txt | uniq -c
统计行数sort unsorted.txt | uniq -d
只显示重复行 -u 只显示不重复行cut 选取某一列 sort
--completment 选取补集 --output-delimiter 输出
统计单词 -c 字符数 -w 单词数 -l 行数
给每行添加行号 -ba 所有行
按列合并文本,可以组合两个 csv 成为一个多列的 csv
paste -d "\t" file1.txt file2.txt > fileresults.txt
xargs reformat the data it received and give it to next command. xargs squish all parameters into one line
-d set delemeters -t 回显命令 -I 执行指定的 Instruction,需要指定操作符 如 ls *.md | xargs -t -I "%" cp "%" markdown/ -n output with n args on one line
Commands | BRE | ERE | PCRE |
---|---|---|---|
sed | * | -E/-r | |
awk | * | ||
grep | * | -E | -P |
find | * |
sed options script file
by default, sed uses BRE, but you probably want sed -E to enable ERE, see http://liujiacai.net/blog/2014/12/07/regexp-favors/. basically, sed works line by line and do transformation
s for substitution
sed s/day/night/OPTION OLDFILE > NEWFILE
默认只替换第一处出现,/g 替换所有
/ 只是默认的分隔符,可以任意使用,推荐使用:
使用 & 作为匹配到的字符,使用 \1 \2 来表示匹配到的分组,这种表达方式倒是和 pcre 有些类似
使用 -r(linux) 或者 -E(mac) 才能够使用扩展的 posix 正则,一定要带上这两个
OPTION
处理命令行输入输出的时候可能经常需要 trim, 尤其是字符串带了 \n 的情况
sed -n 's/PATTERN/&/p' file
sed '/PATTERN/p' file
-i inplace
-i.bak inplace with backup
-E/-r use ERE
sed command syntax: option/pattern/command parameters pattern selects lines and do command with parameters on those lines
gawk options program varlist file
gawk, like sed, is also line-oriented
$0 the whole line $1...$n word in line NR number of rows NF number of fileds
awk 'BEGIN{ } pattern { } END { }' file
BEGIN in used for initialization, and END is used to print out the result, pattern {} is run against each line
awk 'pattern' file
print lines with the pattern
grep pattern file1 file2...
grep pattern *
grep pattern -r <dir>
grep 有个小技巧:
ps -ef | grep [p]ython
这样就只会显示 python,而不会显示 grep 了。
force grep to show file name:
grep pattern file /dev/null
-v invert search -r/-R recursively -c count matching lines -o printing matched part in different lines -b -o output offset -n with line numbers -i ignore case -l list matched files -f pattern from --include include files for grep -Z use \0 as delemeter -q no output only return value -A n lines after -B n lines before -C n lines combined
按文件过滤,支持多种文件类型,可以用ag --list-file-types
查看支持的类型
© 2016-2022 Yifei Kong. Powered by ynotes
All contents are under the CC-BY-NC-SA license, if not otherwise specified.
Opinions expressed here are solely my own and do not express the views or opinions of my employer.
友情链接: MySQL 教程站