天天看點

第04章-vim編輯器-環境變量-使用者檔案權限-sed與awk

作者:湖南小陳dy

上一章:第03章-linux常用指令

Vim編輯器

Vim編輯器是多模式編輯器,它是Vi的更新版本,它相容Vi的所有指令,還有些新特性在裡面,Vim具有代碼補全,編譯以及錯誤跳轉等友善程式設計的豐富功能。我們對linux的各種服務進行配置的時候,實際上都是對各種配置檔案的配置,一般伺服器都是指令界面,是以掌握Vim編輯器是你開發路上的必備技能。

Vim是一款使用特别廣泛的文本編輯器,使用過程中會涉及到3種模式:一般模式丶行末模式丶輸入模式,每種模式又支援多種不同的指令快捷鍵,使用者習慣後會覺得相當的順手。如果你想熟悉的使用它,就必須熟練的掌握這3種模式的切換和快捷鍵使用。三種模式的切換方法見下圖:

第04章-vim編輯器-環境變量-使用者檔案權限-sed與awk

一般模式快捷鍵

光标移動快捷鍵:

快捷鍵 說明
光标移動到這一行最前面
$ 光标移動到這一行最後面
g 光标移動到檔案第一行
G 光标移動到檔案最後一行
數字+enter 光标移動到數字所在行

内容操作快捷鍵:

快捷指令 說明
yy 複制光标所在整行
3yy 複制從光标處開始的後3行
p p:粘貼到光标下一行
P P:粘貼到光标上一行
u 撤銷
dd 删除(剪切)光标所在行
3dd 删除(剪切)從光标處開始的後3行

一般模式切換到輸入模式快捷鍵

快捷指令 說明
i 目前光标進入到輸入模式
I 目前光标移到所在行最前面進入輸入模式
a 目前光标下一個光标進入到輸入模式
A 目前光标移到所在行最後面進入輸入模式
o 目前光标移到下一行進入輸入模式,注意會添加新行
O 目前光标移到上一行進入輸入模式,注意會添加新行

一般模式切換到行末模式快捷鍵

快捷指令 說明
:w 儲存
:q 退出
:wq [檔案名] 儲存且退出,若後面有檔案名,則儲存為指定檔案名檔案
:q! 強制離開不儲存
:wq! 強制儲存且退出
:set nu 顯示行号
:set nonu 不顯示行号
:3 光标定位到第3行
:s/hello/world/g 将整行的hello替換為world
:%s/hello/world/g 将整個檔案的所有行的hello替換為world
/hello 查找hello字元串,從上到下查找,n向下找,N向上找
?hello 查找hello字元串,從下到上查找,n向下找,N向上找

環境變量

什麼是環境變量

環境變量是用來存儲有關shell會話的資訊以及通用的系統工作環境資訊,可以把它看做一個通用的鍵值對系統配置檔案資訊加載到了記憶體,以便其它的程式或者shell能夠友善的通路使用。預設情況下,系統會有一批預設的環境變量來定義系統環境。注意系統環境變量基本上都使用全大寫字母。下表格列出些常用到的系統環境變量。

目錄路徑名稱 說明
$HOME 目前使用者的主目錄
$PATH SHELL查找指令的目錄清單
$LANG SHELL的語言環境
$SHELL 目前使用的SHELL解釋器
$RANDOM 生成一個随機數
$USER 目前使用者名稱

全局環境變量和局部環境變量

全局環境變量就是對于shell的會話和子shell會話來說都是可以進行使用的。與之對應的局部環境變量隻有本身建立的shell可以通路。全局變量定義:export 變量名稱。

例1:局部變量隻能在本身shell可以通路。

下面是test1.sh和test2.sh代碼

第04章-vim編輯器-環境變量-使用者檔案權限-sed與awk
第04章-vim編輯器-環境變量-使用者檔案權限-sed與awk
[root@cdphost temp1]# sh test1.sh
test1 var1=hello
test2 var1=           

說明:從上面輸出結果可以看出test2.sh沒有通路到test1.sh中的變量var1。

例2:全局變量可以在子shell通路。

下面是test1.sh和test2.sh代碼

第04章-vim編輯器-環境變量-使用者檔案權限-sed與awk
第04章-vim編輯器-環境變量-使用者檔案權限-sed與awk
[root@cdphost temp1]# sh test1.sh
test1 var1=hello
test2 var1=hello           

說明:從上面輸出結果可以看出test2.sh可以通路到test1.sh中的變量var1。

删除環境變量

在shell中可以建立環境變量,當然我們也可以删除環境變量,使用關鍵字unset進行删除環境變量的操作,例如:unset 環境變量名稱。

持久化環境變量

你已經知道了如何建立以及删除環境變量,那麼接下來是如何持久的儲存環境變量。

當你登入shell的時候,shell會從下面4個檔案裡面讀取指令:

  • /etc/profile
  • /etc/bashrc
  • $HOME/.bash_profile
  • $HOME/.bashrc

/etc/profile和/etc/bashrc檔案是系統環境檔案,每個使用者啟動shell的時候會執行。

$HOME/.bash_profile和$HOME/.bashrc檔案是每個使用者獨有的自己的環境檔案,每個使用者啟動shell的時候會執行。對于全使用者環境變量來說一般在/etc/profile中設定。對于使用者自己的環境變量一般在$HOME/.bash_profile中設定

例1:設定全使用者環境變量my_var1=hello。

第04章-vim編輯器-環境變量-使用者檔案權限-sed與awk
[root@cdphost ~]# echo $my_var1
hello
[root@cdphost ~]# su - cdp
Last login: Mon Dec 26 23:35:40 CST 2022 on pts/1
[cdp@cdphost ~]$ echo $my_var1
hello           

說明:通過上面可以看到root使用者和cdp使用者都可以通路環境變量 my_var1。

PATH環境變量

linux下的PATH環境變量和windows中的Path環境變量是一個作用,當你從指令行界面輸入一個指令的時候,shell會從PATH所定義的目錄查找指令和程式。是以它是你執行指令和程式的關鍵所在。PATH環境變量如下:

[root@cdphost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin           

說明:從上面環境變量的輸出可以看出,目錄之間使用冒号(:)進行分割。當你需要加入你安裝的程式和工具的時候,一定記得要設定PATH環境變量。

使用者管理

linux安全

使用者是所有系統安全的核心,linux系統為每一個使用者配置設定唯一的UID,但是登入系統的時候我們用的是登入名,同時也會關聯一個對應的密碼。Linux系統會用特定的檔案和工具跟蹤和管理系統的使用者賬戶。

/etc/passwd檔案

Linux系統有一個專門的檔案儲存登入使用者的基本資訊,這個檔案就是/etc/passwd。該檔案是一個标準的文本檔案,你可以用任何文本編輯器打開進行手動管理操作,但是不建議這樣操作,因為如果不小心破壞了它,有可能導緻使用者無法正常登入。下面是/etc/passwd的基本例子:

[root@cdphost ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin           

/etc/passwd的字段資訊如下:

  • 登入使用者名
  • 使用者密碼
  • 使用者賬戶的UID(數字形式)
  • 使用者賬戶的組ID(GID)(數字形式)
  • 使用者賬戶的文本描述(稱為備注字段)
  • 使用者HOME目錄的位置
  • 使用者的預設shell

/etc/passwd檔案種的密碼都是x,并不是所有使用者密碼都是一樣的。是因為早期系統密碼都加密記錄在/etc/passwd。這樣不安全,會導緻很多不良用心的人暴力破解密碼。基于這個考慮,後面改為将密碼保留在單獨的一個檔案種/etc/shadow。

添加使用者(useradd)

useradd常用指令參數:

-d home_dir 指定使用者登入時的起始目錄

-g 指定使用者所屬的群組

-G 指定使用者所屬的附加群組。

-m 自動建立使用者的HOME目錄

-s shell 指定預設的登入shell

-u uid 為賬戶指定唯一的UID

例1:建立使用者user001且自動建立home目錄。

[root@cdphost ~]# useradd -m user001           

删除使用者(userdel)

n userdel常用指令參數:

-r 删除home目錄。

例子1:删除使用者user001。

[root@cdphost home]# userdel -r user001           

修改使用者密碼(passwd)

文法:passwd 使用者名。

例1:修改user001登入密碼。

[root@cdphost home]# passwd user001
Changing password for user user001.
New password:           

批量修改使用者密碼

批量修改使用者密碼:

[root@cdphost home]# echo "3" | passwd --stdin user001
Changing password for user user001.
passwd: all authentication tokens updated successfully.           

使用者組管理

/etc/group檔案

/etc/group檔案是使用者組檔案。

檢視/etc/group檔案:

[root@cdphost home]# cat /etc/group | tail -n 5
tcpdump:x:72:
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
cdp:x:1000:cdp           

輸出的字段資訊如下:

  • 組名
  • 組密碼
  • GID
  • 屬于該組的使用者清單

添加使用者組(groupadd)

文法:groupadd -g 組id 組名稱。

例1:建立組id為1050,組名為gp001的組。

[root@cdphost home]# groupadd -g 1050 gp001
[root@cdphost home]# cat /etc/group | grep gp001
gp001:x:1050:           

删除使用者組(groupdel)

文法:groupdel 組名稱

例1:删除使用者組gp001。

[root@cdphost home]# cat /etc/group | grep gp001
gp001:x:1050:
[root@cdphost home]# groupdel gp001
[root@cdphost home]# cat /etc/group | grep gp001
[root@cdphost home]#           

檔案權限

認識檔案權限

檢視檔案詳細資訊如下:

第04章-vim編輯器-環境變量-使用者檔案權限-sed與awk

上圖輸出的結果紅框内容分為4段:

第1段:1個字元代表了對象類型:

  • - 代表目錄
  • d 代表目錄
  • l 代表連結
  • c 代表字元型裝置
  • b 代表塊裝置
  • n 代表網絡裝置

第2段到第4段每3個字元為一組,分别代表了:

  • 對象的屬主權限
  • 對象的屬組權限
  • 系統其他使用者權限

具體檔案權限檢視下圖:

第04章-vim編輯器-環境變量-使用者檔案權限-sed與awk

權限二進制和八進制表現形式如下:

權限 二進制
--- 000
--x 001
-w- 010
-wx 011
r-- 100
r-x 101
rw- 110
rwx 111

預設檔案權限(umask)

我們建立檔案的時候,會預設給個權限,那麼這個權限從哪裡來呢,答案就是umask。umask用來設定檔案和目錄的預設權限。umask通常設定在檔案/ect/profile中。

umask值隻是個掩碼。它會屏蔽掉不想授予該安全級别的權限。檔案和檔案夾的預設權限等于對象的全權限減去umask值。檔案的全權限值是666(所有使用者都有讀寫權限),檔案夾的全權限值是777(所有使用者都有讀寫執行權限)。

Linux中的umask值如下:

第04章-vim編輯器-環境變量-使用者檔案權限-sed與awk

root使用者建立檔案預設權限計算如下:

-- 預設權限
檔案預設權限 666-022=644 à rw-r--r--
檔案夾預設權限 777-022=755 à rwxr-xr-x

修改檔案權限(chmod)

chmod指令用來改變檔案和目錄的安全性設定。通常可以用八進制和符号模式進行修改檔案權限。

八進制模式:chmod 777 newfile.txt

符号模式:chmod [ugoa] [+-] [rwx]

  • u 代表使用者
  • g 代表組
  • o 代表其他
  • a 代表上述所有

例1:建立newfile.txt檔案且設定權限為777。

[root@cdphost temp2]# touch newfile.txt
[root@cdphost temp2]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 14 17:01 newfile.txt
[root@cdphost temp2]# chmod 777 newfile.txt
[root@cdphost temp2]# ll
total 0
-rwxrwxrwx 1 root root 0 Aug 14 17:01 newfile.txt           

例2:newfile.txt檔案所屬使用者去掉讀權限,所屬組去掉寫權限,其它使用者去掉執行權限。

[root@cdphost temp2]# ll
total 0
-rwxrwxrwx 1 root root 0 Aug 14 17:01 newfile.txt
[root@cdphost temp2]# chmod u-r newfile.txt
[root@cdphost temp2]# chmod g-w newfile.txt
[root@cdphost temp2]# chmod o-x newfile.txt
[root@cdphost temp2]# ll
total 0
--wxr-xrw- 1 root root 0 Aug 14 17:01 newfile.txt           

例3:newfile.txt檔案所屬使用者加上讀權限,所屬組加上寫權限,其它使用者加上執行權限。

[root@cdphost temp2]# ll
total 0
--wxr-xrw- 1 root root 0 Aug 14 17:01 newfile.txt
[root@cdphost temp2]# chmod u+r newfile.txt
[root@cdphost temp2]# chmod g+w newfile.txt
[root@cdphost temp2]# chmod o+x newfile.txt
[root@cdphost temp2]# ll
total 0
-rwxrwxrwx 1 root root 0 Aug 14 17:01 newfile.txt           

例4:newfile.txt去掉檔案所屬使用者,所屬組,其它使用者的執行權限。

[root@cdphost temp2]# ll
total 0
-rwxrwxrwx 1 root root 0 Aug 14 17:01 newfile.txt
[root@cdphost temp2]# chmod a-x newfile.txt
[root@cdphost temp2]# ll
total 0
-rw-rw-rw- 1 root root 0 Aug 14 17:01 newfile.txt           

改變檔案所屬關系(chown)

chown指令改變檔案的所有者或者所有組。

例1:改變檔案ccc.txt的檔案所有者為cdp使用者。

[root@cdphost ~]# ll ccc.txt
-rw-r--r-- 1 root root 0 Sep 10 14:03 ccc.txt
[root@cdphost ~]# chown cdp ccc.txt
[root@cdphost ~]# ll ccc.txt
-rw-r--r-- 1 cdp root 0 Sep 10 14:03 ccc.txt           

例2:改變檔案ccc.txt的檔案所有者為cdp使用者,所屬組為cdp組。

[root@cdphost ~]# ll ccc.txt
-rw-r--r-- 1 root root 0 Sep 10 14:03 ccc.txt
[root@cdphost ~]# chown cdp:cdp ccc.txt
[root@cdphost ~]# ll ccc.txt
-rw-r--r-- 1 cdp cdp 0 Sep 10 14:03 ccc.txt           

例3:改變目錄ccc及其子孫所有檔案的所有者為cdp使用者,所屬組為cdp組。

[root@cdphost ~]# ll | grep ccc
drwxr-xr-x 2 root root 6 Sep 10 14:12 ccc
[root@cdphost ~]# chown -R cdp:cdp ccc
[root@cdphost ~]# ll | grep ccc
drwxr-xr-x 2 cdp cdp 6 Sep 10 14:12 ccc           

輸入輸出重定向

linux指令運作時會打開三個檔案:

标準輸入檔案(stdin):stdin的檔案描述符為0,從stdin讀取資料。

标準輸出檔案(stdout):stdout的檔案描述符為1,向stdout輸出資料。

标準錯誤檔案(stderr):stderr的檔案描述符為2,向stderr寫入錯誤資訊。

預設情況下,command > file 将 stdout 重定向到 file,command < file 将stdin 重定向到 file。

重定向指令如下:

指令 說明
command > file 将輸出重定向到 file
command >> file 将輸出以追加的方式重定向到 file
command < file 将輸入重定向到 file

Here Document

Here Document 是一種特殊的重定向方式,将輸入重定向到一個互動式 Shell 腳本或程式。

文法:

command << delimiter
document
delimiter           

注意:第二個分割串delimiter必須頂格寫,前後均不可有空格或者tab

/dev/null 檔案

/dev/null 是一個特殊的檔案,寫入到它的内容都會被丢棄;如果嘗試從該檔案讀取内容,那麼什麼也讀不到。但是 /dev/null 檔案非常有用,将指令的輸出重定向到它,會起到"禁止輸出"的效果

執行指令,不想在螢幕上輸出結果:

command > /dev/null           

執行指令,不想在螢幕上輸出結果,錯誤資訊也屏蔽:

command > /dev/null 2>&1           

說明:

0 是标準輸入,1 是标準輸出,2 是标準錯誤輸出。

2 和 > 之間不可以有空格,2> 是一體的時候才表示錯誤輸出。

例1:輸出重定向

[root@cdphost Desktop]# echo "hello" > file.txt
[root@cdphost Desktop]# cat file.txt
hello
[root@cdphost Desktop]# echo "world" > file.txt
[root@cdphost Desktop]# cat file.txt
world           

例2:輸出重定向(追加)

[root@cdphost Desktop]# echo "hello" > file.txt
[root@cdphost Desktop]# cat file.txt
hello
[root@cdphost Desktop]# echo "world" >> file.txt
[root@cdphost Desktop]# cat file.txt
hello
world           

例3:輸入重定向

[root@cdphost Desktop]# cat << eof
> hello
> world
> cdp
> eof
hello
world
cdp
[root@cdphost Desktop]#           

sed和awk

在我們日常開發中,sed更多的是基于行來處理,而awk更多的是基于列來處理。

sed

sed編輯器可以根據指令來處理資料流中的資料,這些指令要麼從指令行中輸入,要麼存儲在一個指令文本檔案中。

sed編輯器會執行操作如下:

1. 一次從輸入中讀取一行資料。

2. 根據編輯器指令比對資料。

3. 根據指令修改流中的資料。

4. 将更改後的資料輸出到STDOUT。

重點:sed編輯器不會修改文本檔案的資料,它隻會将修改後的資料發送到STDOUT,如果你檢視原來的文本檔案,它仍然保留着原始資料。

sed文法:sed [選項] [-e<script指令>] [-f<script檔案>] [文本檔案]

選項說明:

選項 說明
-e script 處理輸入時,将script中指定的指令添加到已有的指令中
-f file 處理輸入時,将file中指定的指令添加到已有的指令中
-n 僅顯示script處理後的結果
-i 表示直接修改檔案内容,請不要随便用到系統配置檔案

script動作說明:

選項 說明
s 替換:替換文本
p 列印:列印比對到的資料,通常和-n選項聯合使用
a 新增:a後面的字元串做為新行字元,指定行下一行
d 删除:删除指定行
i 插入:i後面的字元串做為新行字元,指定行上一行

例子1:通過管道符輸入sed編輯器處理,将word替換為test

[root@localhost Desktop]# echo "hello word" | sed 's/word/test/'
hello test           

例子2:使用sed編輯器處理檔案,将檔案中的word替換為test,輸出到螢幕

[root@localhost Desktop]# cat data.txt
hello word
hello word
hello word
hello word
hello word
hello word
hello word
[root@localhost Desktop]# sed 's/word/test/' data.txt
hello test
hello test
hello test
hello test
hello test
hello test
hello test           

例子3:使用sed編輯器處理檔案,将檔案中的word替換為test,hello替換為hehe輸出到螢幕

[root@localhost Desktop]# cat data.txt
hello word
hello word
hello word
hello word
hello word
hello word
hello word
[root@localhost Desktop]# sed -e 's/word/test/;s/hello/hehe/' data.txt
hehe test
hehe test
hehe test
hehe test
hehe test
hehe test
hehe test           

例子4:使用sed編輯器處理檔案,從檔案中讀取編輯器指令

[root@localhost Desktop]# cat data.sed
s/word/test/
s/hello/hehe/
[root@localhost Desktop]# cat data.txt
hello word
hello word
hello word
hello word
hello word
hello word
hello word
[root@localhost Desktop]# sed -f data.sed data.txt
hehe test
hehe test
hehe test
hehe test
hehe test
hehe test
hehe test
           

例5:替換行中所有word為test

[root@localhost Desktop]# echo "hello word word" | sed 's/word/test/'
hello test word
[root@localhost Desktop]# echo "hello word word" | sed 's/word/test/g'
hello test test           

例6:列印比對的資料行

[root@localhost Desktop]# cat data.txt
hello word
hello word
hello word
helslo woris
[root@localhost Desktop]# sed -n 's/word/test/p' data.txt
hello test
hello test
hello test           

例7:檔案的第3行新增一行,該行字元串為hhhh

[root@localhost Desktop]# cat data.txt
hello word
hello word
hello word
helslo woris
[root@localhost Desktop]# sed '3a hhhh' data.txt
hello word
hello word
hello word
hhhh
helslo woris           

例8:删除1到3行,删除3到最後一行

[root@localhost Desktop]# cat data.txt
hello word
hello word
hello word
helslo woris
[root@localhost Desktop]# sed '1,3d' data.txt
helslo woris
[root@localhost Desktop]# sed '3,$d' data.txt
hello word
hello word           

例9:檔案的第3行新增一行,改行位于上一行,該行字元串為hhhh

[root@localhost Desktop]# cat data.txt
hello word
hello word
hello word
helslo woris
[root@localhost Desktop]# sed '3i hhhh' data.txt
hello word
hello word
hhhh
hello word
helslo woris           

例10:删除第1行

[root@localhost Desktop]# cat data.txt
hello word
asfsdsfhello word
hello word
helslo woris
[root@localhost Desktop]# sed -i '1d' data.txt
[root@localhost Desktop]# cat data.txt
asfsdsfhello word
hello word
helslo woris           

例子11:最後一行新增test字元

[root@localhost Desktop]# cat data.txt
asfsdsfhello word
hello word
helslo woris
[root@localhost Desktop]# sed -i '$a test' data.txt
[root@localhost Desktop]# cat data.txt
asfsdsfhello word
hello word
helslo woris
test           

例子12:第一行新增first line字元

[root@localhost Desktop]# cat data.txt
asfsdsfhello word
hello word
helslo woris
test
[root@localhost Desktop]# sed -i '1i first line' data.txt
[root@localhost Desktop]# cat data.txt
first line
asfsdsfhello word
hello word
helslo woris
test           

例13:替換一行中的多處需要替換的字元

[root@localhost Desktop]# cat data.txt
first line
asfsdsfhello word word word
hello word hhh word
helslo woris word word
test word word
[root@localhost Desktop]# sed 's/word/test/' data.txt
first line
asfsdsfhello test word word
hello test hhh word
helslo woris test word
test test word
[root@localhost Desktop]# sed 's/word/test/g' data.txt
first line
asfsdsfhello test test test
hello test hhh test
helslo woris test test
test test test           

awk

awk編輯器可以根據指令來處理資料流中的資料,這些指令要麼從指令行中輸入,要麼存儲在一個指令文本檔案中。

awk最大的特點是提取格式化的資料檔案中的資料,分隔為單個元素,然後你又可以進行格式化輸出,awk工具在日常開發中使用相當頻繁。

舉個簡單的例子:你ls –l列出一個目錄的檔案資訊,然後拷貝檔案名。

[root@localhost Desktop]# ls -l | grep -v total | awk '{print $9}'
1.sh
2
2.sh
data.sed
data.txt
file1.txt
file.txt
temp1.txt
test001.sh
test002.sh
test003.sh
test004.sh
test005.sh
test006.sh
test007.sh
           

awk文法:awk [選項] [script] [文本檔案]。

選項說明:

選項 說明
-F 指定按照什麼分隔符進行分列改行元素
-f scriptfile 按指定的腳本檔案中的指令處理檔案

預設情況下:awk是以空白符号(空格和制表符)來分隔一行字元的。

  • $0代表整個文本行
  • $1代表文本行第1個資料元素
  • $2代表文本行第2個資料元素
  • $3代表文本行第3個資料元素
  • $n代表文本行第n個資料元素

注意:當你使用腳本指令的時候必須用花括号{}包裹指令

例1:讀取文本檔案顯示第1個字段值

[root@localhost Desktop]# cat data.txt
first line
asfsdsfhello word word word
hello word hhh word
helslo woris word word
test word word
[root@localhost Desktop]# awk '{print $1}' data.txt
first
asfsdsfhello
hello
helslo
test           

例2:列印所有系統使用者

[root@localhost Desktop]# awk -F: '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
…           

例3:改變指定列的值,然後列印

[root@localhost Desktop]# cat data.txt
a a a a a
b b b b b
c c c c c
d d d d d
[root@localhost Desktop]# awk '{$5="f";print $0}' data.txt
a a a a f
b b b b f
c c c c f
d d d d f           

例4:列印所有使用者的home目錄

[root@localhost Desktop]# cat data.awk
{
text="'s home directory is'"
print $1 text $6
}
[root@localhost Desktop]# awk -F: -f data.awk /etc/passwd
root's home directory is'/root
bin's home directory is'/bin
daemon's home directory is'/sbin
adm's home directory is'/var/adm
lp's home directory is'/var/spool/lpd           

我們有時候會在處理檔案前運作腳本,或者在處理檔案後運作腳本,這時候需要用到BEGIN和END關鍵字。

例1:使用BEGIN和END

[root@localhost Desktop]# cat data.txt
a a a a a
b b b b b
c c c c c
d d d d d
[root@localhost Desktop]# awk 'BEGIN {print "start file"} {print $0} END {print "end file"} ' data.txt
start file
a a a a a
b b b b b
c c c c c
d d d d d
end file           

awk内置變量使用方法:

變量說明:

變量 說明
FS 輸入字段分隔符,預設是空格
RS 輸入記錄分隔符,預設換行符
OFS 輸出字段分隔符,預設是空格
ORS 輸出記錄分隔符,預設換行符
FIELDWIDTHS 由空格分隔的一列數字,定義了每個資料字段的确切寬度

例子1:使用FS和OFS

[root@localhost Desktop]# cat data.txt
a,a,a,a,a
b,b,b,b,b
c,c,c,c,c
d,d,d,d,d
[root@localhost Desktop]# awk 'BEGIN {FS=",";OFS="-"} {print $1,$2,$3}' data.txt
a-a-a
b-b-b
c-c-c
d-d-d           

例子2:FS和RS的使用

[root@localhost Desktop]# cat data.txt
zhangsan
shuxue 100
yuwen 90
age 15

lisi
shuxue 80
yuwen 95
age 14

wangwu
shuxue 95
yuwen 80
age 16
[root@localhost Desktop]# awk 'BEGIN {FS="\n";RS=""} {print $1,$4}' data.txt
zhangsan age 15
lisi age 14
wangwu age 16