亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
12下一頁(yè)
最近訪(fǎng)問(wèn)板塊 發(fā)新帖
查看: 3598 | 回復(fù): 11
打印 上一主題 下一主題

[文本處理] [已解決]檢查“重復(fù)”啟動(dòng)的process,uniq搞不定了,awk怎么寫(xiě) [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2012-10-01 15:47 |只看該作者 |倒序?yàn)g覽
本帖最后由 可可火山 于 2012-10-03 16:21 編輯

本來(lái)有個(gè)腳本去檢查是否有重復(fù)的程序運(yùn)行著,開(kāi)始使用 ps 在awk取用戶(hù)和命令部分 然后sort再u(mài)niq -d.
不過(guò)該程序支持-s 指定開(kāi)始處理的number,這樣其實(shí)加-s 參數(shù)和不加的程序認(rèn)為是相同的。 -c 是指定配置文件,所以配置文件不同可以認(rèn)為不同的程序。

下面的output為例子
user runapp -c 2runappconf1.cfg
user runapp -c 2runappconf1b.cfg -s 10030
user runapp -c 2runappconf1c.cfg
user runapp -c 2runappconf1c.cfg

user runapp -c 2runappconf2.cfg -s 10405
user runapp -c 2runappconf2b.cfg -s 9951
user runapp -c 2runappconf2c.cfg -s 10177
user runapp -c 2runappconf3.cfg -s 10406
user runapp -c 2runappconf3b.cfg -s 9892
user runapp -c 2runappconf3c.cfg -s 19662
user runapp -c 2runappconf4.cfg -s 10372
user runapp -c 2runappconf4b.cfg -s 9939
user runapp -c 2runappconf4c.cfg -s 9798
user runapp -c 2runappconf5.cfg -s 10604
user runapp -c 2runappconf5b.cfg -s 10043
user runapp -c 2runappconf5c.cfg -s 9907
user runapp -c 2runappconf6.cfg
user runapp -c 2runappconf6.cfg -s 9999

user runapp -c 2runappconf7.cfg -s 1173752


user runapp -c 2runappconf1c.cfg
通過(guò)sort 再u(mài)niq -d 可以找出來(lái),但是
user runapp -c 2runappconf6.cfg
user runapp -c 2runappconf6.cfg -s 9999
應(yīng)該是重復(fù)的進(jìn)程,sort+uniq已經(jīng)不可以把這兩行找出來(lái)了。所以準(zhǔn)備寫(xiě)awk腳本。

先考慮簡(jiǎn)單的情況,也就是上面的例子。
{
if(PRELINE==$0){
print $0;
}
if($0 ~ /PRELINE -s [0-9]*/){
print PRELINE "\n" $0;
}
PRELINE=$0
}

其中當(dāng)上一行和當(dāng)前行相同時(shí),打印當(dāng)前行,就是uniq -d的輸出 (暫不說(shuō)output有重復(fù)多行的話(huà)先不考慮)
當(dāng)前行匹配上一行加 -s 數(shù)字參數(shù)的話(huà)也認(rèn)為是重復(fù),同時(shí)打印當(dāng)前行和下一行。 不過(guò)好像語(yǔ)法什么的不同,或者正則內(nèi)部不能用變量?這部分腳本好像不工作。
請(qǐng)教下大家, 謝先。

update 20121003 寫(xiě)了個(gè)比較胖的版本。應(yīng)該還能再簡(jiǎn)化點(diǎn)。腳本:


具體測(cè)試和結(jié)果請(qǐng)看后面回復(fù)。
  1. {
  2.     TMP=$0;
  3.     gsub(" -s [0-9]*","",TMP);
  4.     CURLINEKEY=TMP;
  5.     if(PRELINE==$0&&PREPRELINE!=$0)print $0;
  6.     if(PRELINE!=$0&&CURLINEKEY==PRELINEKEY){
  7.         if(PREPRELINEKEY!=PRELINEKEY){
  8.             print PRELINE;
  9.         }
  10.             print $0;
  11.     }

  12.     PREPRELINE=PRELINE;
  13.     PREPRELINEKEY=PRELINEKEY;
  14.     PRELINE=$0;
  15.     PRELINEKEY=CURLINEKEY;
  16. }
