十一 062009

例如,我有一个文件包含这样的内容,我需要查找给变量abc和k连续赋值得语句(连续两句)

…..
abc=13               这里是符合条件的地方
k=12
…..

abc=5                 单独给abc赋值,不符合条件

…..
…..
abc=14               这个也是符合条件的地方
k=5
……

也就是说,如果我单独用两个grep来查找的话,肯定会得到很多不符合条件的结果。
如何才能达到前面我描述的需求呢? 也就是说,我希望的过滤结果像下面这样:
abc=13
k=12
abc=14
k=5
解答:

awk ‘/abc=/{s=$0;getline;if (/k=/) print s”\n”$0}’ file

sed -n ‘/abc=/{N;/k=/p}’ file

while read line
do
echo $line | grep -q k= && echo $pre | grep -q abc= && echo $pre && echo $line
pre=$line
done<file

来自:http://bbs3.chinaunix.net/viewthread.php?tid=1604893

Posted by admin Tagged with: ,
142009

<?xml version="1.0" encoding="UTF-8"?>
<player showDisplay="yes" showPlaylist="yes" autoStart="no"> <song path="http://music.u148.net/Kris_Kristofferson-holy_woman.mp3" title="Kris_Kristofferson-holy_woman" /> <song path="http://music.u148.net/public_image_ltd-disappointed.mp3" title="public_image_ltd-disappointed" /> <song path="http://music.u148.net/Muse-Uprising.mp3" title="Muse-Uprising" /> <song path="http://music.u148.net/Orla_Fallon-She_Moved_Thro_the_Fair.mp3" title="Orla_Fallon-She_Moved_Thro_the_Fair" /> <song path="http://music.u148.net/damon_and_naomi-E.T.A.mp3" title="damon_and_naomi-E.T.A." /> <song path="http://music.u148.net/England_Dan_And_John_Ford_Coley-Who_s_Lonely_Now.mp3" title="England_Dan_And_John_Ford_Coley-Who_s_Lonely_Now" /> <song path="http://music.u148.net/the_avett_brothers-will_you_return.mp3" title="the_avett_brothers-will_you_return" /> <song path="http://music.u148.net/richard_hawley-ashes_on_the_fire.mp3" title="richard_hawley-ashes_on_the_fire" /> <song path="http://music.u148.net/avion_travel-abbassando.mp3" title="avion_travel-abbassando" /> <song path="http://music.u148.net/Elisabeth_Kontomanou-I_gotta_right_to_sing_the_blues.mp3" title="Elisabeth_Kontomanou-I_gotta_right_to_sing_the_blues" /> <song path="http://music.u148.net/Matthew_Barber-Easily_bruised.mp3" title="Matthew_Barber-Easily_bruised" /> <song path="http://music.u148.net/Pearl_Jam-Just_Breathe.mp3" title="Pearl_Jam-Just_Breathe" /> <song path="http://music.u148.net/Hope_Sandoval_&_The_Warm_Inventions-Wild_Roses.mp3" title="Hope_Sandoval_&_The_Warm_Inventions-Wild_Roses" /> <song path="http://music.u148.net/u2-ill_go_crazy_if_i_dont_go_crazy_tonight.mp3" title="u2-ill_go_crazy_if_i_dont_go_crazy_tonight" /> <song path="http://music.u148.net/Monsters_Of_Folk-Say_Please.mp3" title="Monsters_Of_Folk-Say_Please" /> <song path="http://music.u148.net/Andres_Cepeda-Faltarán.mp3" title="Andres_Cepeda-Faltarán" /> <song path="http://music.u148.net/Mark_Mulcahy-Be_Sure.mp3" title="Mark_Mulcahy-Be_Sure" /> <song path="http://music.u148.net/Brett_Dennen-San Francisco.mp3" title="Brett_Dennen-San Francisco" /> <song path="http://music.u148.net/Hnir_Pan-Piyka.mp3" title="Hnir_Pan-Piyka" /> <song path="http://music.u148.net/KAYAH-EMBARCACAO.mp3" title="KAYAH-EMBARCACAO" /> <song path="http://music.u148.net/Mosh_Ben_Ari-Bein_hazlilim.mp3" title="Mosh_Ben_Ari-Bein_hazlilim" /> <song path="http://music.u148.net/the_black_crowes-appaloosa.mp3" title="the_black_crowes-appaloosa" /></player>

今天想去下载王小峰博客上的音乐,像往常一样,把http://www.wangxiaofeng.net/mp3player.xml拉回来,发现格式都乱了,像以上,只有2行(<player ×××>到</player>为一行)。
想过将http与mp3之间的内容提取出来,但不知道如何入手,最后想出先将它分行,再提出mp3地址

sed "s/<song/\n\r<song/g" mp3player.xml |cut -d\" -f2

输出,有首歌的歌名不是英文,乱码了,Mark_Mulcahy-Be_Sure.mp3

http://music.u148.net/Kris_Kristofferson-holy_woman.mp3

http://music.u148.net/public_image_ltd-disappointed.mp3

