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

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

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

  1. 不能只是按字段的顺序打印它们,因为它们在桌面文件中的顺序可能不是"Name"、"Exec"、"Icon"的顺序。
  2. 也许你想要设置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
英文:
  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:

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

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:

确定