awk
详细参数说明awk '{print $3,$4}' /proc/net/arp
把arp文件的第三行和第四行,打印出来,可以重定向到文件
root@OpenWrt:/tmp# awk '{print $3,$4}' /proc/net/arp >/tmp/net_arp_list
root@OpenWrt:/tmp# cat net_arp_list
HW type
0x2 28:ed:6a:e9:ab:fb
0x2 00:e0:4c:68:00:03
0x2 a4:44:d1:d6:d2:3f
root@OpenWrt:/tmp# cd /tmp/
root@OpenWrt:/tmp# ls
802.mt7603e.log extroot net_arp_list
802.mt7612e.log fastcgi.socket-0 overlay
root@OpenWrt:/tmp# cat /proc/net/arp
IP address HW type Flags HW address Mask Device
192.168.0.30 0x1 0x2 28:ed:6a:e9:ab:fb * br-lan
192.168.0.188 0x1 0x2 00:e0:4c:68:00:03 * br-lan
192.168.0.250 0x1 0x2 a4:44:d1:d6:d2:3f * br-lan
root@OpenWrt:/tmp# awk '{print $3,$4}' /proc/net/arp
HW type
0x2 28:ed:6a:e9:ab:fb
0x2 00:e0:4c:68:00:03
0x2 a4:44:d1:d6:d2:3f
awk -F ‘:’,以冒号为分隔符
root@OpenWrt:/tmp# cat Update_status
version=2
=0.0.99
DTBVersion=
remote=9
IDUVersion=0.0.99
DTBVersion=
root@OpenWrt:/tmp# cat Update_status | grep IDUVersion
IDUVersion=0.0.99
root@OpenWrt:/tmp# cat Update_status | grep IDUVersion | awk -F '=' '{print $2}'
0.0.99
root@OpenWrt:/tmp#
root@OpenWrt:/tmp#
root@OpenWrt:~#
root@OpenWrt:~# cat /tmp/Update_status
version=2
remote=9
remote=9
IDUVersion=0.0.99
IDUVersion=0.0.99
DTBVersion=
DTBVersion=
root@OpenWrt:~# cat /tmp/Update_status | grep IDUVersion
IDUVersion=0.0.99
IDUVersion=0.0.99
root@OpenWrt:~#
root@OpenWrt:~# cat /tmp/Update_status | grep IDUVersion | awk -F '=' '{print $2}'
0.0.99
0.0.99
root@OpenWrt:~# cat /tmp/Update_status | grep IDUVersion | awk -F '=' '{print $2}' | awk 'NR==1 {print}'
0.0.99
root@OpenWrt:~#
root@OpenWrt:~#
root@OpenWrt:~# cat /tmp/Update_status | grep IDUVersion | awk -F '=' 'NR==1 {print $2}'
0.0.99
root@OpenWrt:~#
NR - Number of Record - 当前处理的行是第几行(因为awk是流处理工具,一行一行处理的,所以NR在不停的自增1)
FNR - File Number of Record - 当前处理的行是当前处理文件的第几行
NF - Number of Fileds - 当前行有多少列数据(这个在每行都会根据设定的分割符重新计算,默认分割符是任意连续的多个空白符)
cut
详细参数说明 cut -d ‘空格’ -f 1
以 ‘空格’ 为分隔符,选取第一个域里面的内容,输出
root@OpenWrt:/tmp# grep "28:ed:6a:e9:ab:fb" /tmp/net_arp_list
0x2 28:ed:6a:e9:ab:fb
root@OpenWrt:/tmp# grep "28:ed:6a:e9:ab:fb" /tmp/net_arp_list | cut -d ' ' -f 1
0x2
root@OpenWrt:/tmp#
将一行内容,去回车,拼成字符串
40007: 65
40008: 43
40009: 21
拼成:654321
root@OpenWrt:~# flash -r 0x40007 -c 3 | awk '{print $2}' | sed ':a ; N;s/\n// ;t a ;'
654321
root@OpenWrt:~# flash -r 0x40007 -c 3
40007: 65
40008: 43
40009: 21
root@OpenWrt:~# flash -r 0x40007 -c 3 | awk '{print $2}'
65
43
21
root@OpenWrt:~# flash -r 0x40007 -c 3 | awk '{print $2}' | sed ':a ; N;s/\n// ;t a ;'
654321
root@OpenWrt:~#
sed
1.sed 匹配字段后,在其前后,添加文本方法
sed -i “/匹配字段/i\加上想要插入到匹配字段前一行的内容” 文件名
红色的i是添加到前面,a是添加到后面
root@OpenWrt:~# cat /etc/config/8192eewpa
ctrl_interface_group=0
ap_scan=1
network={
ssid="lin"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP TKIP
group=CCMP TKIP
psk="11223355"
}
root@OpenWrt:~# sed -i "/psk/i\ bssid=11.22.33" /etc/config/8192eewpa
root@OpenWrt:~# cat /etc/config/8192eewpa
ctrl_interface_group=0
ap_scan=1
network={
ssid="lin"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP TKIP
group=CCMP TKIP
bssid=11.22.33
psk="11223355"
}
root@OpenWrt:~#
2.替换某行的内容
把第9行的内容替换为" 空格 bssid=22.33.44.55.66"
sed -i '9c\ bssid=22.33.44.55.66' /etc/config/8192eewpa
举例:
root@OpenWrt:~# cat /etc/config/8192eewpa
ctrl_interface_group=0
ap_scan=1
network={
ssid="lin"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP TKIP
group=CCMP TKIP
bssid=11.22.33.44.55
psk="11223355"
}
root@OpenWrt:~#
root@OpenWrt:~# sed -i '9c\ bssid=22.33.44.55.66' /etc/config/8192eewpa
root@OpenWrt:~#
root@OpenWrt:~# cat /etc/config/8192eewpa
ctrl_interface_group=0
ap_scan=1
network={
ssid="lin"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP TKIP
group=CCMP TKIP
bssid=22.33.44.55.66
psk="11223355"
}
root@OpenWrt:~#
3.删除匹配行和替换
替换匹配行
sed -i 's/要匹配的字符串.*/替换为的字符串/g' demo
-i 表示操作在源文件上生效
root@OpenWrt:/etc/uplink# cat test1
ctrl_interface_group=0
ap_scan=1
network={
ssid=bbbbbbbb
proto=RSN
key_mgmt=NONE
}
root@OpenWrt:/etc/uplink# sed 's/ssid=.*/ssid='eeeeee'/g' test1
ctrl_interface_group=0
ap_scan=1
network={
ssid=eeeeee
proto=RSN
key_mgmt=NONE
}
root@OpenWrt:/etc/uplink#
删除以a开头的行
sed -i '/^a.*/d' tmp.txt
替换文件中的某个字符串为替它字符串
文件内容:
#ukui embedded 的config.
#当Key是response,value是true时响应Windows按钮按键,false则不响应
[winKeyResponse]
response=true
--------------------------------------------
命令:
sed "/^response/c response=false" /etc/ukui/lite-config/ukui_menu_config.ini
备注:#替换文件/etc/ukui/lite-config/ukui_menu_config.ini中的response=true为response=false。加^表示行首
--------------------------------------------
执行后:
#ukui embedded 的config.
#当Key是response,value是true时响应Windows按钮按键,false则不响应
[winKeyResponse]
response=false
删除一个字符串中的部分字符
root@OpenWrt:/proc/wlan0# cat /tmp/dhcp.leases | awk '{print $3,$4,$2}'
192.168.1.181 iPhone 28:ed:6a:e9:ab:fb
192.168.1.109 lin-iPhone 90:e1:7b:1c:b1:4b
192.168.1.126 blin-pc1 f6:08:0b:0a:e0:78
root@OpenWrt:/proc/wlan0# cat /tmp/dhcp.leases | awk '{print $3,$4,$2}' | sed 's/://g'
192.168.1.181 iPhone 28ed6ae9abfb
192.168.1.109 lin-iPhone 90e17b1cb14b
192.168.1.126 blin-pc1 f6080b0ae078
root@OpenWrt:/proc/wlan0#
如果想把一个字符串中的一些字符删除可以如此:
#Echo “2006-11-21 22:16:30” | sed ‘s/-//g’ | sed ‘s/ //g’ | sed ‘s/://g’
得到的结果就是:20061121221630
http://blog.chinaunix.net/uid-71729-id-114582.html
-i 表示操作在源文件上生效.否则操作内存中数据,并不写入文件中.
在分号内的/d表示删除匹配的行
在文件中查找指定的字符串,找到后打印出行号
grep -rn 'psk' /etc/config/8192eewpa | awk -F ':' '{print $1}'
举例:
root@OpenWrt:~# cat /etc/config/8192eewpa
ctrl_interface_group=0
ap_scan=1
network={
ssid="lin"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP TKIP
group=CCMP TKIP
bssid=11.22.33
psk="11223355"
}
root@OpenWrt:~# grep -rn "psk" /etc/config/8192eewpa
10: psk="11223355"
root@OpenWrt:~# grep -rn 'psk' /etc/config/8192eewpa | awk -F ':' '{print $1}'
10
WC
1.查看文件有多少行
root@OpenWrt:/etc/config# cat /tmp/dhcp_list_file
192.168.1.181 iPhone 01:28:ed:6a:e9:ab:fb
192.168.1.108 blin-pc1 01:16:ae:ca:dc:84:5a
root@OpenWrt:/etc/config# wc -l /tmp/dhcp_list_file
2 /tmp/dhcp_list_file
root@OpenWrt:/etc/config# wc -l /tmp/dhcp_list_file | awk '{print $1}'
2
root@OpenWrt:/etc/config#
shell脚本 修改xml文件
#!/bin/bash
#修改设置关闭特效需要修改qt5-ukui-platformtheme-lite包的libqt5-ukui-style1的xml值
if [ -f /usr/share/glib-2.0/schemas/org.ukui.style.gschema.xml ]; then
MenuNum=`cat /usr/share/glib-2.0/schemas/org.ukui.style.gschema.xml | grep -n menu-transparency | awk -F ':' '{print $1}' `
if [ -z "$MenuNum" ]; then
echo "-------The value is not present in the current file MenuNum---------"
exit 0
else
echo "menunum:$MenuNum................."
fi
ModifyNum=$(($MenuNum+1))
echo "ModifyNum:$ModifyNum................."
#将第N替换为<default>100</default>
#sudo sed -i "${ModifyNum} <default>100</default>" /usr/share/glib-2.0/schemas/org.ukui.style.gschema.xml
#c\添加空格。将第N替换为: 空格或tab <default>100</default>
sudo sed -i "${ModifyNum}c\ <default>100</default>" /usr/share/glib-2.0/schemas/org.ukui.style.gschema.xml
fi
if [ -f /usr/share/glib-2.0/schemas/org.ukui.style.gschema.xml ]; then
PeonyNum=`cat /usr/share/glib-2.0/schemas/org.ukui.style.gschema.xml | grep -n peony-side-bar-transparency | awk -F ':' '{print $1}' `
if [ -z "$PeonyNum" ]; then
echo "-------The value is not present in the current file PeonyNum---------"
exit 0
else
echo "PeonyNum:$PeonyNum................."
fi
ModifyPeonyNum=$(($PeonyNum+1))
echo "ModifyPeonyNum:$ModifyPeonyNum................."
sudo sed -i "${ModifyPeonyNum}c\ <default>100</default>" /usr/share/glib-2.0/schemas/org.ukui.style.gschema.xml
fi