![](/img/trans.png)
[英]shell script to Find a particular Pattern in repetitive data a file . and add new blank line before matched pattern
[英]shell script(AIX) : finding a string after the matched pattern line by line in a file
我在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
你可以看一下
使用grep
:
$ grep -Eo '(country|language)=[^&]*' file
country=us
language=en
$ grep -Po '(?<=country=|language=)[^&]*' file
us
en
# Grep Options
-o, --only-matching show only the part of a line matching PATTERN
-E, --extended-regexp PATTERN is an extended regular expression (ERE)
-P, --perl-regexp PATTERN is a Perl regular expression
使用sed
:
sed -E 's/.*country=([^&]*).*language=([^&]*).*/\1 \2/g' file
us en
# Sed option
-E use extended regular expression
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.