英文:
AWK command replace third comma with new line or in other words make three columns for data
问题
考虑以下awk语句:
awk -v ORS=", " ' /^(Name|Exec|Icon)=/{sub(/Name=|Exec=|Icon=/,"");print}' /usr/share/applications/*.desktop
输出是在manjaro Linux发行版中/usr/share/application/文件夹中已安装应用程序的.desktop
文件的“Name=/Exec=/Icon”部分,用逗号分隔,并带有尾随逗号:
Qt助手,助手,助手,Avahi Zeroconf浏览器,/usr/bin/avahi-discover,网络-有线,kcmshell5 breezestyleconfig,preferences-desktop-theme,Breeze Widget风格,Avahi SSH服务器浏览器,/usr/bin/bssh,网络-有线,
等等
而我想要的输出是:
Qt助手,助手,助手
Avahi Zeroconf浏览器,/usr/bin/avahi-discover,网络-有线
kcmshell5 breezestyleconfig,preferences-desktop-theme,Breeze Widget风格
Avahi SSH服务器浏览器,/usr/bin/bssh,网络-有线
等等
没有尾随逗号。
有任何建议吗?
英文:
Consider the following awk statement:
awk -v ORS=", " ' /^(Name|Exec|Icon)=/{sub(/Name=|Exec=|Icon=/,"");print}' /usr/share/applications/*.desktop
The output is the "Name=/Exec=/Icon" of .desktop
files of installed applications in /usr/share/application/ folder in manjaro Linux distro, separated with commas and with trailing comma:
Qt Assistant, assistant, assistant, Avahi Zeroconf Browser, /usr/bin/avahi-discover, network-wired, kcmshell5 breezestyleconfig, preferences-desktop-theme, Breeze Widget Style, Avahi SSH Server Browser, /usr/bin/bssh, network-wired,
etc
The output that I want instead is:
Qt Assistant, assistant, assistant
Avahi Zeroconf Browser, /usr/bin/avahi-discover, network-wired
kcmshell5 breezestyleconfig, preferences-desktop-theme, Breeze Widget Style
Avahi SSH Server Browser, /usr/bin/bssh, network-wired
etc
without trailing comma.
Any suggestions?
答案1
得分: 2
- 不能只是按字段的顺序打印它们,因为它们在桌面文件中的顺序可能不是"Name"、"Exec"、"Icon"的顺序。
- 也许你想要设置
OFS=","
,而不是设置ORS
?
一种方法是先收集值,然后在每个整个文件被读取后再打印:
awk '
BEGIN {
FS = "="
OFS = ", "
}
{
k = $1
sub(/^[^=]*=/,"",$0)
}
k=="Name" { n[FILENAME] = $0 }
k=="Exec" { e[FILENAME] = $0 }
k=="Icon" { i[FILENAME] = $0 }
FNR==1 {
if(pf) print n[pf],e[pf],i[pf]
pf = FILENAME
}
END {
if(pf) print n[pf],e[pf],i[pf]
}
' /usr/share/applications/*.desktop
英文:
- You can't just print the fields when you see them as they may not appear in the order name,exec,icon in the desktop file.
- Perhaps you meant to set
OFS=", "
rather than settingORS
?
An approach that collects values, and then prints after each entire file has been read:
awk '
BEGIN {
FS = "="
OFS = ", "
}
{
k = $1
sub(/^[^=]*=/,"",$0)
}
k=="Name" { n[FILENAME] = $0 }
k=="Exec" { e[FILENAME] = $0 }
k=="Icon" { i[FILENAME] = $0 }
FNR==1 {
if(pf) print n[pf],e[pf],i[pf]
pf = FILENAME
}
END {
if(pf) print n[pf],e[pf],i[pf]
}
' /usr/share/applications/*.desktop
答案2
得分: 1
Qt Assistant, assistant, assistant
Avahi Zeroconf Browser, /usr/bin/avahi-discover, network-wired
kcmshell5 breezestyleconfig, preferences-desktop-theme, Breeze Widget Style
Avahi SSH Server Browser, /usr/bin/bssh, network-wired
英文:
echo 'Qt Assistant, assistant, assistant, Avahi Zeroconf Browser, /usr/bin/avahi-discover, network-wired, kcmshell5 breezestyleconfig, preferences-desktop-theme, Breeze Widget Style, Avahi SSH Server Browser, /usr/bin/bssh, network-wired' |
awk 'BEGIN { FS = (_ = "[ \t]*")(__ = "\n")("+")_
RS = "^$"
___ = (_ = "[^" (OFS = ",") "]+, *")(_)_
_ = ""
} gsub(___,"&" _, $!(NF = NF)) \
gsub(",[^," (_) "]*") _, __) sub("[" (_) "- ,]*$", __)'
Qt Assistant, assistant, assistant
Avahi Zeroconf Browser, /usr/bin/avahi-discover, network-wired
kcmshell5 breezestyleconfig, preferences-desktop-theme, Breeze Widget Style
Avahi SSH Server Browser, /usr/bin/bssh, network-wired
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论