awk 命令在看起来不同的文件中查找

发布于 2024-09-18 17:31:03 字数 979 浏览 3 评论 0原文

为什么这行不通?它找到所有其他文件,但找不到这个文件。唯一的区别是它的名称中有数字。

awk -F= '$1=="Icon" {print $2}' "/usr/share/applications/hildon/dropn900.desktop"

它使用的文件..

 [Desktop Entry]
 Version=1.0.0
 Encoding=UTF-8
 Name=DropN900
 Comment=Python based DropBox client
 Exec=/opt/dropn900/dropn900.py
 Icon=dropn900
 X-Icon-path=/usr/share/icons
 X-Window-Icon=dropn900
 Type=Application
 X-Osso-Type=application/x-executable

它应该给我“dropn900”的输出,但没有。


如果我按照下面的建议进行操作..

awk -F= '$1==" Icon" {print $2}' "/usr/share/applications/hildon/fapman.desktop"
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Name=Faster Application Manager
Exec=fapman.launch
Icon=fapman
X-Osso-Type=application/x-executable
X-Osso-Service=org.maemo.faster_application_manager
Categories=System;

这将不会显示..

丹尼斯建议的给出了输出。

dropn900
/usr/share/icons
dropn900

但我只需要“dropn900”,否则脚本将无法工作。

Why do not this work? It finds all the other files but not this one. The only difference is that it has numbers in its name.

awk -F= '$1=="Icon" {print $2}' "/usr/share/applications/hildon/dropn900.desktop"

The file it uses..

 [Desktop Entry]
 Version=1.0.0
 Encoding=UTF-8
 Name=DropN900
 Comment=Python based DropBox client
 Exec=/opt/dropn900/dropn900.py
 Icon=dropn900
 X-Icon-path=/usr/share/icons
 X-Window-Icon=dropn900
 Type=Application
 X-Osso-Type=application/x-executable

It should give me the output of "dropn900" but doesn't.


If i do as suggested below..

awk -F= '$1==" Icon" {print $2}' "/usr/share/applications/hildon/fapman.desktop"
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Name=Faster Application Manager
Exec=fapman.launch
Icon=fapman
X-Osso-Type=application/x-executable
X-Osso-Service=org.maemo.faster_application_manager
Categories=System;

This will not show..

The one suggested by Dennis gave the output.

dropn900
/usr/share/icons
dropn900

But i need just "dropn900" or the script will not work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身仙ぐ女味 2024-09-25 17:31:04
awk -F= '$1~/^ *Icon/ {print $2}' file
awk -F= '$1~/^ *Icon/ {print $2}' file
梦言归人 2024-09-25 17:31:04

由于您使用等于而不是空格作为字段分隔符,因此 $1 实际上是 " Icon" 带有前导空格。尝试

awk -F= '$1==" Icon" {print $2}'

一下您的文件,您会发现名称中包含数字的文件确实无关紧要。

Since you're using equals, not space, as the field separator, $1 is actually " Icon" with a leading space. Try

awk -F= '$1==" Icon" {print $2}'

on your file, and you'll see that the file having numbers in its name is really irrelevant.

后知后觉 2024-09-25 17:31:04

如果您的某些文件每行都有前导空格,而另一些则没有,则此正则表达式匹配可能会有所帮助:

awk -F= '$1 ~ " *Icon" {print $2}'

If some of your files have leading whitespace on each line and some don't, this regex match may help:

awk -F= '$1 ~ " *Icon" {print $2}'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文