http://music.u148.net/Muse-Uprising.mp3

http://music.u148.net/Orla_Fallon-She_Moved_Thro_the_Fair.mp3

http://music.u148.net/damon_and_naomi-E.T.A.mp3

http://music.u148.net/England_Dan_And_John_Ford_Coley-Who_s_Lonely_Now.mp3

http://music.u148.net/the_avett_brothers-will_you_return.mp3

http://music.u148.net/richard_hawley-ashes_on_the_fire.mp3

http://music.u148.net/avion_travel-abbassando.mp3

http://music.u148.net/Elisabeth_Kontomanou-I_gotta_right_to_sing_the_blues.mp3

http://music.u148.net/Matthew_Barber-Easily_bruised.mp3

http://music.u148.net/Pearl_Jam-Just_Breathe.mp3

http://music.u148.net/Hope_Sandoval_&_The_Warm_Inventions-Wild_Roses.mp3

http://music.u148.net/u2-ill_go_crazy_if_i_dont_go_crazy_tonight.mp3

http://music.u148.net/Monsters_Of_Folk-Say_Please.mp3

http://music.u148.net/Andres_Cepeda-Faltar¨¢n.mp3

http://music.u148.net/Mark_Mulcahy-Be_Sure.mp3

http://music.u148.net/Brett_Dennen-San Francisco.mp3

http://music.u148.net/Hnir_Pan-Piyka.mp3

http://music.u148.net/KAYAH-EMBARCACAO.mp3

http://music.u148.net/Mosh_Ben_Ari-Bein_hazlilim.mp3

注意,如果是vi mp3player.xml,替换时,换行符则只有\r,而用sed时则是\n\r
:s/<song/\r<song/g

Windows下用Notepad++打开正则表达式进行替换,换行符是\n

单命令用

grep -oP '(?<=song path=")[^"]*(?=" title=)' mp3player.xml

更简单的

grep -o 'http[^"]*\.mp3' mp3player.xml

Posted by admin Tagged with: ,
132009

for i in 'awstats www phpmyadmin webconsole'
do
for DX in `du -s $i | awk '{print $1}'`
do
let "DD=${DD}+${DX}"
done
let "DDD=${DD}/1024/1024"
echo ${DDD}G
done

Posted by admin Tagged with:
082009

比较简单,这样就可以了

cat > /var/spool/cron/root <<EOF
* * * * * /usr/sbin/ntpdate cn.pool.ntp.org >> /var/log/ntpdate.log
EOF

额外的资料
1.copy or backup
#crontab -l > /tmp/root.crontab
2.edit
edit root.crontab by vi,emacs, ed ,etc etc .
3.activate
#crontab /tmp/root.crontab

Posted by admin Tagged with: ,
202009

文本文件tmp.txt中含有一下内容
cd $AICBS22/vgroup/busivpmn/server/atmi/;gmake -f Makefile.test clean;gmake -f Makefile.test
cd $AICBS22/vgroup/busivpmn/server/atmi/test/source/;gmake -f Makefile clean;gmake -f Makefile
cd $AICBS22/vgroup/busivpmn/server/mgr/;gmake -f Makefile clean;gmake -f Makefile
cd $AICBS22/vgroup/busivpmn/server/mgr/;gmake -f Makefile.mml clean;gmake -f Makefile.mml
cd $AICBS22/vgroup/busivpmn/server/mgr/;gmake -f Makefile.res clean;gmake -f Makefile.res
cd $AICBS22/vgroup/busivpmn/server/pmgr/;gmake -f Makefile clean;gmake -f Makefile
cd $AICBS22/vgroup/busivpmn/server/pmgr/;gmake -f Makefile.intf clean;gmake -f Makefile.intf
cd $AICBS22/vgroup/busivpmn/server/releaseVpmnPhone/;gmake -f Makefile clean;gmake -f Makefile
cd $AICBS22/vgroup/busivpmn/server/svr/;gmake -f Makefile clean;gmake -f Makefile
cd $AICBS22/vgroup/busivpmn/server/svr/;gmake -f Makefile.svr1 clean;gmake -f Makefile.svr1
cd $AICBS22/vgroup/busivpmn/server/syncvnextprod/;gmake -f Makefile clean;gmake -f Makefile
cd $AICBS22/vgroup/busivpmn/server/vpmnAsyncNextProd/;gmake -f Makefile clean;gmake -f Makefile
请各位老大看看这么写个shell。可以将每行中字符串clean后面的内容都去掉。
最后结果变为
cd $AICBS22/vgroup/busivpmn/server/atmi/;gmake -f Makefile.test clean
。。。
省略
。。。
cd $AICBS22/vgroup/busivpmn/server/vpmnAsyncNextProd/;gmake -f Makefile clean

解决
1、
grep -o '.*clean' tmp.txt
2、
sed 's/\(.*clean\).*/\1/' urfile

3、
awk -F 'clean' '{print $1""FS}' urfile

来自:http://blog.chinaunix.net/u2/72759/showart_1915246.html

Posted by admin Tagged with: