用awk或Mac OS中的sed [英] Using awk or sed in Mac OS
问题描述
我有大约150行的文件,每一行是一个URL的一部分。我想从每一行中提取4个不同的参数,并把它们放入一个文件中。是这样的:
<$p$p><$c$c>/secure/domain/new.aspx?id=620&utm_source=1034&utm_medium=cpc&utm_term=term1&try=1&v=3&utm_account=account_name&utm_campaign=campaign_name&utm_adgroup=adgroup&keyword=keyword1&pkw=pkw1&idimp=id&$p$pmt=$p$pmt1&gclid=id作为试验,我做了
的awk'/ PKW /,/&安培; idimp /文件&gt; output.txt的
心想这下ATLEAST让我值1,但它只是返回的输入文件原样。我究竟做错了什么?此外,如何让它返回所有四个值?我希望得到的关键字,PKW,idimp和preMT。
编辑:预期的输出是一个包含每个150行中输入文件的4个值的文件。因此,
关键字pkw1 IDI preMT1
即使我只得到4种不同的文件中的4个值,那就足够了。
<$p$p><$c$c>s='/helloworld/some/other/standard/URL/mumbo/jumbo/page.aspx?strings&that&I&am¬&interested&in¶m1=value1¶m2=value2¶m3=value3¶m4=value4&some&more&uninteresting&strings'
回声$ S| grep的-o'参数[1234] = [^&放大器;] *|切-d = -f2- |粘贴-d - - - -
值1值2值3 VALUE4
与澄清跟上的问题:
<$p$p><$c$c>s='/secure/domain/new.aspx?id=620&utm_source=1034&utm_medium=cpc&utm_term=term1&try=1&v=3&utm_account=account_name&utm_campaign=campaign_name&utm_adgroup=adgroup&keyword=keyword&pkw=pkw1&idimp=id&$p$pmt=$p$pmt1&gclid=id'回声$ S| grep的-o'\\&LT; \\(关键字\\ | PKW \\ | idimp \\ | preMT \\)= [^&放大器;] *|切-d = -f2- |粘贴-d - - - -
关键字pkw1 ID preMT1
的 \\&LT;
是一个锚这个词开始,以避免像fookeyword
通过awk中,我会写:
的awk -F'[=&放大器;]''
开始 {
#初始化你想要的参数
P [关键字] = P [PKW] = P [idimp] = P [preMT] = 1
}
{
对于(i = 2; I&LT; NF;我+ = 2)
如果(P中$ I)
printf的%S,$(I + 1)
打印
}
I have a file of about 150 lines, where each line is part of a URL. I wanted to extract 4 different parameters from each of the lines and put them into a file. Something like:
/secure/domain/new.aspx?id=620&utm_source=1034&utm_medium=cpc&utm_term=term1&try=1&v=3&utm_account=account_name&utm_campaign=campaign_name&utm_adgroup=adgroup&keyword=keyword1&pkw=pkw1&idimp=id&premt=premt1&gclid=id
As a trial, I did
awk '/pkw/,/&idimp/' file > output.txt
thinking that this would atleast get me value1, but it just returned the input file as is. What am I doing wrong? Also, how to make it return all four values? I'm looking to get keyword, pkw, idimp and premt.
Edit: The expected output is a file containing the 4 values for each of the 150 lines in the input file. So
keyword pkw1 idi premt1
Even if I just get the 4 values in 4 different files, it would suffice.
s='/helloworld/some/other/standard/URL/mumbo/jumbo/page.aspx?strings&that&I&am¬&interested&in¶m1=value1¶m2=value2¶m3=value3¶m4=value4&some&more&uninteresting&strings'
echo "$s" | grep -o 'param[1234]=[^&]*' | cut -d= -f2- | paste -d " " - - - -
value1 value2 value3 value4
Keeping up with the clarifications to the question:
s='/secure/domain/new.aspx?id=620&utm_source=1034&utm_medium=cpc&utm_term=term1&try=1&v=3&utm_account=account_name&utm_campaign=campaign_name&utm_adgroup=adgroup&keyword=keyword&pkw=pkw1&idimp=id&premt=premt1&gclid=id'
echo "$s" | grep -o '\<\(keyword\|pkw\|idimp\|premt\)=[^&]*' | cut -d= -f2- | paste -d " " - - - -
keyword pkw1 id premt1
The \<
is a "start of word" anchor to avoid matching parameters like "fookeyword"
With awk, I'd write:
awk -F '[?=&]' '
BEGIN {
# initialize the parameters you want
p["keyword"] = p["pkw"] = p["idimp"] = p["premt"] = 1
}
{
for (i=2; i<NF; i+=2)
if ($i in p)
printf "%s ", $(i+1)
print ""
}
'
这篇关于用awk或Mac OS中的sed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!