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}`
相關文章
相關標籤/搜索