Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 在文件中查找文本并获取所需内容_Linux_Grep - Fatal编程技术网

Linux 在文件中查找文本并获取所需内容

Linux 在文件中查找文本并获取所需内容,linux,grep,Linux,Grep,我有许多访问日志文件的权限。这是他们档案中的一行 access_log.20111215:111.222.333.13 - - [15/Dec/2011:05:25:00 +0900] "GET /index.php?uid=01O9m5s23O0p&p=nutty&a=check_promotion&guid=ON HTTP/1.1" 302 - "http://xxx.com/index.php?uid=xxx&p=mypage&a=index&

我有许多访问日志文件的权限。这是他们档案中的一行

access_log.20111215:111.222.333.13 - - [15/Dec/2011:05:25:00 +0900] "GET /index.php?uid=01O9m5s23O0p&p=nutty&a=check_promotion&guid=ON HTTP/1.1" 302 - "http://xxx.com/index.php?uid=xxx&p=mypage&a=index&sid=&fid=&rand=1681" "Something/2.0 qqq(xxx;yyy;zzz)" "-" "-" 0
如何从出现“p=nutty&a=check\u promotion”的行中提取uid“01O9m5s23O0p”,并输出到新文件

例如,“output.txt”文件应为:

01O9m5s23O0p
01O9m5s0999p
01O9m5s3249p
fFDSFewrew23
SOMETHINGzzz
...
我试过:

grep "p=nutty&a=check_promotion" access* > using_grep.out

但它会打印整行。我只是想得到你的身份证

摘要:

1) Find the lines which have "p=nutty&a=check_promotion"

2) Extract uid from those lines.

3) Print them to a file.
[jaypal:~/Temp] cat file
access_log.20111215:210.136.161.13 - - [15/Dec/2011:05:25:00 +0900] "GET /index.php?uid=01O9m5s23O0p&p=nutty&a=check_promotion&guid=ON HTTP/1.1" 302 - "http://xxx.com/index.php?uid=xxx&p=mypage&a=index&sid=&fid=&rand=1681" "Something/2.0 qqq(xxx;yyy;zzz)" "-" "-" 0 
[jaypal:~/Temp] awk -v FS="[?&=]" '
$0~/p=nutty&a=check_promotion/{ print $3 > "output_file"}' input_file
[jaypal:~/Temp] cat output_file 
01O9m5s23O0p

要做到这一点,分三个阶段:

(格式化以避免滚动)


要做到这一点,分三个阶段:

(格式化以避免滚动)


如果具有
p=nutty&a=check\u升级
的行在性质上相似,那么我们可以设置分隔符并使用
awk
提取uid并将其放入文件中

awk -v FS="[?&=]" '
$0~/p=nutty&a=check_promotion/{ print $3 > "output_file"}' input_file
测试:

1) Find the lines which have "p=nutty&a=check_promotion"

2) Extract uid from those lines.

3) Print them to a file.
[jaypal:~/Temp] cat file
access_log.20111215:210.136.161.13 - - [15/Dec/2011:05:25:00 +0900] "GET /index.php?uid=01O9m5s23O0p&p=nutty&a=check_promotion&guid=ON HTTP/1.1" 302 - "http://xxx.com/index.php?uid=xxx&p=mypage&a=index&sid=&fid=&rand=1681" "Something/2.0 qqq(xxx;yyy;zzz)" "-" "-" 0 
[jaypal:~/Temp] awk -v FS="[?&=]" '
$0~/p=nutty&a=check_promotion/{ print $3 > "output_file"}' input_file
[jaypal:~/Temp] cat output_file 
01O9m5s23O0p

如果具有
p=nutty&a=check\u升级
的行在性质上相似,那么我们可以设置分隔符并使用
awk
提取uid并将其放入文件中

awk -v FS="[?&=]" '
$0~/p=nutty&a=check_promotion/{ print $3 > "output_file"}' input_file
测试:

1) Find the lines which have "p=nutty&a=check_promotion"

2) Extract uid from those lines.

3) Print them to a file.
[jaypal:~/Temp] cat file
access_log.20111215:210.136.161.13 - - [15/Dec/2011:05:25:00 +0900] "GET /index.php?uid=01O9m5s23O0p&p=nutty&a=check_promotion&guid=ON HTTP/1.1" 302 - "http://xxx.com/index.php?uid=xxx&p=mypage&a=index&sid=&fid=&rand=1681" "Something/2.0 qqq(xxx;yyy;zzz)" "-" "-" 0 
[jaypal:~/Temp] awk -v FS="[?&=]" '
$0~/p=nutty&a=check_promotion/{ print $3 > "output_file"}' input_file
[jaypal:~/Temp] cat output_file 
01O9m5s23O0p

+1表示
-o
。但是也许应该是
grep-E-o'uid=[^&]*'| cut-b5-
或者类似的东西来捕获uid?@Johnsyweb如果uid有随机格式(也有字母数字格式),该怎么办?例如:uid=ureisdks21323zzzzzzzt然后使用awk,正如@Jaypal建议的那样:
awk-F'[?&=]''/p=nutty&a=check_promotion/{print$3}access*>output.txt
+1 for
-o
。但是也许应该是
grep-E-o'uid=[^&]*'| cut-b5-
或者类似的东西来捕获uid?@Johnsyweb如果uid有随机格式(也有字母数字格式),该怎么办?例如:uid=ureisdks21323zzzzzzzt然后使用awk,正如@Jaypal建议的那样:
awk-F'[?&=]''/p=nutty&a=check_promotion/{print$3}'access*>output.txt
+1:这可能是解决此问题的最佳方法。你可以把它缩短为
awk-F'[?&=]'/p=nutty&a=check\u promotion/{print$3}'access*>output.txt
。没错,一开始我有点怀疑,因为有这么多分隔符,我更热衷于使用
$0~/../..//code>而不是
/..//code>。但我想这两种方法都有效+1:这可能是解决这个问题的最好办法。你可以把它缩短为
awk-F'[?&=]'/p=nutty&a=check\u promotion/{print$3}'access*>output.txt
。没错,一开始我有点怀疑,因为有这么多分隔符,我更热衷于使用
$0~/../..//code>而不是
/..//code>。但我想这两种方法都有效