2

我有一个记录用户输入的日志文件。日志中的每一行都是唯一的,我需要提取 2 个特定项目 - 一个 userId 和一个 URL。我不能只使用awk < file '{print$1, print$6}',因为这些项目并不总是在每一行中的相同位置。

示例文本:

userId='1' managed:no address:123street phone:1234567890 http:/someurl.com
newuser:yes userId='2' managed:yes address:123street  http:/someurl.com
userId='3' address:123 street phone:1234567890 http:/someurl.com
userId='4' managed:no address:123street phone:1234567890 http:/someurl.com

我需要将 userId 和 URL 地址解析为文件,但它们并不总是在每一行中的相同位置。任何建议将不胜感激。

4

3 回答 3

2
$ awk '{for(i=1;$i!~/userId/;i++); print $i, $NF}' file
userId='1' http:/someurl.com
userId='2' http:/someurl.com
userId='3' http:/someurl.com
userId='4' http:/someurl.com
于 2013-08-15T16:54:04.507 回答
1

尝试以下代码:

gawk '{
    for (i=1; i<=NF; i++)
        if ($i ~ "^userId=") id=gensub(/userId=\047([0-9]+)\047/, "\\1", "", $i)
        else if ($i ~ "^http") url=$i
        print "In line "NR", the id is "id" and the url is "url
}' file.txt

样本输入:

userId='1' managed:no address:123street phone:1234567890 http:/someurl1.com
newuser:yes userId='2' managed:yes address:123street  http:/someurl2.com
userId='3' address:123 street phone:1234567890 http:/someurl3.com
userId='4' managed:no address:123street phone:1234567890 http:/someurl4.com

样本输出:

In line 1, the id is 1 and the url is http:/someurl1.com
In line 2, the id is 2 and the url is http:/someurl2.com
In line 3, the id is 3 and the url is http:/someurl3.com
In line 4, the id is 4 and the url is http:/someurl4.com

此解决方案的优势在于可以将 id 或 http 项放在您想要的任何位置。

于 2013-08-14T20:49:03.863 回答
0

awk

awk '{for(c=1;c<NF;c++){if(match($c,/userId/)){print $c,$NF; break}}}' your.file

输出:

userId='1' http:/someurl.com
userId='2' http:/someurl.com
userId='3' http:/someurl.com
userId='4' http:/someurl.com
于 2013-08-14T20:49:04.537 回答