LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-15-2021, 01:22 PM   #1
arifd86
Member
 
Registered: Aug 2009
Distribution: Ubuntu 19.04
Posts: 30

Rep: Reputation: 5
Help me sed or awk this figity config file please!


So if I want to programmatically add an app to the quicklaunch of lxqt-panel, they've made it somewhat frustratingly difficult to do so.

Among the config file is this relevant section
Code:
[quicklaunch]
alignment=Left
apps\1\desktop=/usr/share/applications/pcmanfm-qt.desktop
apps\2\desktop=/usr/share/applications/pavucontrol-qt.desktop
apps\size=2
type=quicklaunch
I want some sed or awk code that will find this line 'apps\size=' followed by an unkwnown number, which has to appear after a line containing '[quicklaunch]' but before any empty lines. the number must be incremented by 1 and stored into a variable so that we can then inject into the line above our desired .desktop file.

So that in this concrete example, the result will look like this:
Code:
[quicklaunch]
alignment=Left
apps\1\desktop=/usr/share/applications/pcmanfm-qt.desktop
apps\2\desktop=/usr/share/applications/pavucontrol-qt.desktop
apps\3\desktop=/path/to/app.desktop
apps\size=3
type=quicklaunch
This is rather advanced awk manipulation for me, so I appreciate your help.

Thanks!
 
Old 01-15-2021, 01:27 PM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
I'd suggest you using crudini for this.
Code:
file=panel.conf
sect=quicklaunch
param='apps\size'
crudini --set "$file" "$sect" "$param" \
  $(( $(crudini --get "$file" "$sect" "$param" 2>/dev/null)+1 ))
awk may look easier in this particular case
Code:
awk -F= -vOFS== \
  '/^\[quicklaunch\]$/,/^$/{if($1=="apps\\size")$2++}1' \
  panel.conf
but I prefer using dedicated tools where they exist.

Last edited by shruggy; 01-15-2021 at 02:00 PM.
 
Old 01-15-2021, 03:54 PM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,805

Rep: Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204Reputation: 1204
The following adds and increments
Code:
awk -v insert="/path/to/app.desktop" '
  BEGIN { FS=OFS="=" } $1=="[quicklaunch]" { q=1 } NF==0 { q=0 } q && $1=="apps\\size" { print ("apps\\" ++$2 "\\desktop"), insert } 1
' panel.conf
 
1 members found this post helpful.
Old 01-16-2021, 04:51 AM   #4
arifd86
Member
 
Registered: Aug 2009
Distribution: Ubuntu 19.04
Posts: 30

Original Poster
Rep: Reputation: 5
Great stuff! I really gotta learn AWK properly one day! Thank you.

Thanks for the crudini suggestion also, but awk is preferable for a single task than relying on and installing extra software.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed inside awk or awk inside awk maddyfreaks Linux - Newbie 4 06-29-2016 01:10 PM
Please help with sed and awk script master-of-puppets Programming 3 10-01-2014 10:07 AM
[SOLVED] Once again... awk.. awk... awk shivaa Linux - Newbie 13 12-31-2012 04:56 AM
Please help compare 2 files via awk/sed Seemoi Programming 10 06-24-2012 01:23 PM
Parsing through a Nagios config file to extract info w/ Sed, Awk, Vi, etc. chudster Linux - General 3 10-14-2010 02:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration