bash – 从不同文件中grep多行,并将它们打印到xml
发布时间:2020-12-15 21:07:07 所属栏目:安全 来源:网络整理
导读:我必须创建一个bash脚本,用于处理来自不同文件的多个匹配项.这些文件都包含在目录中,具有相同的扩展名(.desktop).这是一个例子: [Desktop Entry]Icon=text-x-c++srcName=button.cppType=LinkURL[$e]=file://$HOME/Configs/button.cppX-KDE-LastOpenedWith=k
我必须创建一个bash脚本,用于处理来自不同文件的多个匹配项.这些文件都包含在目录中,具有相同的扩展名(.desktop).这是一个例子:
[Desktop Entry] Icon=text-x-c++src Name=button.cpp Type=Link URL[$e]=file://$HOME/Configs/button.cpp X-KDE-LastOpenedWith=kate 匹配是URL,LastOpenedWith,Name和Icon.它们中的每一个都必须存储到一个不同的变量(例如$Name,$URL等)中,这些变量将被打印到xml文件中,相应于这个sintax: <action label="$Name" icon="$Icon" exec="$LastOpenedWith $URL"/> 为所有.desktop文件创建单个条目. 我真的不知道怎么做,因为我是bash脚本的初学者,任何提示都会受到欢迎:) 解决方法
像这样的东西可能会起作用:
echo -e "<menu>n<submenu>" > output.xml for f in *.desktop; do Icon=$(awk -F= '/Icon/{print $2}' "$f") Name=$(awk -F= '/Name/{print $2}' "$f") URL=$(awk -F= '/URL/{print $2}' "$f") LastOpenedWith=$(awk -F= '/X-KDE-LastOpenedWith/{print $2}' "$f") echo "<action label="$Name" icon="$Icon" exec="$LastOpenedWith $URL"/>" done >> output.xml echo -e "</submenu>n</menu>" >> output.xml 使用Config::IniFiles的Perl解决方案可能如下所示: #!/usr/bin/env perl use strict; use warnings; use Config::IniFiles; open XML,">/path/to/output.xml" or die $!; print XML "<menu>n<submenu>n"; foreach my $file (</path/to/*.desktop>) { my $ini = Config::IniFiles->new( -file => "$file" ); printf XML "<action label='%s' icon='%s' exec='%s %s'/>n",$ini->val('Desktop Entry','Name'),'Icon'),'X-KDE-LastOpenedWith'),'URL[$e]') } print XML "</submenu>n</menu>n"; close XML; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |