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

Chinaunix

標題: awk RS [打印本頁]

作者: sync_1521    時間: 2015-10-16 17:54
標題: awk RS
本帖最后由 sync_1521 于 2015-10-16 17:55 編輯

各位大神問個問題,謝謝!
test@sever:/tmp> cat 2
aaa
bbb
cc
aaa
cc
aaa
test@sever:/tmp> awk -v RS="cc" '{print $0}' 2
aaa
bbb


aaa


aaa

test@sever:/tmp> awk -v RS="cc" '{$1=$1}{print $0}' 2
aaa bbb
aaa
aaa

為什么加了$1=$1后就把第一第二第三行中的\n給去掉了?
作者: waker    時間: 2015-10-16 18:09
Changing FS Does Not Affect the Fields
According to the POSIX standard, awk is supposed to behave as if each record is split into fields at the time it is read. In particular, this means that if you change the value of FS after a record is read, the values of the fields (i.e., how they were split) should reflect the old value of FS, not the new one.

However, many older implementations of awk do not work this way. Instead, they defer splitting the fields until a field is actually referenced. The fields are split using the current value of FS! (d.c.) This behavior can be difficult to diagnose. The following example illustrates the difference between the two methods:

sed 1q /etc/passwd | awk '{ FS = ":" ; print $1 }'
which usually prints:

root
on an incorrect implementation of awk, while gawk prints the full first line of the file, something like:

root:0:0:Root:/:
(The sed23 command prints just the first line of /etc/passwd.)
作者: waker    時間: 2015-10-16 18:10
如果你用的是gawk,它是不符合posix標準的
作者: sync_1521    時間: 2015-10-16 18:36
本帖最后由 sync_1521 于 2015-10-16 18:44 編輯

沒看懂。。
我知道OFS有這么一種說法$1=$1后OFS才會生效
如:
test@sever:/tmp> echo "a b c" |awk -vOFS=":" '{print $0}'
a b c
test@sever:/tmp> echo "a b c" |awk -vOFS=":" '{$1=$1;print $0}'
a:b:c

但是這個RS應該是用$1=$1和不用$1=$1都生效了的
test@sever:/tmp> cat 2
aaa
bbb
cc
aaa
cc
aaa
test@sever:/tmp> awk -v RS="cc" '{print $0}' 2
aaa
bbb


aaa


aaa

注:指定RS="cc"后,整個文件2被分成3行,第一行$0=aaa\nbbb\n
                                                         第二行$0=aaa\n
                                                         第三行$0=aaa
    所以awk在讀入第一行執(zhí)行print $0時輸出aaa\nbbb\n\n
          awk在讀入第二行執(zhí)行print $0時輸出aaa\n\n
          awk在讀入第三行執(zhí)行print $0時輸出aaa\n
           
test@sever:/tmp> awk -v RS="cc" '{$1=$1}{print $0}' 2
aaa bbb
aaa
aaa

而這個例子,我就不明白$1=$1后 第一行執(zhí)行print $0時輸出aaa bbb\n
                                            第二行執(zhí)行print $0時輸出aaa\n   
                                            第三行執(zhí)行print $0時輸出aaa\n
這個$1=$1把\n全干掉了                  
                                 
回復 3# waker


   
作者: waker    時間: 2015-10-16 18:51
\n被去除是域拆分的結果,和RS沒關系
作者: tc1989tc    時間: 2015-10-16 19:56
Assigning a value to an existing field causes the whole record to be rebuilt when  $0  is  referenced.   Simi-
       larly, assigning a value to $0 causes the record to be resplit, creating new values for the fields.

$1=$1賦值,在print $0 的時候會根據(jù)OFS重新build $0
所以導致$0的換行符號替換為了空格(默認的OFS為空格)
作者: xiang1162090014    時間: 2015-10-18 15:24
test@sever:/tmp> awk -v RS="cc" '{print $0}' 2
aaa
bbb


aaa


aaa

注:指定RS="cc"后,整個文件2被分成3行,第一行$0=aaa\nbbb\n
                                                         第二行$0=aaa\n
                                                         第三行$0=aaa
    所以awk在讀入第一行執(zhí)行print $0時輸出aaa\nbbb\n\n
          awk在讀入第二行執(zhí)行print $0時輸出aaa\n\n
          awk在讀入第三行執(zhí)行print $0時輸出aaa\n
           


這里我是這么理解的:
制定RS="cc"后,整個文件2被分成3個record,第一個record $0=aaa
                                                                                           bbb
                                                                                          
                                                                第二個record $0=aaa
                                                                                    
                                                                  第三個record $0=aaa
所以 awk 在讀入第一個record,執(zhí)行print $0時輸出 aaa bbb   ($0 的第一二行 ”空格“ ”tab“  ”\n" 當作 FS  ,而 OFS 為 空格)
                                                                                      ($0的第三行--空行--因為“$1"為空,所以awk沉默,無輸出)
...
...
作者: jason680    時間: 2015-10-18 20:52
回復 1# sync_1521

http://www.72891.cn/thread-1790335-1-1.html

http://www.72891.cn/thread-2309494-1-1.html
   
作者: sync_1521    時間: 2015-10-19 09:35
謝謝各位的回復,大概明白了。
這里面應該有兩個知識點:
1.$1=$1后域根據(jù)OFS(這個OFS因為沒指定,所以默認為空格)重新拆分組合。
2.當RS不為默認的“\n”時,以連續(xù)的 空格 或 制表符(\t) 或 換行符(\n)作為FS(這個例子里就用空格和\n作為FS)。
作者: 聆雨淋夜    時間: 2015-10-24 22:17
回復 9# sync_1521
你第二條理解可能有些誤會。
一般不管RS是什么,都不影響FS ,\n本來即是RS也是FS,并不是因為改變了RS,\n成了FS
再說特殊情況,那就是當RS=""時(這時候以連續(xù)的空行為RS),如果此時FS是單個字符,比如":",則將\n強制為FS,則FS為":"和\n


   




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2