linux - shell脚本(AIX) : finding a string after the matched pattern line by line in a file

标签 linux shell unix aix

我在 AIX 环境中有一个日志文件,其中包含如下行

10.100.108.23 100.10.10.11 - [05/Dec/2012:09:35:27 +0000] "GET /chgs/checkprofile/checkServlet?requestType=signPart1&off=false&oquestions=true&userid=false&source=false&link=%23&country=us&language=en&origin=&displayLayer=no HTTP/1.1" 200 8904 "https://www.test.com/services/request/Home.action" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)" "PD-ERR=; rlang=nl_NL;

我需要分别在模式 &country&language 之后的每一行中找到国家/地区和语言。由于我是 shell 脚本编写的新手,我尝试使用 grep 和 awk 脚本来实现此目的,但没有成功。

最佳答案

awk -F"&" '{for(i=1;i<=NF;i++)if($i~/country/ ||$i~/language/){split($i,a,"=");printf a[2]" "}}' your_file

或者您可以使用 sed:

sed -e 's/.*country=//g;s/language=\([^\&]*\)&.*/\1/g' your_file

删除&符号:

> sed -e 's/.*country=//g;s/&language=\([^\&]*\)&.*/ \1/g' temp
us en

您可以阅读this

关于linux - shell脚本(AIX) : finding a string after the matched pattern line by line in a file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13762296/

相关文章:

linux - Shell-fu 列出目录中包含单词或短语实例的所有文件

Unix/Mac OS X 上的 C++ UTF-8 支持

unix - 使用 xargs 从文档中查找行

java - RHEL Eclipse 无法加载 JNI 共享库 libjvm.so

python - 在 Python 中读取带有特殊字符的参数时出现问题

linux - 如何在命令中插入循环

php - 如何在linux中使用PHP调用另一个CLI.exe进程I/O?

linux - Find -type f 有限制

linux - xorg源代码复制粘贴

linux - 编译 xcbproto 1.7 时出错