Page 1 of 1

grep question (Solved)

Posted: Mon Oct 12, 2020 11:06 pm
by taersh
Hi.

I want to get the entry of Name= and Exec= from a .desktop file.
Usually I'm doing it using the commands below:

Code: Select all

MENUPROGNAME=$(grep "^Name=" $MENUTMPLT|sed 's/\Name=//')

Code: Select all

MENUPROGNAME=$(grep "^Exec=" $MENUTMPLT|sed 's/\Exec=//')
This works good as long as there's not multiple Name= and Exec= entries in the .desktop file.

Now I have a problem with some .desktop files, as there's multiple Name= and Exec= entries included.

Code: Select all

[Desktop Entry]
Version=1.0
Type=Application
Name=Stellarium
Name[af]=Stellarium

... snippet...

#Exec=stellarium --startup-script=%f
Exec=stellarium
Icon=stellarium
StartupNotify=false
Terminal=false
Categories=Astronomy;Education;Science;
Comment=Planetarium
Comment[af]=Planetarium
Comment[ar]=قبة سماوية

[Desktop Action Window]
Name=Open in window
Name[af]=Maak in 'n nuwe skerm oop

... snippet...

Exec=stellarium -f no

[Desktop Action Fullscreen]
Name=Open in fullscreen
Name[af]=Maak in 'n volskerm oop

... snippet...

Exec=stellarium -f yes

[Desktop Action Debugmode]
Name=Open in debug mode
Name[af]=Open in ontfout wyse

... snippet...

Exec=stellarium --dump-opengl-details
I only want to get the first of them under [Desktop Entry] and all others suppressed/excluded.
How could I achieve this using shell script commands?

Thanks.

Re: grep question

Posted: Mon Oct 12, 2020 11:51 pm
by fredx181
Hi taersh,

Using -m1 :

Code: Select all

grep -m1 "^Exec="

will print only the first match.

Fred

Re: grep question

Posted: Tue Oct 13, 2020 12:58 am
by taersh
Thanks!

I really should recall more often to use the terminal instead of annoying members by silly questions! :lol:

Code: Select all

grep --help

Re: grep question

Posted: Tue Oct 13, 2020 1:49 am
by Flash
The answers to your "silly questions" educate other people, especially when you mark the thread as "Solved." ;)

Re: grep question

Posted: Tue Oct 13, 2020 3:06 am
by taersh
Flash wrote: Tue Oct 13, 2020 1:49 am The answers to your "silly questions" educate other people, especially when you mark the thread as "Solved." ;)
Done! ;)