1. 程式人生 > >grep sed awk

grep sed awk

hive 多個 添加 echo 行處理 zha grep sed data

grep命令用於查找文件裏符合條件的字符串。

[root@host tmpdata]# grep ‘shenzhen‘ *.txt
hivelog.txt:tianyongtao 1 50 shenzhen
hivelog.txt:wangwu 1 85 shenzhen
hivelog.txt:zhangsan 1 20 shenzhen
hivelog.txt:liuqin 0 56 shenzhen
hivelog.txt:wangwu 0 47 shenzhen
hivelog.txt:liuyang 1 32 shenzhen

[root@host tmpdata]# grep ‘shenzhen‘ hivelog.txt
tianyongtao 1 50 shenzhen
wangwu 1 85 shenzhen
zhangsan 1 20 shenzhen
liuqin 0 56 shenzhen
wangwu 0 47 shenzhen
liuyang 1 32 shenzhen

[root@host tmpdata]# grep ‘shenzhen‘ hivelog.txt hivelog1.txt
hivelog.txt:tianyongtao 1 50 shenzhen
hivelog.txt:wangwu 1 85 shenzhen
hivelog.txt:zhangsan 1 20 shenzhen
hivelog.txt:liuqin 0 56 shenzhen
hivelog.txt:wangwu 0 47 shenzhen
hivelog.txt:liuyang 1 32 shenzhen
hivelog1.txt:tianyongtao 1 50 shenzhen
hivelog1.txt:wangwu 1 85 shenzhen
hivelog1.txt:zhangsan 1 20 shenzhen
hivelog1.txt:liuqin 0 56 shenzhen
hivelog1.txt:wangwu 0 47 shenzhen
hivelog1.txt:liuyang 1 32 shenzhen

[root@host tmpdata]# grep ‘shenzhen‘ hivelog.txt hivelog1.txt |grep ‘wangwu‘
hivelog.txt:wangwu 1 85 shenzhen
hivelog.txt:wangwu 0 47 shenzhen
hivelog1.txt:wangwu 1 85 shenzhen
hivelog1.txt:wangwu 0 47 shenzhen

sed命令是利用script來處理文本文件。

sed可依照script的指令,來處理、編輯文本文件。

Sed主要用來自動編輯一個或多個文件;簡化對文件的反復操作;編寫轉換程序等。

sed :實現數據的替換,刪除,增加,選取等(以行為單位進行處理)

[root@host tmpdata]# cat tian.txt
name tian
[root@host tmpdata]# sed -i "1i/sex 1" tian.txt //第一行添加一條記錄
[root@host tmpdata]# cat tian.txt
/sex 1
name tian
[root@host tmpdata]# sed -i "1isex 1" tian.txt //第一行添加一條記錄
[root@host tmpdata]# cat tian.txt
sex 1
/sex 1
name tian
[root@host tmpdata]# sed -i "2ilevel 45" tian.txt //第二行添加一條記錄
[root@host tmpdata]# cat tian.txt
sex 1
level 45
/sex 1
name tian

[root@host tmpdata]# echo "home henan">>tian.txt //尾部添加一條記錄
[root@host tmpdata]# cat tian.txt
sex 1
level 45
/sex 1
name tian
home henan

sed並不會修改原文件中的內容,除非重定向新文件

[root@host tmpdata]# cat tian.txt
sex 1
level 45
/sex 1
name tian
home henan
[root@host tmpdata]# sed ‘3d‘ tian.txt //刪除第三行內容
sex 1
level 45
name tian
home henan

[root@host tmpdata]# sed ‘3,5d‘ tian.txt//刪除3-5行的記錄
sex 1
level 45

$表示最後,末尾

[root@host tmpdata]# sed ‘$d‘ tian.txt //刪除最後一行
sex 1
level 45
/sex 1
name tian

[root@host tmpdata]# sed -i ‘$i age 85‘ tian.txt //最後一行位置添加一條記錄,原來的最後一行推後
[root@host tmpdata]# cat tian.txt
sex 1
level 45
/sex 1
name tian
age 85
home henan

grep sed awk