命令行文本处理(sed & awk & grep)


Author: yifei / Created: May 30, 2017, 8:37 p.m. / Modified: May 30, 2017, 8:45 p.m. / Edit

regular expression in shell

Commands BRE ERE PCRE
sed * -E/-r
awk *
grep * -E -P
find *

Sed

Basic format

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

Options

-i  inplace
-i.bak  inplace with backup
-E/-r   use ERE

script

sed command syntax: option/pattern/command parameters pattern selects lines and do command with parameters on those lines

awk

basic usage

gawk options program varlist file

gawk, like sed, is also line-oriented

vaiables

$0 the whole line $1...$n word in line NR number of rows NF number of fileds

awk program

awk 'BEGIN{ } pattern { } END { }' file BEGIN in used for initilization, and END is used to print out the result, pattern {} is run against each line

awk 'pattern' file print lines with the pattern

grep

grep pattern file1 file2...
grep pattern *
grep pattern -r <dir>

force grep to show file name: grep pattern file /dev/null

options

-v invert search -r/-R recursively -c count matching lines -o printing matched part in differnet 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

按文件过滤,支持多种文件类型,可以用ag --list-file-types查看支持的类型


评论区