AWK命令将第三个逗号替换为换行,或者换句话说,将数据分成三列。

huangapple go评论116阅读模式
英文:

AWK command replace third comma with new line or in other words make three columns for data

问题

考虑以下awk语句:

  1. 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,网络-有线,等等

而我想要的输出是:

  1. Qt助手,助手,助手
  2. Avahi Zeroconf浏览器,/usr/bin/avahi-discover,网络-有线
  3. kcmshell5 breezestyleconfigpreferences-desktop-themeBreeze Widget风格
  4. Avahi SSH服务器浏览器,/usr/bin/bssh,网络-有线

等等

没有尾随逗号。

有任何建议吗?

英文:

Consider the following awk statement:

  1. 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:

  1. Qt Assistant, assistant, assistant
  2. Avahi Zeroconf Browser, /usr/bin/avahi-discover, network-wired
  3. kcmshell5 breezestyleconfig, preferences-desktop-theme, Breeze Widget Style
  4. Avahi SSH Server Browser, /usr/bin/bssh, network-wired

etc

without trailing comma.

Any suggestions?

答案1

得分: 2

  1. 不能只是按字段的顺序打印它们,因为它们在桌面文件中的顺序可能不是"Name"、"Exec"、"Icon"的顺序。
  2. 也许你想要设置OFS=",",而不是设置ORS

一种方法是先收集值,然后在每个整个文件被读取后再打印:

  1. awk '
  2. BEGIN {
  3. FS = "="
  4. OFS = ", "
  5. }
  6. {
  7. k = $1
  8. sub(/^[^=]*=/,"",$0)
  9. }
  10. k=="Name" { n[FILENAME] = $0 }
  11. k=="Exec" { e[FILENAME] = $0 }
  12. k=="Icon" { i[FILENAME] = $0 }
  13. FNR==1 {
  14. if(pf) print n[pf],e[pf],i[pf]
  15. pf = FILENAME
  16. }
  17. END {
  18. if(pf) print n[pf],e[pf],i[pf]
  19. }
  20. ' /usr/share/applications/*.desktop
英文:
  1. 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.
  2. Perhaps you meant to set OFS=", " rather than setting ORS ?

An approach that collects values, and then prints after each entire file has been read:

  1. awk '
  2. BEGIN {
  3. FS = "="
  4. OFS = ", "
  5. }
  6. {
  7. k = $1
  8. sub(/^[^=]*=/,"",$0)
  9. }
  10. k=="Name" { n[FILENAME] = $0 }
  11. k=="Exec" { e[FILENAME] = $0 }
  12. k=="Icon" { i[FILENAME] = $0 }
  13. FNR==1 {
  14. if(pf) print n[pf],e[pf],i[pf]
  15. pf = FILENAME
  16. }
  17. END {
  18. if(pf) print n[pf],e[pf],i[pf]
  19. }
  20. ' /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

英文:
  1. 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' |

  1. awk 'BEGIN { FS = (_ = "[ \t]*")(__ = "\n")("+")_
  2. RS = "^$"
  3. ___ = (_ = "[^" (OFS = ",") "]+, *")(_)_
  4. _ = ""
  5. } gsub(___,"&" _, $!(NF = NF)) \
  6. gsub(",[^," (_) "]*") _, __) sub("[" (_) "- ,]*$", __)'

  1. Qt Assistant, assistant, assistant
  2. Avahi Zeroconf Browser, /usr/bin/avahi-discover, network-wired
  3. kcmshell5 breezestyleconfig, preferences-desktop-theme, Breeze Widget Style
  4. Avahi SSH Server Browser, /usr/bin/bssh, network-wired

huangapple
  • 本文由 发表于 2023年6月9日 00:00:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定