復(fù)制代碼

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2012-10-01 18:08 |只看該作者
本帖最后由 可可火山 于 2012-10-01 18:13 編輯

看了下 awk FAQ http://awk.freeshell.org/Frequently_Asked_Questions
正則中用變量需要顯示并且不要 /***/括起來(lái)。
5. How do I use a variable as a regular expression?

The patterns between slashes like /pattern/ are called ERE constants, or regular expressions literals. As the names imply, they can only contain fixed, constant regular expressions. If you have a variable var that contains "abc(123)?r+" and try to match something against /var/, you are matching against the literal string "var", not against the regular expression. You can still use strings in places where regular expressions are expected, like this:

var="abc(123)?r+"
if ($1 ~ var){ # $1 matches, do something }

or

BEGIN{var="abc(123)?r+"}
$0 ~ var { # $0 matches, do something }

Also note that when you're using a string as a regular expression you must explicitly match it against the string you want to check; you can NOT use the string alone and expect awk to understand that you mean $0 ~ string, as happens instead for RE literals. Finally, using a string as a regex produces what's called a "computed" or "dynamic" regex. For a detailed discussion of computed regexes and the issues you should be aware of when using them, see the GNU awk manual.



我改了下腳本,基本滿(mǎn)足要求,
  1. {
  2. if(PRELINE==$0){
  3. print $0;
  4. }
  5. if($0 ~ REGEXRULE && NR>1 ){
  6. print PRELINE "\n" $0;
  7. }
  8. PRELINE=$0;
  9. REGEXRULE=$0" -s [0-9]*";
  10. }
復(fù)制代碼
接下去就是讓腳本更強(qiáng)壯些,
如果同名重復(fù)的腳本超過(guò)2個(gè)。
比如帶-s參數(shù)的相同腳本帶起了2個(gè)以上。


論壇徽章:
5
未羊
日期:2014-08-04 16:15:21天秤座
日期:2014-08-13 13:52:372015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:112015亞冠之浦和紅鉆
日期:2015-06-29 15:30:48
3 [報(bào)告]
發(fā)表于 2012-10-01 18:55 |只看該作者
  1. awk '!a[$3,$NF]++'
復(fù)制代碼

論壇徽章:
15
2015年辭舊歲徽章
日期:2015-03-03 16:54:15雙魚(yú)座
日期:2015-01-15 17:29:44午馬
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉雞
日期:2014-04-02 12:24:51雙子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥豬
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大;照
日期:2013-04-17 11:48:45
4 [報(bào)告]
發(fā)表于 2012-10-01 19:29 |只看該作者
  1. awk 'NR==FNR{a[$2,$4]++;next} a[$2,$4]>1'  output output
復(fù)制代碼

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2012-10-01 19:57 |只看該作者

  1. awk '{if(c!=$4)c=$4;else if(a){print a;print $0};a=$0}' file
復(fù)制代碼

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2012-10-01 20:28 |只看該作者
謝謝大家都熱心,不過(guò)我現(xiàn)在主要是要能判斷 當(dāng)前運(yùn)行的重復(fù)程序,
1) 命令行完全相同
2)使用相同配置文件-c ,有指定 -s 數(shù)字參數(shù)或沒(méi)指定為相同并輸出。

不同的不需要輸出。

輸入例子其實(shí)是 user + command
user runapp -c 2runappconf6.cfg
user runapp -c 2runappconf6.cfg -s 9999

大家提供了不少方案,需要點(diǎn)時(shí)間消化,不過(guò)好像都沒(méi)看到對(duì) -s 數(shù)字的處理。

論壇徽章:
2
射手座
日期:2014-10-10 15:59:4715-16賽季CBA聯(lián)賽之上海
日期:2016-03-03 10:27:14
7 [報(bào)告]
發(fā)表于 2012-10-02 00:54 |只看該作者
回復(fù) 6# 可可火山


    看起來(lái),你的要求是不是可以理解為:只要“使用相同配置文件-c”相同即可,因?yàn)椴还苁欠裼?s,前面的都是一樣的

論壇徽章:
0
8 [報(bào)告]
發(fā)表于 2012-10-02 08:27 |只看該作者
本帖最后由 可可火山 于 2012-10-02 08:30 編輯

回復(fù) 7# yinyuemi

是的

目前自己折騰的長(zhǎng)長(zhǎng)的awk腳本基本功能已經(jīng)滿(mǎn)足需求,現(xiàn)在是要多考慮些場(chǎng)景。
  1. /usr/xpg4/bin/awk '{if(PRELINE==$0) print $0;if($0 ~ REGEXRULE && NR > 1)print PRELINE "\n" $0;PRELINE=$0;REGEXRULE=$0" -s [0-9]*";}'
復(fù)制代碼
(solaris默認(rèn)/usr/bin/awk好像有些功能不支持)

1) 相同的命令啟動(dòng)了超過(guò)2個(gè),比如有n個(gè)相同程序運(yùn)行著,我的腳本會(huì)輸出n-1個(gè)
2) 腳本目前還只能處理一個(gè)相同配置并且另外一個(gè)是-s,如果再第三個(gè)程序也是相同配置文件并帶個(gè)-s的話(huà)就不能輸出。

如下面的例子
user runapp -c 2runappconf1.cfg
user runapp -c 2runappconf1b.cfg -s 10030
user runapp -c 2runappconf1c.cfg
user runapp -c 2runappconf1c.cfg
user runapp -c 2runappconf1c.cfg

user runapp -c 2runappconf2.cfg -s 10405
user runapp -c 2runappconf2b.cfg -s 9951
user runapp -c 2runappconf2c.cfg -s 10177
user runapp -c 2runappconf3.cfg -s 10406
user runapp -c 2runappconf3b.cfg -s 9892
user runapp -c 2runappconf3c.cfg -s 19662
user runapp -c 2runappconf4.cfg -s 10372
user runapp -c 2runappconf4b.cfg -s 9939
user runapp -c 2runappconf4c.cfg -s 9798
user runapp -c 2runappconf5.cfg -s 10604
user runapp -c 2runappconf5b.cfg -s 10043
user runapp -c 2runappconf5c.cfg -s 9907
user runapp -c 2runappconf6.cfg
user runapp -c 2runappconf6.cfg -s 8888
user runapp -c 2runappconf6.cfg -s 9999

user runapp -c 2runappconf7.cfg -s 1173752
腳本輸出
  1. user runapp -c 2runappconf1c.cfg
  2. user runapp -c 2runappconf1c.cfg
  3. user runapp -c 2runappconf6.cfg
  4. user runapp -c 2runappconf6.cfg -s 8888
復(fù)制代碼
希望輸出是
  1. user runapp -c 2runappconf1c.cfg
  2. user runapp -c 2runappconf6.cfg
  3. user runapp -c 2runappconf6.cfg -s 8888
  4. user runapp -c 2runappconf6.cfg -s 999
復(fù)制代碼

論壇徽章:
2
射手座
日期:2014-10-10 15:59:4715-16賽季CBA聯(lián)賽之上海
日期:2016-03-03 10:27:14
9 [報(bào)告]
發(fā)表于 2012-10-02 09:45 |只看該作者
回復(fù) 8# 可可火山

try
  1. awk '{if(n==$4){if(!m)print t;if(/-s/)print;m=1}else{m=0};n=$4;t=$0}'
復(fù)制代碼

