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

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

Chinaunix

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

[文本處理] AWK多行,不同條件輸出不同字段如何處理? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2015-09-02 10:31 |只看該作者 |倒序?yàn)g覽
awk單行不同字段已掌握,當(dāng)處理一段文本,輸出不同行的不同字段時(shí)怎么處理呢?

例如文本內(nèi)容如下:
  1. 2015-08-31 15:53:59,383 INFO  -  進(jìn)入任務(wù):sendTask ZH05
  2. 2015-08-31 15:53:59,384 INFO  -  目標(biāo)號(hào)碼:1
  3. 2015-08-31 15:53:59,384 INFO  -  FROM:111,TO:222 BEGIN
  4. 2015-08-31 15:53:59,384 INFO  -  請(qǐng)求長度為:110
  5. 2015-08-31 15:53:59,424 INFO  -  請(qǐng)求狀態(tài):200
  6. 2015-08-31 15:53:59,424 INFO  -  返回值為:<?xml version="1.0" encoding="utf-8"?>2|14410076429841410</string>
  7. 2015-08-31 15:53:59,427 INFO  -  返回值解析后為:2|14410076429841410
復(fù)制代碼
要求輸出結(jié)果如下:
2015-08-31 15:53:59,ZH05,222,14410076429841410

以逗號(hào)分隔
第一列為本段文本的時(shí)間取值
第二列為第一行的最后一個(gè)字段
第三列為第三行的TO:后面的值
第四列為返回值中的ID(14410076429841410)

多條件對(duì)同一行處理比較清楚,怎么對(duì)多行使用不同條件,選擇性輸出呢,還請(qǐng)給個(gè)例子,謝謝。

論壇徽章:
95
程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-09-05 06:20:00程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-09-17 06:20:00程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-09-18 06:20:002015亞冠之阿爾艾因
日期:2015-09-18 10:35:08月度論壇發(fā)貼之星
日期:2015-09-30 22:25:002015亞冠之阿爾沙巴布
日期:2015-10-03 08:57:39程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-10-05 06:20:00每日論壇發(fā)貼之星
日期:2015-10-05 06:20:002015年亞冠紀(jì)念徽章
日期:2015-10-06 10:06:482015亞冠之塔什干棉農(nóng)
日期:2015-10-19 19:43:35程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-10-21 06:20:00每日論壇發(fā)貼之星
日期:2015-09-14 06:20:00
2 [報(bào)告]
發(fā)表于 2015-09-02 10:50 |只看該作者
回復(fù) 1# xiaogui_vip


    這些行之間有什么關(guān)系(根據(jù)什么關(guān)聯(lián)在一起)?和其他的類似記錄如何區(qū)分?

論壇徽章:
145
技術(shù)圖書徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11獅子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龍
日期:2014-01-08 15:26:12技術(shù)圖書徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
3 [報(bào)告]
發(fā)表于 2015-09-02 10:53 |只看該作者
回復(fù) 1# xiaogui_vip


$ cat FILE
2015-08-31 15:53:59,383 INFO  -  進(jìn)入任務(wù):sendTask ZH05
2015-08-31 15:53:59,384 INFO  -  目標(biāo)號(hào)碼:1
2015-08-31 15:53:59,384 INFO  -  FROM:111,TO:222 BEGIN
2015-08-31 15:53:59,384 INFO  -  請(qǐng)求長度為:110
2015-08-31 15:53:59,424 INFO  -  請(qǐng)求狀態(tài):200
2015-08-31 15:53:59,424 INFO  -  返回值為:<?xml version="1.0" encoding="utf-8"?>2|14410076429841410</string>
2015-08-31 15:53:59,427 INFO  -  返回值解析后為:2|14410076429841410

$ awk '
BEGIN{
OFS=","
}
/進(jìn)入任務(wù)/{
  time = $1" "$2
  sub(/,.+$/, "", time)
  task = $NF
  to = "";
}
match($0,/TO:([^ ]+)/,a){
  to = a[1]
}
/返回值解析后/{
  sub(/^.*[|]/, "", $NF)
  print time, task, to, $NF
}' FILE
2015-08-31 15:53:59,ZH05,222,14410076429841410

   

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2015-09-02 10:56 |只看該作者
回復(fù) 2# MMMIX


    行之間的關(guān)系是這樣的:

    其實(shí)應(yīng)該能看出來,這個(gè)是一個(gè)程序的日志文件,每次程序開始運(yùn)行,進(jìn)入sendTask任務(wù),就會(huì)輸出這樣一段的日志,中間不會(huì)夾雜其他日志
    如果這段文本的內(nèi)容作為A的話,完整的日志應(yīng)該是這樣的:
  1.     A
  2.     無任務(wù)需要處理
  3.     A
  4.     無任務(wù)需要處理
復(fù)制代碼
要說關(guān)聯(lián),并沒有根據(jù)什么關(guān)聯(lián)在一起,只是一次任務(wù)處理的完整日志而已。

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2015-09-02 11:08 |只看該作者
回復(fù) 3# jason680


    前輩能解釋一下兩個(gè)sub分別的意義么?

