从shell中的行中提取单词

时间:2016-03-23 10:20:56

标签: bash shell

我有以下几行,

<property id_type="Sabre TripCase - Mobile" modification_timestamp="2015-10-07T09:47:15.0Z" property_id="000002" media_listing_url="/1098828.xml" property_name="WESTIN BAYSHORE VANCOUVER" address_line1="1601 Bayshore Drive" address_line2="" city="Vancouver" state="BC" zip="V6g 2V4" country="Canada" phone="1-604-682-3377" latitude="" longitude="richMediaUrl="http://www.vfmii.com/exc/aspquery?command=invoke&amp;ipid=000002&amp;ids=96102">

<property id_type="Sabre TripCase - Mobile" modification_timestamp="2016-03-10T09:15:50.0Z" property_id="000004" media_listing_url="/1105855.xml" property_name="SWISSOTEL THE STAMFORD" address_line1="2 STAMFORD ROAD" address_line2="" city="Singapore" state="" zip="178882" country="Singapore" phone="65-6338-8585" latitude="" longitude="richMediaUrl="http://www.vfmii.com/exc/aspquery?command=invoke&amp;ipid=000004&amp;ids=96102">"

我需要shell中的命令,它将提取并提供像

这样的值

000002,1098828
000004,1105855

我尝试使用sed,egrep但是一切都给了我整条线。 问候, Ragavan

@Tom你的修复工作正常,除非在某些负面情况下,

<property id_type="Sabre TripCase - Mobile" modification_timestamp="2016-03-10T09:15:50.0Z" property_id="000004" media_listing_url="/1105855.xml" property_name="SWISSOTEL THE STAMFORD" address_line1="2 STAMFORD ROAD" address_line2="" city="Singapore" state="" zip="178882" country="Singapore" phone="65-6338-8585" latitude="" longitude="richMediaUrl="http://www.vfmii.com/exc/aspquery?command=invoke&amp;ipid=000004&amp;ids=96102">"

<property id_type="Sabre TripCase - Mobile" modification_timestamp="2016-03-10T09:15:50.0Z" property_id="000005"  property_name="SWISSOTEL THE STAMFORD" address_line1="2 STAMFORD ROAD" address_line2="" city="Singapore" state="" zip="178882" country="Singapore" phone="65-6338-8585" latitude="" longitude="richMediaUrl="http://www.vfmii.com/exc/aspquery?command=invoke&amp;ipid=000004&amp;ids=96104">"

<property id_type="Sabre TripCase - Mobile" modification_timestamp="2016-03-10T09:15:50.0Z"  media_listing_url="/1105856.xml" property_name="SWISSOTEL THE STAMFORD" address_line1="2 STAMFORD ROAD" address_line2="" city="Singapore" state="" zip="178882" country="Singapore" phone="65-6338-8585" latitude="" longitude="richMediaUrl="http://www.vfmii.com/exc/aspquery?command=invoke&amp;ipid=000004&amp;ids=96104">"

我需要像打印一样打印它 000001,1066545
000005,
,1105856

此致 Ragavan

3 个答案:

答案 0 :(得分:2)

它不是特别灵活,但这适用于您显示的数据:

sed -E 's/.*property_id="([0-9]+)".*media_listing_url="[^0-9]*([0-9]+).*/\1,\2/' file

使用sed捕获您感兴趣的部分(每个属性值中的数字)并在替换字符串中使用它们,用逗号分隔。

鉴于您的要求更复杂,我建议使用类似的东西(需要GNU awk):

awk '{ match($0, /property_id="([0-9]+)"/, id); 
    match($0, /media_listing_url="[^0-9]*([0-9]+)/, url); 
    print id[1] "," url[1] }' file

match的第三个参数指定一个数组,当没有匹配时清除该数组,并在存在时填充捕获的组。

答案 1 :(得分:0)

Pure awk解决方案

1。一个简单的基于分隔符的解析。

awk -F'"' '{print $6 "," $8}' file.txt | awk -F/ '{print $1 $2}' | awk -F. '{print $1}'

您的输入数据应位于 file.txt

2。 awk with gsub

awk -F'"' '{gsub(".xml","");gsub("/",""); print $6 "," $8}' file.txt

3。 perl解决方案

提取属性ID

perl -ne 'print "$1 \n" if /(?<=property_id=\")(.+?)(?=\")/' <file.txt >file1.txt

结果:

000002
000004

提取media_listing_url

perl -ne 'print "$1 \n" if /(?<=media_listing_url=\"\/)(.+?)(?=\.)/' <file.txt >file2.txt

结果:

1098828
1105855

现在加入两个值: -

paste -d, file1.txt file2.txt

结果:

000002,1098828
000004,1105855

答案 2 :(得分:-1)

您是否尝试过使用-o参数的egrep?