You are not logged in.

#1 2010-07-10 02:27:43

lostinpurdy
Member
Registered: 2010-05-02
Posts: 12

Script to find a specific section in file & insert text below - how?

I am dealing with kind of a problem here - I am using pekwm as my window manager and I use menumaker to automatically update the menu on every startup. My problem is that the original pekwm, the first two sections called 'terminal' and 'Run' and the last two sections called 'Go To' and 'pekwm' are overwritten/modified by mmaker so that there's only the applications submenues and  'pekwm' with the 'Themes' section erased and I want to somehow preserve all that. I was thinking of creating a bash script that does the mmaker thing and then use 'sed' or 'echo' to insert the code back into the appropiate areas of ~/.pekwm/menu.
All I really need to know is how to find a particular string of code in the menu file and insert a block of text below that line using sed or echo.

Any ideas?

Last edited by lostinpurdy (2010-07-10 02:32:38)

Offline

#2 2010-07-10 02:35:29

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Script to find a specific section in file & insert text below - how?

well, a sed onliner could probably do it, so maybe hold out for that -- but i used to have a bash script to do this sort of thing (i've since deleted it)...

basically:

get the line number of the marker (the text you want to insert after):

n=$(grep -m 1 -n 'some regex' ./file | cut -d ':' -f 1)

then put up until that line to a temp file

head -n $n ./file > ./file.tmp

write what you want

cat >> ./file.tmp << EOF
this is my awesomeness
EOF

then tail what's after the mark (note the negative) into the file after our stuff:

tail -n -$((n+1)) ./file >> ./file.tmp

finally just

mv ./file.tmp ./file

*disclaimer* -- top of my head stuff here, my math may be off by a line or two and the negative arguments to head/tail may work differently than i remember, please test this out with some simple sample files to smooth out the logic.

Offline

#3 2010-07-10 03:41:19

lostinpurdy
Member
Registered: 2010-05-02
Posts: 12

Re: Script to find a specific section in file & insert text below - how?

Looks interesting. But I'd like to keep it as simple as possible and avoid the part with the extra textfiles and just insert the code text straight into the menu file, kind of like this:

mmaker --no-desktop -vf pekwm


sed {
        find-and-goto-line-below-'root menu'-declaration, insert-text {
                                                                                                       entry for 'terminal'
                                                                                                       entry for 'run"
                                                                                                       }
       ~/.pekwm/menu
       }

sed {
         find-line-below-'pekwm'-submenu-declaration, insert-text {
                                                                                                   entry for 'themes'
                                                                                                  }
       ~/.pekwm/menu
       }

Obviously it's not real code but it's just to outline how I'd like to have it. Just need to know the correct syntax to make it work. I don't care much for 'Go To' and the rest.

Last edited by lostinpurdy (2010-07-10 03:48:10)

Offline

#4 2010-07-10 05:53:49

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: Script to find a specific section in file & insert text below - how?

why not sed -e 's,root menu,&\nterminal\nrun\n,' -e 's,pekwm submenu declaration,&\nthemes\n,' ?

Edit: I mean why don't you structure your pseudocode like that? You should try learning a little about sed before asking questions on it.

Last edited by fsckd (2010-07-10 06:06:21)


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#5 2010-07-10 12:57:33

Bralkein
Member
Registered: 2004-10-26
Posts: 354

Re: Script to find a specific section in file & insert text below - how?

I think what you're looking for is sed's "a" command. To insert the text "foo" after the line "bar" in the file myfile you can do

sed '/bar/ a foo' myfile

If you want sed to modify the file in place (rather than just printing the new version to stdout) then you can use the same command with sed -i.

Edit: Oh, and I feel I should point out that the above command will insert the text after every line in the file which matches the /bar/ regexp so it will also insert foo after a line containing "barry" or "lumbar". Just in case that might catch you out smile

Last edited by Bralkein (2010-07-10 13:03:24)

Offline

Board footer

Powered by FluxBB