sed解析xml file1索引file2

时间:2011-12-17 15:27:44

标签: xml file parsing sed

根据天气代码file1,我需要一个与 /data/weather/weatherIconUrl=http:/someurl (/data/weather/weatherCode=116)中的一行相匹配的衬垫 file2

FILE1

/data/weather/date=2011-12-18
/data/weather/tempMaxC=25
/data/weather/tempMaxF=76
/data/weather/tempMinC=14
/data/weather/tempMinF=56
/data/weather/windspeedMiles=15
/data/weather/windspeedKmph=24
/data/weather/winddirection=NE
/data/weather/winddir16Point=NE
/data/weather/winddirDegree=39
/data/weather/weatherCode=112
/data/weather/weatherIconUrl=http:/someurl
/data/weather/weatherDesc= Cloudy 
/data/weather/precipMM=0.0
/data/weather
/data/weather/date=2011-12-19
/data/weather/tempMaxC=29
/data/weather/tempMaxF=84
/data/weather/tempMinC=17
/data/weather/tempMinF=63
/data/weather/windspeedMiles=13
/data/weather/windspeedKmph=21
/data/weather/winddirection=N
/data/weather/winddir16Point=N
/data/weather/winddirDegree=360
/data/weather/weatherCode=116
/data/weather/weatherIconUrl=http://someurl
/data/weather/weatherDesc=Partly Cloudy 
/data/weather/precipMM=14.1

文件2

/data/weather/weatherCode=111
/data/weather/weatherIconUrl=images/icon1.png
/data/weather/weatherCode=112
/data/weather/weatherIconUrl=images/icon2.png
/data/weather/weatherCode=113
/data/weather/weatherIconUrl=images/icon3.png

----------
/data/weather/weatherCode=120
/data/weather/weatherIconUrl=images/icon20.png

期望的结果(File1应该被编辑)

FILE1(编辑)的

    /data/weather/date=2011-12-18
    /data/weather/tempMaxC=25
    /data/weather/tempMaxF=76
    /data/weather/tempMinC=14
    /data/weather/tempMinF=56
    /data/weather/windspeedMiles=15
    /data/weather/windspeedKmph=24
    /data/weather/winddirection=NE
    /data/weather/winddir16Point=NE
    /data/weather/winddirDegree=39
    /data/weather/weatherCode=112
    /data/weather/weatherIconUrl=images/icon12.png
    /data/weather/weatherDesc= Cloudy 
    /data/weather/precipMM=0.0
    /data/weather
    /data/weather/date=2011-12-19
    /data/weather/tempMaxC=29
    /data/weather/tempMaxF=84
    /data/weather/tempMinC=17
    /data/weather/tempMinF=63
    /data/weather/windspeedMiles=13
    /data/weather/windspeedKmph=21
    /data/weather/winddirection=N
    /data/weather/winddir16Point=N
    /data/weather/winddirDegree=360
    /data/weather/weatherCode=116
    /data/weather/weatherIconUrl=images/icon16.png
    /data/weather/weatherDesc=Partly Cloudy 
    /data/weather/precipMM=14.1

2 个答案:

答案 0 :(得分:0)

awk行应该适用于你:

awk 'FNR==NR{if(NR%2)i=$0;else a[i]=$0;next;}{if($0 in a){print; print a[$0]}else print}' file2 file1

修改

这里我看不到EXTRA线。看到测试。 (也许您的示例数据格式与您的真实数据不同。)。

然而,我已经表明了方法,如何做到这一点。您可以更改awk oneliner以适合您的真实数据格式。

kent$  cat f1
/data/weather/date=2011-12-18
/data/weather/tempMaxC=25
/data/weather/tempMaxF=76
/data/weather/tempMinC=14
/data/weather/tempMinF=56
/data/weather/windspeedMiles=15
/data/weather/windspeedKmph=24
/data/weather/winddirection=NE
/data/weather/winddir16Point=NE
/data/weather/winddirDegree=39
/data/weather/weatherCode=112
/data/weather/weatherIconUrl=http:/someurl
/data/weather/weatherDesc= Cloudy 
/data/weather/precipMM=0.0
/data/weather
/data/weather/date=2011-12-19
/data/weather/tempMaxC=29
/data/weather/tempMaxF=84
/data/weather/tempMinC=17
/data/weather/tempMinF=63
/data/weather/windspeedMiles=13
/data/weather/windspeedKmph=21
/data/weather/winddirection=N
/data/weather/winddir16Point=N
/data/weather/winddirDegree=360
/data/weather/weatherCode=116
/data/weather/weatherIconUrl=http://someurl
/data/weather/weatherDesc=Partly Cloudy 
/data/weather/precipMM=14.1

kent$  cat f2
/data/weather/weatherCode=111
/data/weather/weatherIconUrl=images/icon1.png
/data/weather/weatherCode=112
/data/weather/weatherIconUrl=images/icon2.png
/data/weather/weatherCode=113
/data/weather/weatherIconUrl=images/icon3.png
/data/weather/weatherCode=116
/data/weather/weatherIconUrl=images/icon16.png


kent$  awk 'FNR==NR{if(NR%2)i=$0;else a[i]=$0;next;}{if($0 in a){print; print a[$0]}else print}' f2 f1
/data/weather/date=2011-12-18
/data/weather/tempMaxC=25
/data/weather/tempMaxF=76
/data/weather/tempMinC=14
/data/weather/tempMinF=56
/data/weather/windspeedMiles=15
/data/weather/windspeedKmph=24
/data/weather/winddirection=NE
/data/weather/winddir16Point=NE
/data/weather/winddirDegree=39
/data/weather/weatherCode=112
/data/weather/weatherIconUrl=images/icon2.png
/data/weather/weatherIconUrl=http:/someurl
/data/weather/weatherDesc= Cloudy 
/data/weather/precipMM=0.0
/data/weather
/data/weather/date=2011-12-19
/data/weather/tempMaxC=29
/data/weather/tempMaxF=84
/data/weather/tempMinC=17
/data/weather/tempMinF=63
/data/weather/windspeedMiles=13
/data/weather/windspeedKmph=21
/data/weather/winddirection=N
/data/weather/winddir16Point=N
/data/weather/winddirDegree=360
/data/weather/weatherCode=116
/data/weather/weatherIconUrl=images/icon16.png
/data/weather/weatherIconUrl=http://someurl
/data/weather/weatherDesc=Partly Cloudy 
/data/weather/precipMM=14.1

<强> EDIT2

没有.... sameurl ...

 awk 'FNR==NR{if(NR%2)i=$0;else a[i]=$0;next;}{if($0 in a){print; print a[$0]}else if ($0!~/someurl/) print}' f2 f1

答案 1 :(得分:0)

这可能对您有用:

sed 'N;s|\(.*\)\n\([^=]*\)\(.*\)|\\!\1!,\\!\2!s!http.*!\3!|' file2 | sed -i -f - file1

说明:

从file2生成sed命令以编辑file1。

N.B。我在file2中假设-&#39; s表示与上面相同的模式,直到代码120的最后一个模式(没有空行)。