1

我有以下文件:

<CamcorderProfiles cameraId="0">

    <EncoderProfile quality="720p" fileFormat="mp4" duration="30">
        <Video codec="h264"
               bitRate="8000000"
               width="1280"
               height="720"
               frameRate="30" />
        <Audio codec="aac"
               bitRate="96000"
               sampleRate="48000"
               channels="1" />
    </EncoderProfile>

    <EncoderProfile quality="1080p" fileFormat="mp4" duration="30">
        <Video codec="h264"
               bitRate="12000000"
               width="1920"
               height="1080"
               frameRate="30" />
        <Audio codec="aac"
               bitRate="96000"
               sampleRate="48000"
               channels="1" />
    </EncoderProfile>

</CamcorderProfiles>

<CamcorderProfiles cameraId="1">

    <EncoderProfile quality="720p" fileFormat="mp4" duration="30">
        <Video codec="h264"
               bitRate="8000000"
               width="1280"
               height="720"
               frameRate="30" />
        <Audio codec="aac"
               bitRate="96000"
               sampleRate="48000"
               channels="1" />
    </EncoderProfile>

    <EncoderProfile quality="1080p" fileFormat="mp4" duration="30">
        <Video codec="h264"
               bitRate="12000000"
               width="1920"
               height="1080"
               frameRate="30" />
        <Audio codec="aac"
               bitRate="96000"
               sampleRate="48000"
               channels="1" />
    </EncoderProfile>

</CamcorderProfiles>

这是我的代码:

camerastart=$(sed -n '{/<CamcorderProfiles cameraId="0">/=}' $file)
cameraend=$(sed -n $camerastart',$ {/<\/CamcorderProfiles>/=}' $file)

camera1080pstart=$(sed -n $camerastart','$cameraend' {/<EncoderProfile quality="1080p" fileFormat="mp4" duration="30">/=}' $file)

当我打印变量时,除了camera1080p_start之外,我得到了正确的行号,它出现了空白。

camerastart = 3
cameraend = 29
camera1080pstart = 

如果我将代码的第一行更改为cameraId="1",那么我确实会得到适合这种情况的结果。

camerastart = 31
cameraend = 57
camera1080pstart = 45

如果我更改代码以便硬编码前两个变量,我会得到正确的17camera1080pstart 输出。

camerastart="3"
cameraend="29"

camera1080pstart=$(sed -n $camerastart','$cameraend' {/<EncoderProfile quality="1080p" fileFormat="mp4" duration="30">/=}' $file)

有谁知道发生了什么?

4

3 回答 3

1

将第二个命令更改为:

cameraend=$(sed -n $camerastart',$ {/<\/CamcorderProfiles>/=}' $file | head -1)

因为它返回多个行号。但是,这可能不是解决这个特定问题的最佳方法。

于 2013-01-06T04:15:59.467 回答
0

这是一个脚本,它将查找并打印您要更改的行:

$猫tst.awk

BEGIN{ FS = "\"" }
/<CamcorderProfiles cameraId=/ { cameraId = $2 }
/<EncoderProfile quality=/     { quality  = $2 }
/bitRate="12000000"/ {
   if ( (cameraId == "0") && (quality == "1080p") ) {
      print NR, "this is the line I want to change:"
      print
   }
}
$ awk -f tst.awk file
17 this is the line I want to change:
               bitRate="12000000"

如果您需要能够使用不同的 cameraId 和/或质量和/或任何其他值来调用它,那么这是一个微不足道的调整。

如果您告诉我们您想以何种方式更改脚本找到的行,那也很简单。

编辑:根据下面的 OP 评论更新脚本。

$ cat tst.awk
BEGIN{ FS = "\"" }
/<CamcorderProfiles cameraId=/ { cameraId = $2 }
/<EncoderProfile quality=/     { quality  = $2 }
/bitRate="12000000"/ {
   if ( (cameraId == "0") && (quality == "1080p") ) {
      sub(/12000000/,"20000000")
   }
}
{ print }

$ awk -f tst.awk file > tmpfile

$ diff file tmpfile
17c17
<                bitRate="12000000"
---
>                bitRate="20000000"

如果要更新原始文件,只需将其运行为:

awk -f tst.awk file > tmpfile && mv tmpfile file
于 2013-01-06T01:08:09.840 回答
0

对我有用的解决方案是:

camerastart=$(sed -n '{/<CamcorderProfiles cameraId="0">/=}' $file | head -1)
cameraend=$(sed -n $camerastart',$ {/<\/CamcorderProfiles>/=}' $file | head -1)

camera1080pstart=$(sed -n $camerastart','$cameraend' {/<EncoderProfile quality="1080p" fileFormat="mp4" duration="30">/=}' $file | head -1)
于 2013-01-07T23:14:29.070 回答