我有以下文件:
<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
如果我更改代码以便前两个变量被硬编码,我会得到17
camera1080pstart的正确输出。
camerastart="3"
cameraend="29"
camera1080pstart=$(sed -n $camerastart','$cameraend' {/<EncoderProfile quality="1080p" fileFormat="mp4" duration="30">/=}' $file)
任何人都知道发生了什么事?
答案 0 :(得分:1)
将第二个命令更改为:
cameraend=$(sed -n $camerastart',$ {/<\/CamcorderProfiles>/=}' $file | head -1)
因为它返回多个行号。但是,这可能不是解决这一特定问题的最佳方法。
答案 1 :(得分:0)
这是一个脚本,可以找到并打印您想要更改的行:
$ cat 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
答案 2 :(得分: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)