過濾字段,查詢替換(學習筆記)

2021-06-22 02:51:13 字數 4659 閱讀 2256

sed  做行操作

cut  取字段

awk 做列操作

我們以ifconfig

命令檢視的資訊為基礎進行實驗操作。

[root@localhost ~]# ifconfig

eth0      link encap:ethernet  hwaddr 00:0c:29:23:ab:9b  

inet addr:10.15.62.115  bcast:10.15.62.255  mask:255.255.255.0

inet6 addr: fe80::20c:29ff:fe23:ab9b/64 scope:link

up broadcast running multicast  mtu:1500  metric:1

rx packets:72662 errors:0 dropped:0 overruns:0 frame:0

tx packets:73 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000 

rx bytes:35091571 (33.4 mib)  tx bytes:11507 (11.2 kib)

lo        link encap:local loopback  

inet addr:127.0.0.1  mask:255.0.0.0

inet6 addr: ::1/128 scope:host

up loopback running  mtu:16436  metric:1

rx packets:8 errors:0 dropped:0 overruns:0 frame:0

tx packets:8 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0 

rx bytes:560 (560.0 b)  tx bytes:560 (560.0 b)

要求一:

顯示出ifconfig

中第一行中以空格為分隔符的第二列內容

[root@localhost ~]# ifconfig | sed -n '1p' | awk -f " " '  '

link

要求二:

從ifconfig

檔案中過濾出eth0

這一行,然後

以冒號為分隔符取第三段內容

[root@localhost ~]# ifconfig | grep eth0 | awk -f ":" '  ' 0c

[root@localhost ~]# ifconfig | grep eth0 | cut -d ":" -f3 0c

要求三:

ifconfig

去除eth0

這一行內容,顯示餘下的所有內容

[root@localhost ~]# ifconfig | grep -v eth0

inet addr:10.15.62.115  bcast:10.15.62.255  mask:255.255.255.0

inet6 addr: fe80::20c:29ff:fe23:ab9b/64 scope:link

up broadcast running multicast  mtu:1500  metric:1

rx packets:139927 errors:0 dropped:0 overruns:0 frame:0

tx packets:328 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000 

rx bytes:67824283 (64.6 mib)  tx bytes:39241 (38.3 kib)

lo        link encap:local loopback  

inet addr:127.0.0.1  mask:255.0.0.0

inet6 addr: ::1/128 scope:host

up loopback running  mtu:16436  metric:1

rx packets:8 errors:0 dropped:0 overruns:0 frame:0

tx packets:8 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0 

rx bytes:560 (560.0 b)  tx bytes:560 (560.0 b)

要求四:

只顯示ifconfig

中第一行內容

[root@localhost ~]# ifconfig | sed -n "1p"

eth0      link encap:ethernet  hwaddr 00:0c:29:23:ab:9b 

要求五:

顯示ifconfig

中1--4

行的內容

[root@localhost ~]# ifconfig | sed -n "1,4p"

eth0      link encap:ethernet  hwaddr 00:0c:29:23:ab:9b  

inet addr:10.15.62.115  bcast:10.15.62.255  mask:255.255.255.0

inet6 addr: fe80::20c:29ff:fe23:ab9b/64 scope:link

up broadcast running multicast  mtu:1500  metric:1

要求六:

查詢替換字段(

將第一行中的eth0

替換成eth1

),並篩選顯示修改過的第一行內容

[root@localhost ~]# ifconfig | sed '1 s/eth0/eth1/g' | sed -n "1p"

eth1      link encap:ethernet  hwaddr 00:0c:29:23:ab:9b  

要求七:

查詢替換字段(

將所有行中的eth0

替換成eth1

),並篩選顯示修改過的第一行內容

[root@localhost ~]# ifconfig | sed '1,$s/eth0/eth1/g' | sed -n '1p'

eth1      link encap:ethernet  hwaddr 00:0c:29:23:ab:9b  

find 目錄 

-name 

要查詢的檔案

find 目錄 

-name 

要查詢的檔案 

-exec 

查詢命令

{}\;

find [-path……

] -options [-print -exec -ok]

path:要查詢的目錄路徑,~表示

$home

目錄,. 

表示當前目錄

, / 

根目錄

print:表示將結果輸出到標準輸出;

exec:對匹配的檔案執行該引數給出的

shell

命令;

比如:形式為command {} \;

,注意{}與\;

之間有空格

ok:與

exec

作用相同,執行命令之前,會給出確認提示;

options:表示以哪種格式查詢

比如:-name 按照名字查詢;

-perm:安裝許可權查詢;

-user:檔案屬主查詢;

-prune:不再當前指定的目錄下查詢;

-group:檔案所屬組查詢;

-type:檔案型別查詢;

-size:檔案大小查詢;

例: 以檔案型別查詢:

find 目錄 

-type 

d-name 目錄名字 如:

[root@localhost home]# find /home -type d -name redisma

/home/redisma

find 目錄 

-type

f-name 檔案名字

[root@localhost home]# find /home -type f -name aa.sh

/home/aa.sh

刪除前提示確認 

注意{}與\;之間有空格 :

[root@localhost home]# find /home -type d -name "redis*" 

-ok rm {} \;

< rm ... /home/redissa > ? n

< rm ... /home/redisma > ? n

n表示no,不刪除,y表示

yes,確認刪除

查到需要的內容後,將所需要的資訊移動到另外乙個目錄下:

[root@localhost home]# find /home -type d -name "redis*"

-exec cp {} /opt \;

cp: omitting directory `/home/redissa'

cp: omitting directory `/home/redisma'

查詢到redis

開頭的資訊之後,複製到

/opt

目錄下

層次查詢 行列轉換學習筆記

level 語法 select level column,expr.from table where condition start with condition connect by prior column1 column2 column1 prior column2 舉例 按照級別對emp員工...

Hive array欄位 查詢過濾

如果乙個hive 表的乙個字段 是array型別,裡面存在多個值,如果要對其中的某個值進行過濾,那麼可以用lateral view 結合udtf user defined table generating functions 來進行過濾 lateral view語法 lateralview late...

CSS變形轉換 學習筆記

css的變形轉換 屬性 transform 一 translate 位移 常用 該屬性值有三種型別 translatex translatey 和 translate。translate 可以作用於已經執行了 絕對定位 position absolute 的元素,而要用 position 已經設定為...