linux sed, grep 用法

sed: http://www.gnu.org/software/sed/manual/sed.html
grep: http://www.gnu.org/software/grep/manual/html_node/index.htmlnode

sed

Overview

  • 使用sed的常见形式
    sed SCRIPT INPUTFILE...

sed替换命令:s

  • 把文件中的‘hello’ 替换为 ‘world’
    sed 's/hello/world/g' input.txt > output.txt
    其中 选项g是指替换全部搭配,不仅是第一个。

sed 删除命令:d

  • 把输入文件中全部匹配的行删除。
    sed -i '/hello/d' input.txtlinux

  • 不说明输入文件或是输入文件为横杆时候,sed将处理标准输入的内容,下面命令等价
    sed 's/hello/world/' input.txt > output.txt
    sed 's/hello/world/' < input.txt > output.txt
    cat input.txt | sed 's/hello/world/' - > output.txtshell

  • sed 默认输出是标准输出,用选项 -i ,把编辑的结果保存到文件,而不是把结果打印到标准输出。
    下面的命令会修改文件,没有输出。
    sed -i 's/hello/world/' file.txt
  • sed 默认打印全部要处理的输入行(除非经过d删除的行不打印),选项 -n to suppress output,用-p打印特定行,
    下面的命令打印第3行
    sed -n '45p' file.txt
  • 没有选项-e or -f 时候,sed用第一个非选项参数做为脚本例如('s/hello/world/'),接着的非选项参数做为输入文件。
    若是有选项-e or -f 来指定脚本,全部的非选项参数做为输入文件。-e and -f 能够同时出现屡次。
    The following examples are equivalent:
    sed 's/hello/world/' input.txt > output.txt
    sed -e 's/hello/world/' input.txt > output.txt
    sed --expression='s/hello/world/' input.txt > output.txt
    echo 's/hello/world/' > myscript.sed
    sed -f myscript.sed input.txt > output.txt
    sed --file=myscript.sed input.txt > output.txtexpress

命令行选项 Command-Line Options

  • The full format for invoking sed is:
    sed OPTIONS... [SCRIPT] [INPUTFILE...]

grep

总览 SYNOPSIS
       grep [options] PATTERN [FILE...]
       grep [options] [-e PATTERN | -f FILE] [FILE...]

       grep [选项]  要匹配的字符串  [文件或者路径]

OPTIONS

Matching Control 匹配控制

-e PATTERN, --regexp=PATTERN

指定【PATTERN】,为了区分【PATTERN】中含有 “-” 的状况函数

-f FILE, --file=FILE
从文件中读取【PATTERN】,每行一个。ui

-i, --ignore-case
忽略大小写this

-v, --invert-match
反向匹配, 打印无 【PATTERN】的行。命令行

-w, --word-regexp
选中包含【PATTERN】的行,其中【PATTERN】是整个单词。code

-x, --line-regexp
选中包含【PATTERN】的行,其中【PATTERN】是占整个行,无别的内容。

General Output Control 输出控制

-c, --count
只打印文件中匹配行的个数,不打印行内容。

--color[=WHEN], --colour[=WHEN]
WHEN is never, always, or auto.

-L, --files-without-match
打印无【PATTERN】的文件名。

-l, --files-with-matches
只打印含【PATTERN】的文件名。

-q, --quiet, --silent
无打印,正常返回0

-s, --no-messages
不打印,没法读取之类的错误消息。

Output Line Prefix Control 输出行前缀控制

-b, --byte-offset
          在输出的每行前面同时打印出当前行在输入文件中的字节偏移量。
   -H, --with-filename
          Print the file name for each match.  This is the default when there is more than one file to search.
        打印每一个匹配行的文件名,多个文件时候默认打开

   -h, --no-filename
          Suppress the prefixing of file names on output.  This is the default when there is only one file (or only standard input) to search.
        不打印每一个匹配行的文件名,单个文件时候默认打开

    -n, --line-number
          Prefix each line of output with the 1-based line number within its input file.
        打印行号

-T, --initial-tab
用tab 把文件名,行号,内容隔开,并对齐。

Context Line Control,先后行控制

-A NUM, --after-context=NUM
          Print NUM lines of trailing context after matching lines.  Places a line containing a group separator  (--)  between  contiguous  groups  of
          matches.  With the -o or --only-matching option, this has no effect and a warning is given.
        打印匹配行 后的 NUM。

   -B NUM, --before-context=NUM
          Print  NUM  lines  of  leading  context before matching lines.  Places a line containing a group separator (--) between contiguous groups of
          matches.  With the -o or --only-matching option, this has no effect and a warning is given.
        打印匹配行 后的 NUM。

   -C NUM, -NUM, --context=NUM
          Print NUM lines of output context.  Places a line containing a group separator (--) between contiguous groups of matches.  With  the  -o  or
          --only-matching option, this has no effect and a warning is given.
        打印匹配行先后的 NUM 行,等价于同时设  -A  -B

File and Directory Selection

-d ACTION, --directories=ACTION

If an input file is a directory, use ACTION to process it.
若是输入文件是一个目录,就用 ACTION 去处理它,默认是read,
If ACTION is skip, silently skip directories.若是是skip,就跳过,
If ACTION is recurse, read all files under each directory,若是是recurse,就递归读入每一个文件。This is equivalent to the -r option.

-R, -r, --recursive
          递归地读每一目录下的全部文件。这样作和 -d recurse 选项等价。

    --include=PATTERN
          仅仅在搜索匹配 PATTERN 的文件时在目录中递归搜索。

     --exclude=PATTERN
          在目录中递归搜索,可是跳过匹配 PATTERN 的文件。

-s, --no-messages
禁止输出关于文件不存在或不可读的错误信息。

shell

ref

大括号、花括号 {}

(1) 大括号拓展。(通配(globbing))将对大括号中的文件名作扩展。在大括号中,不容许有空白,除非这个空白被引用或转义。第一种:对大括号中的以逗号分割的文件列表进行拓展。如 touch {a,b}.txt 结果为a.txt b.txt。第二种:对大括号中以点点(..)分割的顺序文件列表起拓展做用,如:touch {a..d}.txt 结果为a.txt b.txt c.txt d.txt

# ls {ex1,ex2}.sh    
ex1.sh  ex2.sh    
# ls {ex{1..3},ex4}.sh    
ex1.sh  ex2.sh  ex3.sh  ex4.sh    
# ls {ex[1-3],ex4}.sh    
ex1.sh  ex2.sh  ex3.sh  ex4.sh

(2) 代码块,又被称为内部组,这个结构事实上建立了一个匿名函数 。与小括号中的命令不一样,大括号内的命令不会新开一个子shell运行,即脚本余下部分仍可以使用括号内变量。括号内的命令间用分号隔开,最后一个也必须有分号。{}的第一个命令和左括号之间必需要有一个空格。

demo

grep

当前目录下的全部文件, *号不能少,匹配全部文件。
grep objStr ./*

当前目录下全部文件,递归搜索,只须要指定目录。
grep objStr -r ./

指定一个匹配类型
grep objStr -r ./ --include=*.h

指定多个匹配类型
grep objStr -rl ./ --include=*.{cpp,c}

grep combine sed

变量替换。

sed -i 's/gBpfShareData/g_bpfShareData/g'   `grep gBpfShareData -rl ./ --include=*.{cpp,c,h}`
相关文章
相关标签/搜索