声明:本文仅用于学习提高,若涉及到版权问题,概不负责!!!
1. 1.txt 的内容 1||0201111||2014-06-06 09:09:09||菜单||1||22
awk -F'[|]+' '{print $3}' 1.txt
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
2, udhcpc_eth0.result 文件 截取 192.168.1.1和255.255.255.0
DHCP_OK='/var/lock/dhcp.ok'
DHCP_OK2='/var/lock/dhcp.'
IFS='
'
NETMASK='netmask 255.255.255.0'
OPTIND='1'
PATH='/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin'
PPID='7860'
PS1='\w \$ '
PS2='> '
PS4='+ '
PWD='/usr/local/bin/IMS_Modules/Wfdisplay/scripts'
RESOLV_CONF='/etc/resolv.conf'
dns='192.168.1.1'
interface='eth0'
ip='192.168.1.2'
lease='86400'
mask='24'
router='192.168.1.1'
serverid='192.168.1.1'
subnet='255.255.255.0'
答案:egrep 'dns|subnet' 1 | awk -F'=' '{print $2}' | sed "s/'/ /g"
192.168.1.1
255.255.255.0
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
# cat 1.txt
a
b
c
d
# cat 2.txt
1
2
3
4
如何合并成以下结果呢?
paste 1.txt 2.txt |xargs -n 1
a
1
b
2
c
3
d
"113.213.67.196" 简单方式把双引号替换掉,113.213.67.196 。目前没想到个简单方式。各位牛牛解决一下
方法:sed 's/\"//g' 1.txt
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
SNMPv2-SMI::enterprises.15227.1.3.1.1.5.0 = STRING: "36kbps"
以上字段我怎么取kbps之前的数字呢?那个数字是个变化值
谢谢大侠们
方法:echo SNMPv2-SMI::enterprises.15227.1.3.1.1.5.0 = STRING: "36kbps" | awk '{print $NF}'| awk -F"kbps" '{print $1}'
36
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
www
wwwww
www
ert
2345
rtyu568
84256
wrqq
www
我怎么截取最后两个www之间的内容?
方法: cat 3 | sed -n '/www/,/www/{//!p}'
需求:我有这样的字符串****** Windows NT 6.3****
我想只取6.3这个值,正则表达式怎么写?
echo "****** Windows NT 6.3****"|sed -r 's/[^0-9.]+//g'
6.3
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
需求:需要得到第十行后面的内容
sed -n '10,$p' /etc/passwd
nobody:x:65534:65533:nobody:/var/lib/nobody:/bin/bash
ntp:x:74:103:NTP daemon:/var/lib/ntp:/bin/false
postfix:x:51:51:Postfix Daemon:/var/spool/postfix:/bin/false
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
root:x:0:0:root:/root:/bin/bash
sshd:x:71:65:SSH daemon:/var/lib/sshd:/bin/false
suse-ncc:x:102:104:Novell Customer Center User:/var/lib/YaST2/suse-ncc-fakehome:/bin/bash
wwwrun:x:30:8:WWW daemon apache:/var/lib/wwwrun:/bin/false
www:x:30:8:WWW :/var/lib/www:/bin/false
man:x:13:62:Manual pages viewer:/var/cache/man:/bin/bash
news:x:9:13:News system:/etc/news:/bin/bash
uucp:x:10:14:Unix-to-Unix CoPy system:/etc/uucp:/bin/bash
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
有一个文件,里面有包含一些url,比如以下内容:
.button{width:100px;background-url('/static/images/smile.jpg')}
.button:hover {width:100px;color:blue;background-url('/static/images/smile_hover.jpg')}
url的位置不固定,我想把所有的这些url都提取到一个文件中保存,类似:
/static/images/smile.jpg
/static/images/smile_hover.jpg
cat file | awk -F "'" '{print $2}'
/static/images/smile.jpg
/static/images/smile_hover.jpg
cat file | grep -oP "(?<=\')[^')]+"
/static/images/smile.jpg
/static/images/smile_hover.jpg
cat file | cut -d "'" -f2
/static/images/smile.jpg
/static/images/smile_hover.jpg
cat file | sed -r 's/.+'\''(.+)'\''.*/\1/'
/static/images/smile.jpg
/static/images/smile_hover.jpg
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
把整个文本中第三次出现的a替换成xxoo
cat 1
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
方案1:sed -e ':a;/[^a]*\(a[^a]*\)\{3\}/{s/a/b/3;q};N;b a'
方案1:
cat 1 | sed -r ':1; /^([^a]*a){3}/{s/a/XXX/3;q}; N; b1'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:dXXXemon:/sbin:/sbin/nologin
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
awk中特殊的分隔符
echo "aaa bbb|ccc-ddd\eee" | awk -F ' +|-|\\||\\\\' '{print $1,"xxx ",$2,"ooo "$3,"xxx "$4,"ooo " $5}'
aaa xxx bbb ooo ccc xxx ddd ooo eee
定义分隔符时,改成双引号,会报错
echo "aaa bbb|ccc-ddd\eee" | awk -F " +|-|\\||\\\\" '{print $1,"xxx ",$2,"ooo "$3,"xxx "$4,"ooo " $5}'
awk: warning: escape sequence `\|' treated as plain `|'
aaa xxx bbb|ccc ooo ddd\eee xxx ooo
这么修改下,就OK
echo "aaa bbb|ccc-ddd\eee" | awk -F " +|-|\\\\||\\\\" '{print $1,"xxx ",$2,"ooo "$3,"xxx "$4,"ooo " $5}'
aaa xxx bbb ooo ccc xxx ddd\eee ooo
但是明显,"\"没有被当成分隔符来处理
再修改下:
echo "aaa bbb|ccc-ddd\eee" | awk -F " +|-|\\\\||\\\\\\" '{print $1,"xxx ",$2,"ooo "$3,"xxx "$4,"ooo " $5}'
aaa xxx bbb ooo ccc xxx ddd ooo eee
OK了,很神奇,但是有很疑惑...期待达人解释下....