亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区
Chinaunix
標(biāo)題:
求大神幫忙寫個腳本
[打印本頁]
作者:
lwphappy
時間:
2014-10-23 14:20
標(biāo)題:
求大神幫忙寫個腳本
本帖最后由 lwphappy 于 2014-10-23 15:34 編輯
處理前的文本中含有這兩行:
s1 1 A hello1
s2 1 A hello1
s3 1 A hello1
aa 1 A hello1
aa 1 R(aa) hello2
s4 1 A hello1
s5 1 A hello1
s6 1 A hello1
處理后的文本變成這樣:
s1 1 A hello1
s2 1 A hello1
s3 1 A hello1
aa 1 A hello1
bb 1 R(aa) hello2
s4 1 A hello1
s5 1 A hello1
s6 1 A hello1
即只替換每一行中關(guān)鍵字出現(xiàn)兩次的首個匹配字段
大神們幫幫忙
作者:
歐陽西風(fēng)
時間:
2014-10-23 15:07
回復(fù)
1#
lwphappy
這個條件給的不是太明確啊, 是連續(xù)出現(xiàn)兩次的第二次需要修改還是只要出現(xiàn)第二次就修改? 另外是恰好出現(xiàn)兩次,還是只要出現(xiàn)兩次就替換?
最好是根據(jù)你的實際情況,給一個比較完整點兒的測試文本。
作者:
lwphappy
時間:
2014-10-23 15:09
回復(fù)
2#
歐陽西風(fēng)
不好意思,使我描述不清楚,是每一行中出現(xiàn)了兩次關(guān)鍵字的行的首個關(guān)鍵字替換成別的
作者:
歐陽西風(fēng)
時間:
2014-10-23 15:21
回復(fù)
3#
lwphappy
是這樣嗎?
awk '{if(split($0,a,$1)==3){$1="##"}} 1' file.txt
復(fù)制代碼
作者:
lwphappy
時間:
2014-10-23 15:26
回復(fù)
4#
歐陽西風(fēng)
謝謝回復(fù),但是我需要修改原文件的數(shù)據(jù),不只是輸出,而且我的文本中還有很多不包括這個關(guān)鍵字的行數(shù)據(jù)
作者:
歐陽西風(fēng)
時間:
2014-10-23 15:29
回復(fù)
5#
lwphappy
所以我讓你把問題描述清楚,并給出比較完整的測試文本啊
作者:
lwphappy
時間:
2014-10-23 15:35
回復(fù)
6#
歐陽西風(fēng)
你看看我剛修改了的文本內(nèi)容,差不多就是這么個樣子了,能幫忙看看?
作者:
歐陽西風(fēng)
時間:
2014-10-23 16:14
回復(fù)
7#
lwphappy
awk:
可以使用之前的代碼,直接重定向到文件中 awk '...' file.txt > file1.txt
不建議直接改寫源文件,如果確實需要更改的話,可以 awk '...' file.txt
1<>file.txt
sed
如果需要替換源文件,請加
-i
參數(shù))
sed 's/^\(\S\+\)\(\s\+.*\1.*\)/bb\2/' file.txt
復(fù)制代碼
作者:
lwphappy
時間:
2014-10-23 17:05
回復(fù)
8#
歐陽西風(fēng)
多謝,我這里sed不支持-i參數(shù),用awk '{...}' file 1<>file解決了
作者:
yestreenstars
時間:
2014-10-23 22:39
回復(fù)
9#
lwphappy
目測不行~{:3_193:}
作者:
bulletmarquis
時間:
2014-10-24 09:22
@lwphappy
@yestreenstars
回復(fù)
8#
歐陽西風(fēng)
用<>真能解決問題?
shell的處理順序,不是先做>再做<么。。這樣應(yīng)該是把源文件清空了呀
作者:
bulletmarquis
時間:
2014-10-24 09:25
本帖最后由 bulletmarquis 于 2014-10-24 09:26 編輯
@Shell_HAT
@jason680
@blackold
回復(fù)
11#
bulletmarquis
測試了一下,確實沒清空,囧。。。
但是結(jié)果是在原文件的開頭追加了本次輸出,這是什么原理。?
awk -F, 'NF=3' file 1<>file
cat file
Symbol Date Time
SZ000001 20100105 91514314
SZ000001 20100105 91542342
SZ000001 20100105 91632392
ce4,BidPrice5,BidPrice6,BidPrice7,BidPrice8,BidPrice9,nAskPrice0,nAskPrice1,nAskPrice2,nAskPrice3,nAskPrice4,nAskPrice5,nAskPrice6,nAskPrice7,nAskPrice8,nAskPrice9,BidVolume0,BidVolume1,BidVolume2,BidVolume3,BidVolume4,BidVolume5,BidVolume6,BidVolume7,BidVolume8,BidVolume9,AskVolume0,AskVolume1,AskVolume2,AskVolume3,AskVolume4,AskVolume5,AskVolume6,AskVolume7,AskVolume8,AskVolume9
SZ000001,20100105,91514314,0.0,0,0,0, ,237100,0,0,0,0,0,0,0,0,0,237100,0,0,0,0,0,0,0,0,0,8000,0,0,0,0,0,0,0,0,0,,8000,5101,0,0,0,0,0,0,0,0
SZ000001,20100105,91542342,0.0,0,0,0, ,237100,0,0,0,0,0,0,0,0,0,237100,0,0,0,0,0,0,0,0,0,9500,0,0,0,0,0,0,0,0,0,,9500,3601,0,0,0,0,0,0,0,0
SZ000001,20100105,91632392,0.0,0,0,0, ,237100,0,0,0,0,0,0,0,0,0,237100,0,0,0,0,0,0,0,0,0,10500,0,0,0,0,0,0,0,0,0,,10500,5451,0,0,0,0,0,0,0,0
復(fù)制代碼
作者:
blackold
時間:
2014-10-24 09:51
回復(fù)
12#
bulletmarquis
<> 不是 < 和 >
作者:
yestreenstars
時間:
2014-10-24 10:00
回復(fù)
12#
bulletmarquis
13問里好像有,請叫我紅領(lǐng)巾~{:3_193:}
作者:
bulletmarquis
時間:
2014-10-24 10:11
回復(fù)
14#
yestreenstars
趕快復(fù)習(xí)去~~~
我就是當(dāng)初看十三問的時候,還特意試了一下<a>a 和<a>>a,不過<>a是啥意思就沒注意過了=。=
作者:
yestreenstars
時間:
2014-10-24 10:30
回復(fù)
15#
bulletmarquis
老實說,我也沒用過<>,十三問也沒怎么看~{:3_193:}
作者:
bulletmarquis
時間:
2014-10-24 11:04
本帖最后由 bulletmarquis 于 2014-10-24 11:05 編輯
@yestreenstars
@blackold
回復(fù)
14#
yestreenstars
紅領(lǐng)巾你好~~十三問里面沒解釋<>,只是作為習(xí)題的,我測試了一下,完全看不懂。。。
這個<>到底是咋解釋的??
cat file
1 2
3 4
5 6
echo aaa <> file
aaa
cat file
1 2
3 4
5 6
echo aaa 1<>file
aaa
2
3 4
5 6
echo bbb 1<>file
cat file
bbb
2
3 4
5 6
ls kk 2<> file
cat file
ls: 無法訪問 kk: 沒有那個文件或目錄
復(fù)制代碼
作者:
yestreenstars
時間:
2014-10-24 11:56
回復(fù)
17#
bulletmarquis
有提到,只是沒解釋,繁體看得好累~{:3_193:}
作者:
lwphappy
時間:
2014-10-24 14:45
回復(fù)
10#
yestreenstars
大神,實測是可以的,不過有風(fēng)險,會覆蓋原文件
[sop01] >cat test1.txt
s1 1 A hello1
s2 1 A hello1
s3 1 A hello1
s4 1 A hello1
s5 1 A hello1
aa 1 A hello1
aa 1 R(aa) hello2
s1 1 A hello1
s2 1 A hello1
s3 1 A hello1
s4 1 A hello1
[sop01] >awk '{if($1=="aa" && $3=="R(aa)") {$1="bb";print} else print}' test1.txt 1<>test1.txt
[sop01] >cat test1.txt
s1 1 A hello1
s2 1 A hello1
s3 1 A hello1
s4 1 A hello1
s5 1 A hello1
aa 1 A hello1
bb 1 R(aa) hello2
s1 1 A hello1
s2 1 A hello1
s3 1 A hello1
s4 1 A hello1
復(fù)制代碼
作者:
yestreenstars
時間:
2014-10-24 14:46
回復(fù)
19#
lwphappy
你的目的不就是覆蓋原文件么?{:3_193:}
作者:
lwphappy
時間:
2014-10-24 15:09
回復(fù)
20#
yestreenstars
我的意思是萬一失誤操作,勞動成果就都付之一炬了
作者:
lwphappy
時間:
2014-10-24 15:10
回復(fù)
11#
bulletmarquis
可以的,請看19樓的測試案例
作者:
yestreenstars
時間:
2014-10-24 17:41
回復(fù)
21#
lwphappy
所以說最好還是用臨時文件~
歡迎光臨 Chinaunix (http://www.72891.cn/)
Powered by Discuz! X3.2