Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用bash脚本更改xml文件的时间格式_Bash_Unix_Sed - Fatal编程技术网

使用bash脚本更改xml文件的时间格式

使用bash脚本更改xml文件的时间格式,bash,unix,sed,Bash,Unix,Sed,我正在尝试使用bash脚本更改文件中时间的格式 当前时间格式: 08:05:00 目标时间格式: 8-05 文件中还有其他我不想更改的时间戳,我想更改的每个实例都用xml包装: time=“当前时间格式” 有人能帮忙吗 必须使用XML解析器来解决此问题。我会用一种带有XML解析库和日期时间解析库的语言来完成。巨蟒符合要求 将xml.etree.ElementTree作为ET导入 从日期时间导入日期时间 导入系统 tree=ET.parse(sys.argv[1]) #对于每个具有“time”属性

我正在尝试使用bash脚本更改文件中时间的格式

当前时间格式: 08:05:00

目标时间格式: 8-05

文件中还有其他我不想更改的时间戳,我想更改的每个实例都用xml包装:

time=“当前时间格式”


有人能帮忙吗

必须使用XML解析器来解决此问题。我会用一种带有XML解析库和日期时间解析库的语言来完成。巨蟒符合要求

将xml.etree.ElementTree作为ET导入
从日期时间导入日期时间
导入系统
tree=ET.parse(sys.argv[1])
#对于每个具有“time”属性的元素,更改属性值的格式
对于tree.findall('./*[@time]')中的元素:
time=datetime.strtime(elem.get('time'),“%H:%M:%S”)
元素集('time',time.strftime('%k-%M').lstrip())
#将新XML打印到标准输出
打印(bytes.decode(ET.tostring(tree.getroot()))
测试:

$ cat file.xml
<root>
<a>
<b time="08:05:00">
<c>text contains time 08:05:00</c>
</b>
<d foo="bar" time="19:54:55"/>
</a>
</root>

$ python3 alter_time.py file.xml > new.file.xml

$ cat new.file.xml
<root>
<a>
<b time="8-05">
<c>text contains time 08:05:00</c>
</b>
<d foo="bar" time="19-54" />
</a>
</root>
$cat file.xml
文本包含时间08:05:00
$python3 alter_time.py file.xml>new.file.xml
$cat new.file.xml
文本包含时间08:05:00

作为练习处理左侧错误

请创建一个。特别是您需要不想更改的日期示例和XML文件的完整摘录。至少您应该发布您的输入XML内容。我们是否猜到您每次都想更改未包装为XML的内容?因此,要更改的格式可能是
,或者必须是
时间=
?也不应该更改
XML08:05:00
?提供一个包含2、3个变体的最小示例(包括角案例)就可以了。