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

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

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
樓主: yinyuemi
打印 上一主題 下一主題

[學(xué)習(xí)共享] awk初學(xué)之常見問題 [復(fù)制鏈接]

論壇徽章:
1
技術(shù)圖書徽章
日期:2013-09-25 21:06:29
41 [報(bào)告]
發(fā)表于 2011-11-07 22:22 |只看該作者
回復(fù) 1# yinyuemi


    非常感謝共享!

論壇徽章:
1
技術(shù)圖書徽章
日期:2013-09-25 21:06:29
42 [報(bào)告]
發(fā)表于 2011-11-07 22:37 |只看該作者
回復(fù) 1# yinyuemi


    樓主具體解析下 awk -v RS="#" '{$1=$1;print $0}'  $1=$1應(yīng)該如何理解啊? 剛剛看了你給發(fā)的那個(gè)帖子,沒有看明白!

論壇徽章:
2
射手座
日期:2014-10-10 15:59:4715-16賽季CBA聯(lián)賽之上海
日期:2016-03-03 10:27:14
43 [報(bào)告]
發(fā)表于 2011-11-08 00:52 |只看該作者
本帖最后由 yinyuemi 于 2011-11-08 00:54 編輯
回復(fù)  yinyuemi


    樓主具體解析下 awk -v RS="#" '{$1=$1;print $0}'  $1=$1應(yīng)該如何理解。 剛剛 ...
yuloveban 發(fā)表于 2011-11-07 22:37

http://www.gnu.org/s/gawk/manual/gawk.html#Fields
Advanced Notes: Understanding $0

It is important to remember that $0 is the full record, exactly as it was read from the input. This includes any leading or trailing whitespace, and the exact whitespace (or other characters) that separate the fields.

It is a not-uncommon error to try to change the field separators in a record simply by setting FS and OFS, and then expecting a plain ‘print’ or ‘print $0’ to print the modified record.

But this does not work, since nothing was done to change the record itself. Instead, you must force the record to be rebuilt, typically with a statement such as ‘$1 = $1’, as described earlier.
  1. $1=$1可以使OFS生效


  2. echo '1#1#1
  3. 2#2#2'  |awk -vRS="#" '{$1=$1;print $0}'
  4. 1
  5. 1
  6. 1 2 # OFS默認(rèn)值為空格,生效!或者說,任意一個(gè)對(duì)域進(jìn)行操作的action,都會(huì)使得OFS生效,比如$2=$2,NF+=0;
  7. 2
  8. 2

  9. 再舉兩個(gè)例子:
  10.     echo '1 1 1
  11.     2 2 2' |awk 'NR==1{OFS=":";$1=$1;print}NR==2{OFS="#";$1=$1;print}'  
  12.     1:1:1
  13.     2#2#2

  14.     echo '1 1 1
  15.     2 2 2' |awk 'NR==1{OFS=":";$0=$0;print}NR==2{OFS="#";$0=$0;print}'   # $0=$0的指令并不會(huì)引起OFS生效。
  16.     1 1 1
  17.     2 2 2

復(fù)制代碼

論壇徽章:
1
技術(shù)圖書徽章
日期:2013-09-25 21:06:29
44 [報(bào)告]
發(fā)表于 2011-11-08 09:28 |只看該作者
回復(fù) 43# yinyuemi


    3qs

論壇徽章:
0
45 [報(bào)告]
發(fā)表于 2011-11-08 13:08 |只看該作者
總結(jié)的真好,感謝

論壇徽章:
0
46 [報(bào)告]
發(fā)表于 2011-11-17 20:12 |只看該作者
關(guān)于第四點(diǎn),今天看manual的時(shí)候找到了,貼下:
Finally, there are times when it is convenient to force awk to rebuild the entire record, using the current value of the fields and OFS. To do this, use the seemingly innocuous assignment:

     $1 = $1   # force record to be reconstituted
     print $0  # or whatever else with $0

This forces awk rebuild the record. It does help to add a comment, as we've shown here.

There is a flip side to the relationship between $0 and the fields. Any assignment to $0 causes the record to be reparsed into fields using the current value of FS. This also applies to any built-in function that updates $0, such as sub() and gsub() (see String Functions).

Advanced Notes: Understanding $0
It is important to remember that $0 is the full record, exactly as it was read from the input. This includes any leading or trailing whitespace, and the exact whitespace (or other characters) that separate the fields.

It is a not-uncommon error to try to change the field separators in a record simply by setting FS and OFS, and then expecting a plain ‘print’ or ‘print $0’ to print the modified record.

But this does not work, since nothing was done to change the record itself. Instead, you must force the record to be rebuilt, typically with a statement such as ‘$1 = $1’, as described earlier.

論壇徽章:
2
射手座
日期:2014-10-10 15:59:4715-16賽季CBA聯(lián)賽之上海
日期:2016-03-03 10:27:14
47 [報(bào)告]
發(fā)表于 2011-11-17 23:57 |只看該作者
回復(fù) 46# xiaopan3322


    多謝Bob

論壇徽章:
0
48 [報(bào)告]
發(fā)表于 2011-11-18 09:03 |只看該作者
不錯(cuò),給力的貼子

論壇徽章:
0
49 [報(bào)告]
發(fā)表于 2012-05-15 14:08 |只看該作者
10. awk ‘! a[$0]++’ 怎么理解?

這是一個(gè)非常經(jīng)典的去重復(fù)項(xiàng)的awk語句,雖然短小,不過涉及到了不少知識(shí)點(diǎn),下面一一解讀:

<1> :”!” 即非。

<2>:a[$0],以$0為數(shù)據(jù)下標(biāo),建立數(shù)組a

<3>:a[$0]++,即給數(shù)組a賦值,a[$0]+=1

<4> :那么組合起來,awk是怎么執(zhí)行!a[$0]++的呢?我用一個(gè)實(shí)際例子來解釋:


01.cat file

02.111

03.222

04.111

05.222

06.333

07.

08.awk '{print a[$0],!a[$0]++,a[$0],!a[$0],$0}' file

09.  1 1 0 111

10.  1 1 0 222

11.1 0 2 0 111

12.1 0 2 0 222

13.  1 1 0 333
復(fù)制代碼


原來,第一個(gè)a[$0]的值為空,所以!a[$0]++是先作判斷,結(jié)果為1(非空為真,即為1),再作數(shù)組賦值a[$0]++。這也就是為什么前面的!a[$0]++并不一定等于后面的!a[$0]。

awk ‘++a[$0]==1’ 和上面的代碼作用一樣,你理解了么?


不懂

論壇徽章:
13
15-16賽季CBA聯(lián)賽之同曦
日期:2016-01-28 19:52:032015亞冠之北京國(guó)安
日期:2015-10-07 14:28:19NBA常規(guī)賽紀(jì)念章
日期:2015-05-04 22:32:03處女座
日期:2015-01-15 19:45:44卯兔
日期:2014-10-28 16:17:14白羊座
日期:2014-05-24 15:10:46寅虎
日期:2014-05-10 09:50:35白羊座
日期:2014-03-12 20:52:17午馬
日期:2014-03-01 08:37:27射手座
日期:2014-02-19 19:26:54子鼠
日期:2013-11-30 09:03:56獅子座
日期:2013-09-08 08:37:52
50 [報(bào)告]
發(fā)表于 2012-05-29 15:10 |只看該作者
總結(jié)的真好 ,感謝分享!
您需要登錄后才可以回帖 登錄 | 注冊(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)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP