[英]grep regex to pull out a string between two known strings
I have a string of text in a file that I am parsing out, I almost got it but not sure what I am missing 我在一个文件中有一串文字,我正在解析,我几乎得到了它,但不知道我错过了什么
basic expression I am using is 我正在使用的基本表达是
cat cred.txt | grep -m 1 -o '&CD=[^&]*'
I am getting a results of 我得到的结果
&CD=u8AA-RaF-97gc_SdZ0J74gc_SdZ0J196gc_SdZ0J211
I do not want the &CD=
part in the resulting string, how would I do that. 我不希望结果字符串中的&CD=
部分 ,我该怎么做。
The string I am parsing from is: 我正在解析的字符串是:
webpage.asp?UserName=username&CD=u8AA-RaF-97gc_SdZ0J74gc_SdZ0J196gc_SdZ0J211&Country=USA
If your grep
knows Perl regex: 如果你的grep
知道Perl正则表达式:
grep -m 1 -oP '(?<=&CD=)[^&]*' cred.txt
If not: 如果不:
sed '1s/.*&CD=\([^&]*\).*/\1/' cred.txt
Many ways to skin this cat. 许多方法皮肤这只猫。
Extend your pipe: 扩展你的管道:
grep -o 'CD=[^&]*' cred.txt | cut -d= -f2
Or do a replacement in sed: 或者在sed中进行替换:
sed -r 's/.*[&?]CD=([^&]*).*/\1/' cred.txt
Or get really fancy and parse the actual QUERY_STRING in awk: 或者真正花哨并解析awk中的实际QUERY_STRING:
awk -F'?' '{ split($2, a, "&"); for(i in a){split(a[i], kv, "="); out[kv[1]]=kv[2];} print out["CD"];}'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.