論壇徽章:
95
程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-09-05 06:20:00程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-09-17 06:20:00程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-09-18 06:20:002015亞冠之阿爾艾因
日期:2015-09-18 10:35:08月度論壇發(fā)貼之星
日期:2015-09-30 22:25:002015亞冠之阿爾沙巴布
日期:2015-10-03 08:57:39程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-10-05 06:20:00每日論壇發(fā)貼之星
日期:2015-10-05 06:20:002015年亞冠紀(jì)念徽章
日期:2015-10-06 10:06:482015亞冠之塔什干棉農(nóng)
日期:2015-10-19 19:43:35程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2015-10-21 06:20:00每日論壇發(fā)貼之星
日期:2015-09-14 06:20:00
6 [報(bào)告]
發(fā)表于 2015-09-02 11:08 |只看該作者
回復(fù) 4# xiaogui_vip


    隱含的條件你最好是明確說出來,不要讓別人去猜。猜錯(cuò)了你不是浪費(fèi)別人時(shí)間么?

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2015-09-02 11:10 |只看該作者
本帖最后由 xiaogui_vip 于 2015-09-02 11:11 編輯

回復(fù) 3# jason680


    搜嘎,仔細(xì)看了一下,明白了,是因?yàn)镽S是空格,使用sub格式化了一下。

    謝謝!仔細(xì)咀嚼一下~

論壇徽章:
0
8 [報(bào)告]
發(fā)表于 2015-09-02 11:11 |只看該作者
回復(fù) 6# MMMIX


    什么是隱含的條件? 沒明白你的意思呢,只是當(dāng)作單純的一段文本去處理不好么?

論壇徽章:
145
技術(shù)圖書徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11獅子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龍
日期:2014-01-08 15:26:12技術(shù)圖書徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
9 [報(bào)告]
發(fā)表于 2015-09-02 11:14 |只看該作者
回復(fù) 5# xiaogui_vip

https://www.gnu.org/software/gawk/manual/gawk.html#String-Functions
9.1.3 String-Manipulation Functions

The functions in this section look at or change the text of one or more strings.

....

sub(regexp, replacement [, target])

    Search target, which is treated as a string, for the leftmost, longest substring matched by the regular expression regexp. Modify the entire string by replacing the matched text with replacement. The modified string becomes the new value of target. Return the number of substitutions made (zero or one).

    The regexp argument may be either a regexp constant (/…/) or a string constant ("…"). In the latter case, the string is treated as a regexp to be matched. See Computed Regexps, for a discussion of the difference between the two forms, and the implications for writing your program correctly.

    This function is peculiar because target is not simply used to compute a value, and not just any expression will do—it must be a variable, field, or array element so that sub() can store a modified value there. If this argument is omitted, then the default is to use and alter $0.47 For example:

    str = "water, water, everywhere"
    sub(/at/, "ith", str)

    sets str to ‘wither, water, everywhere’, by replacing the leftmost longest occurrence of ‘a(chǎn)t’ with ‘ith’.

    If the special character ‘&’ appears in replacement, it stands for the precise substring that was matched by regexp. (If the regexp can match more than one string, then this precise substring may vary.) For example:

    { sub(/candidate/, "& and his wife"); print }

    changes the first occurrence of ‘candidate’ to ‘candidate and his wife’ on each input line. Here is another example:

    $ awk 'BEGIN {
    >         str = "daabaaa"
    >         sub(/a+/, "C&C", str)
    >         print str
    > }'
    -| dCaaCbaaa

    This shows how ‘&’ can represent a nonconstant string and also illustrates the “l(fā)eftmost, longest” rule in regexp matching (see Leftmost Longest).

    The effect of this special character (‘&’) can be turned off by putting a backslash before it in the string. As usual, to insert one backslash in the string, you must write two backslashes. Therefore, write ‘\\&’ in a string constant to include a literal ‘&’ in the replacement. For example, the following shows how to replace the first ‘|’ on each line with an ‘&’:

    { sub(/\|/, "\\&"); print }

    As mentioned, the third argument to sub() must be a variable, field, or array element. Some versions of awk allow the third argument to be an expression that is not an lvalue. In such a case, sub() still searches for the pattern and returns zero or one, but the result of the substitution (if any) is thrown away because there is no place to put it. Such versions of awk accept expressions like the following:

    sub(/USA/, "United States", "the USA and Canada")

    For historical compatibility, gawk accepts such erroneous code. However, using any other nonchangeable object as the third parameter causes a fatal error and your program will not run.

    Finally, if the regexp is not a regexp constant, it is converted into a string, and then the value of that string is treated as the regexp to match.

   

論壇徽章:
0
10 [報(bào)告]
發(fā)表于 2015-09-02 11:17 |只看該作者
回復(fù) 9# jason680


    懂你的意思。謝謝
您需要登錄后才可以回帖 登錄 | 注冊(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ū)
中國互聯(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