論壇徽章:
0
10 [報(bào)告]
發(fā)表于 2012-10-03 16:18 |只看該作者
回復(fù) 9# yinyuemi


    謝謝熱心,最近國(guó)慶加班忙著做其他事情,可能有些需求也沒(méi)講清楚。
自己又折騰了下,功能是實(shí)現(xiàn)了,腳本比較丑,判斷的時(shí)候引入了當(dāng)前行CURLINE,前面一行PRELINE和前面的前面一行PREPRELINE,以及gsub去掉-s number作為判斷的KEY。

需求:
檢查系統(tǒng)當(dāng)前運(yùn)行重復(fù)的程序,程序可以是不同,可以運(yùn)行在不同用戶(hù)下。原來(lái)腳本的做法是ps出來(lái)然后awk print出用戶(hù)+CMD.
然后對(duì)結(jié)果做一次sort和uniq-d 來(lái)得到重復(fù)的。不過(guò)問(wèn)題是uniq不能判斷相同用戶(hù)下,配置文件相同但有-s參數(shù)不同。

最終版本腳本:
  1. {
  2.     TMP=$0;
  3.     gsub(" -s [0-9]*","",TMP);
  4.     CURLINEKEY=TMP;
  5.     if(PRELINE==$0&&PREPRELINE!=$0)print $0;
  6.     if(PRELINE!=$0&&CURLINEKEY==PRELINEKEY){
  7.         if(PREPRELINEKEY!=PRELINEKEY){
  8.             print PRELINE;
  9.         }
  10.             print $0;
  11.     }

  12.     PREPRELINE=PRELINE;
  13.     PREPRELINEKEY=PRELINEKEY;
  14.     PRELINE=$0;
  15.     PRELINEKEY=CURLINEKEY;
  16. }
復(fù)制代碼
[appuser@dupprocess (0)]$cat ps.out
user runapp -c 2runappconf1.cfg
user runapp -c 2runappconf1.cfg
user runapp -c 2runappconf1.cfg
user runapp -c 2runappconf1b.cfg -s 10030
user runapp -c 2runappconf1c.cfg
user runapp -c 2runappconf1c.cfg
user runapp -c 2runappconf2.cfg -s 10405
user runapp -c 2runappconf2.cfg -s 10444
user runapp -c 2runappconf2b.cfg -s 9951
user runapp -c 2runappconf2c.cfg -s 10177
user runapp -c 2runappconf3.cfg -s 10406
user runapp -c 2runappconf3b.cfg -s 9892
user runapp -c 2runappconf3c.cfg -s 19662
user runapp -c 2runappconf4.cfg -s 10372
user runapp -c 2runappconf4b.cfg -s 9939
user runapp -c 2runappconf4c.cfg -s 9798
user runapp -c 2runappconf5.cfg -s 10604
user runapp -c 2runappconf5b.cfg -s 10043
user runapp -c 2runappconf5c.cfg -s 9907
user runapp -c 2runappconf6.cfg
user runapp -c 2runappconf6.cfg -s 9999
user runapp -c 2runappconf7.cfg -s 1173752
user2 application2 argument1 -c configile01.cfg argument2
user2 application2 argument1 -c configile02.cfg argument4
user2 application2 argument1 -c configile03.cfg argument6
user2 application2 argument1 -c configile04.cfg argument8
user2 application2 argument1 -c configile05.cfg argument9
user3 application3 argument1 argument2 argument9
user3 application3 argument1 argument2 argument10
user3 application3 argument1 argument2 argument10
[appuser@dupprocess (0)]$/usr/xpg4/bin/awk -f dup.awk ps.out
user runapp -c 2runappconf1.cfg
user runapp -c 2runappconf1c.cfg
user runapp -c 2runappconf2.cfg -s 10405
user runapp -c 2runappconf2.cfg -s 10444
user runapp -c 2runappconf6.cfg
user runapp -c 2runappconf6.cfg -s 9999
user3 application3 argument1 argument2 argument10
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專(zhuān)區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP