shell工具之一:sed
sed基礎
sed編輯器被稱作流編輯器,與常見的交互式文本編輯器剛好相反。文本編輯器可以通過鍵盤來交互式地插入、刪除、替換文本中的數據;而流編輯器是基於一組預先的規則來編輯數據流。
sed命令的格式如下:
sed options script file
選項 |
說明 |
-e script |
將script中指定的命令添加到運行的命令中 |
-f file |
將file中指定的命令添加到運行的命令中 |
-n |
不為每個命令生成輸出,等待print命令來輸出 |
說明:
script用於指定作用在數據量上的單個命令。
如果需要使用多個命令,有兩種選擇:可以在命令行中使用-e選項指定,不同命令之間用分號隔開;或者使用-f選項在文件中指定。
默認情況下,sed編輯器將指定的命令應用到STDIN輸入流上,而不作用於數據源本身,就是說sed不會修改文本文件中的原數據。
1 替換命令substitute
s/pattern/replacement/flags
flags取值如下:
數字:表示replacement將替換每行中第幾次出現的pattern
g:表示replacement將替換所有出現的pattern
p:打印用replacement替換過的行(經常與-n選項搭配使用,-n禁止sed輸出,而p會輸出修改過的行,二者搭配則僅輸出被replacement替換過的行)
w file:將替換的結果寫到file文件中(只有被替換過的行才會保存到輸出文件file中)
[root@benxintuzi shell]# cat data1 benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin [root@benxintuzi shell]# sed 's/benxin/tuzi/3' data1 benxin benxin tuzi benxin benxin benxin benxin tuzi benxin benxin benxin benxin tuzi benxin benxin [root@benxintuzi shell]# sed 's/benxin/tuzi/g' data1 tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi [root@benxintuzi shell]# cat data2 benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin hello hello hello hello hello hello [root@benxintuzi shell]# sed -n 's/benxin/tuzi/p' data2 tuzi benxin benxin benxin benxin tuzi benxin benxin benxin benxin tuzi benxin benxin benxin benxin [root@benxintuzi shell]# sed 's/benxin/tuzi/w out' data2 tuzi benxin benxin benxin benxin tuzi benxin benxin benxin benxin tuzi benxin benxin benxin benxin hello hello hello hello hello hello [root@benxintuzi shell]# cat out tuzi benxin benxin benxin benxin tuzi benxin benxin benxin benxin tuzi benxin benxin benxin benxin
2 指定作用行
默認情況下,sed會作用於所有行,如果只想作用於特定行,必須使用行尋址,格式如下:
[address]command或者
address
{
command1
command2
command3
...
}
address可以使用單個行號,或者是起始行號、逗號、結尾行號指定。
[root@benxintuzi shell]# sed '2s/benxin/tuzi/' data1 benxin benxin benxin benxin benxin tuzi benxin benxin benxin benxin benxin benxin benxin benxin benxin [root@benxintuzi shell]# sed '2,$s/benxin/tuzi/' data1 benxin benxin benxin benxin benxin tuzi benxin benxin benxin benxin tuzi benxin benxin benxin benxin [root@benxintuzi shell]# sed '2,${ s/benxin/tuzi/3 s/hello/world/2 }' data2 benxin benxin benxin benxin benxin benxin benxin tuzi benxin benxin benxin benxin tuzi benxin benxin hello world hello hello hello hello
delete命令會刪除指定模式的所有行,如果沒有加入行尋址,則會刪除流中的所有文本(並不修改原文件)。
3 插入和追加
insert命令i在指定行前增加一個新行;
append命令a在指定行后增加一個新行。
格式如下:
sed '[address]command\new line'
[root@benxintuzi shell]# cat data1 benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin [root@benxintuzi shell]# sed '3i\tuzi tuzi tuzi' data1 benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin tuzi tuzi tuzi benxin benxin benxin benxin benxin [root@benxintuzi shell]# sed '3a\tuzi tuzi tuzi' data1 benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin tuzi tuzi tuzi
4 轉換命令transform
轉換命令y可以處理單個字符,格式如下:
[address]y/inchars/outchars/
用outchars的第一個字符替換inchars的第一個字符,outchars的第二個字符替換inchars的第二個字符,...,如果inchars和outchars的長度不同,則sed編輯器產生一個錯誤。
[root@benxintuzi shell]# sed 'y/bn/ti/' data1 teixii teixii teixii teixii teixii teixii teixii teixii teixii teixii teixii teixii teixii teixii teixii
5 打印命令
p: 打印文本行
=: 打印行號
l: 打印文本行和不可打印的ASCII字符
[root@benxintuzi shell]# cat data2 benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin hello hello hello hello hello hello [root@benxintuzi shell]# sed -n '2,4p' data2 benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin hello hello hello hello hello hello [root@benxintuzi shell]# sed '=' data2 1 benxin benxin benxin benxin benxin 2 benxin benxin benxin benxin benxin 3 benxin benxin benxin benxin benxin 4 hello hello hello hello hello hello [root@benxintuzi shell]# sed -n '/hello/{ > = > p > }' data2 4 hello hello hello hello hello hello [root@benxintuzi shell]# sed -n 'l' data2 benxin benxin benxin benxin benxin$ benxin benxin benxin benxin benxin$ benxin benxin benxin benxin benxin$ hello hello hello hello hello hello$
6 文件命令
[address]w filename
[address]r filename
[root@benxintuzi shell]# cat data3 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]# sed -n '3,4w outfile' data3 [root@benxintuzi shell]# cat outfile This is the line 3 This is the line 4 [root@benxintuzi shell]# sed '1r outfile' data3 This is the line 1 This is the line 3 This is the line 4 This is the line 2 This is the line 3 This is the line 4
sed進階
1 多行命令
N: 將數據流中的下一行加進來創建一個多行組
D: 刪除多行組中的第一行
P: 打印多行組中的第一行
[root@benxintuzi shell]# cat data3 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]# sed '/line 2/{N; s/line/number/g}' data3 This is the line 1 This is the number 2 This is the number 3 This is the line 4
2 跳轉命令
[address]b [label]
address決定了哪些行會觸發跳轉命令;label定義了要跳轉的位置,如果沒有label參數,那么將跳轉到腳本的末尾。定義label:開始,最多可以有7個字符。
[root@benxintuzi shell]# cat data3 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]# sed '{2,3b; s/line/number/g}' data3 This is the number 1 This is the line 2 This is the line 3 This is the number 4 [root@benxintuzi shell]# sed '{ 2,3b s/line/number/g :label s/the/a/g }' data3 This is a number 1 This is the line 2 This is the line 3 This is a number 4
3 測試命令
[address]t [label]
測試命令的跳轉不是基於地址,而是基於替換命令是否成功。如下程序每次去除一個逗號,直到一個逗號都沒有時結束循環跳轉。
[root@benxintuzi shell]# echo "This,is,a,test,to,remove,commas." | sed -n '{ :start s/,/ /p t start }' This is,a,test,to,remove,commas. This is a,test,to,remove,commas. This is a test,to,remove,commas. This is a test to,remove,commas. This is a test to remove,commas. This is a test to remove commas.
4 模式替換
在行The cat sleeps in a hat中,如果想將cat和hat都加上引號,如何做呢?sed中可以使用&表示找到的匹配模式:
[root@benxintuzi shell]# echo "The cat sleeps in a hat" | sed 's/.at/".at"/g' The ".at" sleeps in a ".at" [root@benxintuzi shell]# echo "The cat sleeps in a hat" | sed 's/.at/"&"/g' The "cat" sleeps in a "hat"
5 sed實例工具
# 加倍行間距G:$!G表示最后一行不加倍 [root@benxintuzi shell]# cat data3 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]# sed '$!G' data3 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]#
# 行編號工具: [root@benxintuzi shell]# cat data3 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]# sed '=' data3 | sed 'N; s/\n/\t/' 1 This is the line 1 2 This is the line 2 3 This is the line 3 4 This is the line 4
# 刪除多余的空白行: # 關鍵在於創建一個包含非空白行和一個緊挨空白行的地址區間,匹配該區間則不執行刪除。 # /./,/^$/!d,/./表示至少包含一個字符的行,/^$/表示一個空行: [root@benxintuzi shell]# cat data4 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]# sed '/./,/^$/!d' data4 This is the line 1 This is the line 2 This is the line 3 This is the line 4
# 刪除開頭的空行: [root@benxintuzi shell]# cat data5 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]# sed '/./,$!d' data5 This is the line 1 This is the line 2 This is the line 3 This is the line 4
# 刪除結尾的空行: [root@benxintuzi shell]# cat data6 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]# sed '{ > :start > /^\n*$/{$d; N; b start} > }' data6 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]#
shell工具之二:gawk
gawk基礎
gawk程序是unix中原始awk程序的GNU版本。gawk提供了一種編程環境,允許修改和重新組織文件中的數據。格式如下:
gawk options program file
選項 |
說明 |
-F fs |
指定行中分隔數據字段的分隔符 |
-f file |
指定gawk程序的文件名 |
-v var=value |
定義gawk程序中的一個變量及其默認值 |
-mf N |
指定要處理的數據文件中的最大字段數 |
-mr N |
指定要處理的數據文件中的最大行數 |
-W keyword |
指定gawk的兼容模式或警告等級 |
gawk程序使用的腳本命令必須放在用單引號括起來的花括號中:
gawk '{print "hello benxintui"}'
gawk在處理文本文件時,自動為行中的每個數據字段分配一個變量:
"$0"表示整行;
"$1"表示行中第1個字段;
"$2"表示行中第2個字段;
...
"$n"表示行中第n個字段。
gawk還可以指定程序何時運行。
在處理數據前運行腳本,可以使用BEGIN關鍵字:
[root@benxintuzi shell]# cat data3 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]# gawk 'BEGIN{print "The data3 File Contents:"} {print $0}' data3 The data3 File Contents: This is the line 1 This is the line 2 This is the line 3 This is the line 4
在處理數據后運行腳本,可以使用END關鍵字:
[root@benxintuzi shell]# gawk 'BEGIN{print "The data3 File Contents:"} {print $0} END{print "End of File"}' data3 The data3 File Contents: This is the line 1 This is the line 2 This is the line 3 This is the line 4 End of File
gawk進階
1 使用變量
內置變量:
1 字段和行分隔變量
變量 |
說明 |
FS |
輸入字段分隔符(默認為空格) |
RS |
輸入行分隔符(默認為換行符) |
OFS |
輸出字段分隔符(默認為空格) |
ORS |
輸出行分隔符(默認為換行符) |
[root@benxintuzi shell]# cat data7 This,is,the,line,1 This,is,the,line,2 This,is,the,line,3 This,is,the,line,4 [root@benxintuzi shell]# gawk 'BEGIN{FS=","; OFS="<-->"; RS="\n"; ORS="|\n"} {print $1,$2,$3,$4,$5}' data7 This<-->is<-->the<-->line<-->1| This<-->is<-->the<-->line<-->2| This<-->is<-->the<-->line<-->3| This<-->is<-->the<-->line<-->4| [root@benxintuzi shell]# gawk 'BEGIN{FS=","; OFS="<-->"; RS="\n"; ORS="|\n"} {print $0}' data7 This,is,the,line,1| This,is,the,line,2| This,is,the,line,3| This,is,the,line,4|
2 數據變量
變量 |
說明 |
ARGC |
當前命令行參數個數 |
ARGV |
當前命令行參數數組 |
ARGIND |
當前文件在ARGV中的位置 |
CONVFMT |
數字的轉換格式,默認為%.6g |
OFMT |
數字的輸出格式,默認為%.6g |
ENVIRON |
當前shell環境變量及其值組成的關聯數組 |
ERRNO |
錯誤號 |
FILENAME |
數據文件名 |
FNR |
數據文件中的當前行數 |
NF |
數據文件中每行的字段數 |
NR |
已處理的數據行數(如果同時處理多個文件,則該值會不斷累加) |
IGNORECASE |
當其值非0時,忽略gawk命令中字符串的大小寫 |
RLENGTH |
由match匹配的子字符串的長度 |
RSTART |
由match匹配的子字符串的起始位置 |
[root@benxintuzi shell]# gawk 'BEGIN{print ARGC, ARGV[0], ARGV[1]}' data7 2 gawk data7 [root@benxintuzi shell]# gawk 'BEGIN{print ENVIRON["HOME"]; print ENVIRON["PATH"]}' /root /usr/lib/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/java/jdk1.7.0_75/bin:/usr/java/jdk1.7.0_75/jre/bin:/bigdata/hadoop-2.6.0/bin:/bigdata/hadoop-2.6.0/sbin:/usr/maven/apache-maven-3.3.3/bin:/home/benxintuzi/bin [root@benxintuzi shell]# gawk ' BEGIN{FS=","} {print "FILENAME="FILENAME, "ARGIND="ARGIND, "NF="NF", FNR="FNR, "NR="NR}' data7 data7 FILENAME=data7 ARGIND=1 NF=5, FNR=1 NR=1 FILENAME=data7 ARGIND=1 NF=5, FNR=2 NR=2 FILENAME=data7 ARGIND=1 NF=5, FNR=3 NR=3 FILENAME=data7 ARGIND=1 NF=5, FNR=4 NR=4 FILENAME=data7 ARGIND=2 NF=5, FNR=1 NR=5 FILENAME=data7 ARGIND=2 NF=5, FNR=2 NR=6 FILENAME=data7 ARGIND=2 NF=5, FNR=3 NR=7 FILENAME=data7 ARGIND=2 NF=5, FNR=4 NR=8
自定義變量:
gawk變量名區分大小寫。
[root@benxintuzi shell]# gawk ' BEGIN{ testing="This is a test" print testing testing=45 print testing }' This is a test 45 # 在命令行中給gawk變量賦值: [root@benxintuzi shell]# cat script1 BEGIN{FS=","} {print $n} [root@benxintuzi shell]# gawk -f script1 n=2 data7 is is is is [root@benxintuzi shell]# gawk -f script1 n=5 data7 1 2 3 4
2 操作數組
# 數組的定義 [root@benxintuzi shell]# gawk ' > BEGIN{ > capital["China"]="beijing" > print capital["China"] > var[1]=20 > var[2]=10 > total=var[1] + var[2] > print total > }' beijing 30 # 數組的遍歷 [root@benxintuzi shell]# gawk ' BEGIN{ var["a"]=1 var["b"]=2 var["c"]=3 for (dex in var) { print "Index:" dex " --- " "Value:" var[dex] } }' Index:a --- Value:1 Index:b --- Value:2 Index:c --- Value:3 # 數組元素的刪除 [root@benxintuzi shell]# gawk ' BEGIN{ var["a"]=1 var["b"]=2 var["c"]=3 for (dex in var) { print "Index:" dex " --- " "Value:" var[dex] } delete var["b"] print "-------------------" for (dex in var) { print "Index:" dex " --- " "Value:" var[dex] } }' Index:a --- Value:1 Index:b --- Value:2 Index:c --- Value:3 ------------------- Index:a --- Value:1 Index:c --- Value:3
3 使用模式
使用正則表達式來匹配時,表達式必須放到作用腳本的花括號左邊:
[root@benxintuzi shell]# cat data7 This,is,the,line,1 This,is,the,line,2 This,is,the,line,3 This,is,the,line,4 [root@benxintuzi shell]# gawk 'BEGIN{FS=","} /,2/{print $0}' data7 This,is,the,line,2
使用匹配操作符˜來匹配數據行中的特定字段,如找出以1開頭的第二個字段的行,如下所示:
[root@benxintuzi shell]# cat out1 This is a test program 2684 Loop 1 Loop 2 Loop 3 Loop 4 Loop 5 Loop 6 Loop 7 Loop 8 Loop 9 Loop 10 This is the end of program [root@benxintuzi shell]# gawk '$2 ~ /^1/{print $0}' out1 Loop 1 Loop 10
還可以使用比較表達式(== / <= / < / >= / >)來匹配,如顯示所有屬於root用戶組的系統用戶(組ID==0):
[root@benxintuzi shell]# 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 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin pulse:x:498:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin apache:x:48:48:Apache:/var/www:/sbin/nologin saslauth:x:497:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin benxintuzi:x:500:500:CentOS:/home/benxintuzi:/bin/bash
[root@benxintuzi shell]# gawk -F: '$4 == 0{print $1}' /etc/passwd root sync shutdown halt operator
4 結構化命令
if格式: if (condition) { statements } else { statements } |
if (condition) statements; else statements
|
while格式: while (condition) { statements } |
do { statements } while (condition) |
for格式: for (assignments; condition; iteration) |
|
5 內置函數
數學函數 |
說明 (采用弧度為單位) |
atan2(x, y) |
x/y的反正切 |
cos(x) |
x的余弦值 |
exp(x) |
x的指數函數 |
int(x) |
x的整數部分 |
rand() |
0~1的隨機浮點數 |
sin(x) |
x的正弦值 |
sqrt(x) |
x的平方根 |
srand(x) |
種子函數 |
位操作函數 |
說明 |
and(v1, v2) |
v1與v2按位與 |
or(v1, v2) |
v1與v2按位或 |
compl(val) |
對val求補 |
lshift(val, count) |
將val左移count位 |
rshift(val, count) |
將val右移count位 |
xor(v1, v2) |
v1與v2按位異或 |
字符串函數 |
說明 |
asort(s [, d]) |
對數組s按數據元素排序。排序后數組的索引值變為表示順序的數字。如果指定了d,則將排序后的數組存儲到d中 |
asorti(s [, d]) |
對數組s按索引值進行排序。排序后數組的元素為表示順序的索引值。如果指定了d,則將排序后的數組存儲到d中 |
gensub(r, s, h [, t]) |
查找變量$0或者目標字符串t來匹配正則表達式r。如果h以g/G開頭,則用s替換掉匹配的文本;如果h為數字,表示要替換掉第幾處匹配的文本 |
gsub(r, s [, t]) |
查找變量$0或者目標字符串t來匹配正則表達式r。如果找到了,就全部替換為字符串s |
index(s, t) |
返回t在s中的索引值。如果沒找到,則返回0 |
length([s]) |
返回字符串s的長度。如果沒有指定s,則返回$0的長度 |
match(s, r, [, a]) |
返回字符串s中正則表達式r位置的索引值。如果指定了a,則將s中匹配r的部分存儲到a中 |
split(s, a [, r]) |
將s用FS字符或者r分隔開存儲到a中,返回被分割的行數 |
sprintf(format, variables) |
類似於printf |
sub(r, s [, t]) |
在變量$0或者目標字符串t中查找正則表達式r的匹配,如果找到了,就用s替換掉第一處匹配 |
substr(s, i [, n]) |
返回s中從索引值i開始的n個字符構成的字符串。如果未指定n,則返回i開始的所有部分 |
tolower(s) |
將s中的所有字符轉換為小寫 |
toupper(s) |
將s中的所有字符轉換為大寫 |
# 按數組元素排序 [root@benxintuzi shell]# gawk ' BEGIN{ var["d"]=4 var["b"]=3 var["a"]=2 var["c"]=1 asort(var, result) for (i in result) print "index:" i " <---> " "value:" result[i] }' index:4 <---> value:4 index:1 <---> value:1 index:2 <---> value:2 index:3 <---> value:3 # 按數組索引排序 [root@benxintuzi shell]# gawk ' BEGIN{ var["d"]=4 var["b"]=3 var["a"]=2 var["c"]=1 asorti(var, result) for (i in result) print "index:" i " <---> " "value:" result[i] }' index:4 <---> value:d index:1 <---> value:a index:2 <---> value:b index:3 <---> value:c [root@benxintuzi shell]# cat data7 This,is,the,line,1 This,is,the,line,2 This,is,the,line,3 This,is,the,line,4 [root@benxintuzi shell]# gawk ' BEGIN{FS=","} { number=split($0, var) print($number, var[1], var[2], var[3], var[4], var[5]) }' data7 1 This is the line 1 2 This is the line 2 3 This is the line 3 4 This is the line 4 |
|
時間函數 |
說明 |
mktime(datespec) |
將一個YYYY MM DD HH MM SS [DST]格式的日期轉換成時間戳 |
strftime(format [, timestamp]) |
將當前時間的時間戳轉換為shell函數date()的格式 |
systime() |
返回當前時間的時間戳 |
[root@benxintuzi shell]# gawk ' > BEGIN{ > date=systime() > day=strftime("%A %B %d %Y", date) > print day > }' Sunday August 16 2015 |
6 自定義函數
格式:
function name ([variables])
{
statements
}
函數的定義必須出現在所有代碼塊之前,包括BEGIN塊。如果將函數放在庫文件中定義,則在gawk中使用時,利用-f選項指定庫文件。但是如果需要同時通過-f指定腳本文件名,則利用多次-f即可:
# 在命令行中定義函數 [root@benxintuzi shell]# gawk ' function myfunc() { print($1, $2, $5) } BEGIN{FS=","} { myfunc() }' data7 This is 1 This is 2 This is 3 This is 4 # 在庫文件中定義函數 [root@benxintuzi shell]# cat funclib function myfunc() { print($1, $2, $5) } [root@benxintuzi shell]# cat script2 BEGIN{FS=","; RS="\n"} { myfunc() } [root@benxintuzi shell]# gawk -f funclib -f script2 data7 This is 1 This is 2 This is 3 This is 4