1. 程式人生 > >【unix學習】檔案處理4—sed + wget + awk命令

【unix學習】檔案處理4—sed + wget + awk命令

unix的檔案處理sed + wget + awk命令

sed命令

Stream editor - Sed
a streamlined, noninteractive editor 一個精簡的、非互動式編輯

format

sed ‘[address]command’ filename(s)
sed 是基於行的,因此按順序對每一行執行命令。
然後,sed 將其結果寫入標準輸出(stdout),它不修改任何輸入檔案。

command

command |function
d| Delete lines
s| Substitutes one string for another.
g| Globally substitutes on a line
p| Print lines
a| Appends one or more lines of text to the current line
i| Inserts text above the current line.
!| Applies the command to all lines except the selected ones.

例項

1.顯示檔案時,刪除檔案的指定行數
[[email protected]]$sed '1,15d' /tmp/student_record
刪除檔案的第一行到十五行,然後顯示
當用逗號將兩個地址分開時,sed 將把後面的命令應用到從第一個地址開始、到第二個地址
結束的範圍。

2.帶正則表示式的地址
sed 刪除以 ‘#’ 開始的行
$ sed -e '/^#/d' /etc/services
正則表示式地址總是由斜槓括起。
它們指定一種模式,緊跟在正則表示式地址之後的命令將僅適用於正好與該特定模式匹配的行。

3.刪除空格的行,然後顯示

解釋下面的意思:
    d是刪除的意思
    \ 轉義
    /A ...
B/ 表示的是開始A和結束B ^ 開始 * 任意多個 Beginning-of-word \< End-of-word \>

[[email protected]]$sed '/^$/d' /tmp/student_record

4.刪除包含CS的行
[[email protected]]$sed '/\<CS/d' /tmp/student_record

5.不刪除包含CS的行 !
[[email protected]]$sed '/\<CS/!d' /tmp/student_record

6.替換
s/regexp/replacement/

[s14516@gdufs]$cat /tmp/space.txt
first line
  sencond line
    third line
     forth line

[s14516@gdufs]$sed 's/^ *//' /tmp/space.txt

s替換掉空格後結果:

first line
sencond line
third line
forth line

7.替換字母E為e

eg:
$ sed -e '/^$/,/^END/s/hills/mountains/g' myfile3.txt
該例將用 'mountains' 替換 'hills',但是,只從空行開始,到以三個字元 'END' 開始的行結
束(包括這兩行)的文字塊上這樣做

8.查詢輸出

P   Print up to the first embedded newline of  the  current  pattern space.

預設輸出的過程中,當遇到p命令的時候會再輸出一次
[[email protected]]$sed '/\<CS/p' /tmp/student_record

John    Doe     ECE     3.54
James   Davis   ECE     3.71
Al      Davis   CS      2.63
Al      Davis   CS      2.63

只輸出p命令的行
[[email protected]]$sed -n '/\<CS/p' /tmp/student_record

Al      Davis   CS      2.63

查詢e結尾的行'/e$/p',只顯示匹配到的行(-n)
[[email protected]]$sed -n '/e$/p' /tmp/linux.txt
注意:在windows下可能不能成功,原因看第10點

9.一個字元一個字元顯示

[s14516@gdufs]$od -c /tmp/linux.txt

0000000   I       a   m       a       s   t   u   d   e   n   t  \n   t

10.結尾的判斷是根據\n來判斷的

[[email protected]]$od -c /tmp/linux.txt
0000000   I       a   m       a       s   t   u   d   e   n   t  \n   t
0000020   h   i   s       f   i   r   s   t       l   i   n   e  \n   t
0000040   h   i   s       s   e   c   o   n   d       l   i   n   e  \n
0000060
[[email protected]]$od -c /tmp/win.txt
0000000   I       a   m       a       s   t   u   d   e   n   t  \r  \n
0000020   t   h   i   s       f   i   r   s   t       l   i   n   e  \r
0000040  \n   t   h   i   s       s   e   c   o   n   d       l   i   n
0000060   e  \r  \n
0000063

對於在window下檔案是\r\n換行,所以不能成功查詢。
修改操作,把檔案的\r替換掉

[s14516@gdufs]$sed -i 's/\r//' win.txt
[s14516@gdufs]$sed -n '/e$/p' win.txt

this first line
this second line

wget命令

  1. 下載網頁
    wget url
    [[email protected]]$wget www.baidu.com
    下載後儲存為index.html

2.提取網頁正文

<a href=http://home.baidu.com>關於百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必讀</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a>&nbsp;京ICP證030173號&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

s替換操作
[[email protected]]$sed 's/<.*>//' index.html
上面的正則是最大化的匹配<>
結果為空,全部都被替換了

使用最小化的匹配<>
然後g多次應用正則替換多個。否則只替換一次

[s14516@gdufs]$sed 's/<[^>]*>//g' index.html
 百度一下,你就知道                     新聞 hao123 地圖 視訊 貼吧  登入  document.write('登入'); 更多產品       關於百度 About Baidu  &copy;2017&nbsp;Baidu&nbsp;使用百度前必讀&nbsp; 意見反饋&nbsp;京ICP證030173號&nbsp;

儲存替換的結果
-i 是修改並儲存

[s14516@gdufs]$sed -i 's/<[^>]*>//g' index.html

替換空格
先替換\r
[[email protected]]$sed -i 's/\r//' index.html
再替換空格
[[email protected]]$sed -i '/^[ \t]*$/d' index.html

AWK工具

a programming language used for manipulating data and generating reports
一種用來處理資料和生成報告的程式設計語言。
awk最基本功能是在檔案或字串中基於指定規則瀏覽和抽取資訊

格式

awk [-F field-separator] ‘commands’ input-file(s)

  1. 域和記錄
    [-F域分隔符]是可選的,因為awk使用空格作為預設的域分隔符。
    field-separator: $1 $2....
    awk執行時,其瀏覽域標記為$1,$2...$n,這種方法稱為域標識,$0——表示所有域
    為列印一個域或所有域,使用print命令。這是一個awk動作(動作語法用花括號括起來)。
    awk -F: '{print $1,$6}' /etc/passwd

  2. 儲存awk的輸出
    1)使用輸出重定向
    2)使用tee命令,在輸出到檔案的同時輸出到螢幕。
    awk '{print $3}' /etc/fstab | tee fstab.fs

例項

1.指定欄位分割符號(-F),然後輸出對應的欄位(print $1
[[email protected]]$awk -F: '{print $1,$3}' /etc/passwd

2.指定多個分割符號([ :])
下面指定看空格和冒號為分隔符,找欄位
[[email protected]]$awk -F'[ :]' '{print $1,$3}' /etc/passwd

3.找出滿足關係表示式的欄位 欄位大於3

< 小於
> 大於
>= 大於等於
<= 小於等於
== 等於
!= 不等於
[[email protected]]$awk '$4>3' /tmp/student_record

4.對某個欄位進行匹配使用波浪號~ 第四個欄位開頭是大小寫K
~ 匹配正則表示式
!~ 不匹配正則表示式

[s14516@gdufs]$awk '$2~/^[Kk]/' /tmp/databook
[s14516@gdufs]$awk -F: '$0~/root/' /etc/passwd
root:x:0:0:root:/root:/bin/bash

5.欄位的算數表示式
[[email protected]]$awk '($2+$3+$4)/3>85' /tmp/grade.txt

定義action

[[email protected]]$awk '($2+$3+$4)/3>85{print $1,($2+$3+$4)/3}' /tmp/grade.txt

6.邏輯表示式
複合模式匹配:
&& AND 語句兩邊必須同時匹配為真。
|| OR 語句兩邊同時或其中一邊匹配為真。
! 非 求逆

[s14516@gdufs]$awk '$3=="CS" && $4 >= 3' /tmp/student_record

7.匹配學號後三位是4開頭

[[email protected]]$awk '$1~/4[0-9][0-9]$/' /tmp/stu.txt

不支援\{2\}的正則

8.顯示行號 NR

[s14516@gdufs]$awk '/\&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;CS/{print NR, $0}' /tmp/student_record
3 Al    Davis   CS      2.63
8 Rick  Marsh   CS      2.34

確保整個awk命令用單引號括起來。