Page 1 of 1

Help needed getting ramdisk_size using AWK (or sed) (SOLVED)

Posted: Sun 30 Oct 2005, 09:08
by pakt
I'm working on an update to WakePup 1.0 that will also boot non-standard Puppies (Chubby, Murga, PizzaPup, etc) that use the ramdisk_size=xxxxx parameter when booting.

Some help is needed in extracting ramdisk_size=xxxxx from either isolinux.cfg (on CD) or syslinux.cfg (on USB flash drive).

The tools I have at hand are AWK & sed for DOS.

Here are extracts showing the relevant lines in the files:

(From puppy-1.0.5-mozilla-128Mb-Murga's isolinux.cfg)

Code: Select all

default 1
DISPLAY BOOT.MSG
prompt 1
label 1
 kernel vmlinuz
 append root=/dev/ram0 initrd=image.gz ramdisk_size=63488 PFILE=pup001-none-262144
<--snip-->
(And from a modified puppy-1.0.5-mozilla-128Mb-Murga USB syslinux.cfg)

Code: Select all

default vmlinuz root=/dev/ram0 initrd=image.gz
append ramdisk_size=63488 PSLEEP=25 PHOME=sda1 PFILE=pup100-none-262144 PKEYS=uk
What I have succeded in doing so far is to extract ramdisk_size=63488 like this (remember, I'm using AWK for DOS punctuation (" instead of '):

Code: Select all

M:\awk "/ramdisk_size/{print \"set rdsize=\" $2;exit;}" %pupdrv%\syslinux.cfg >M:\tmp_.bat
call M:\tmp_.bat
del M:\tmp_.bat >NUL
This will set the FreeDOS environment variable rdsize=ramdisk_size=63488 and WakePup can then successfully boot into Puppy using this. Note that this only works when the variable is found in field #2. That's where the potential problem lies: the boot parameters can be written in a different order - ramdisk_size doesn't have to be in field #2, someone might place it at the end of the parameter line...

So here's the question: can anyone with a better knowledge of AWK syntax than me (that's just about anyone ;)) show me how to extract 'ramdisk_size=XXXXX' regardless of which field it is in?

Thanks in advance for any help :)

Paul

Posted: Sun 30 Oct 2005, 16:04
by rarsa
Can there be spaces around the '=' ?

e.g.

Code: Select all

append root =/dev/ram0 initrd= image.gz ramdisk_size = 63488 PFILE = pup001-none-262144

Posted: Sun 30 Oct 2005, 16:20
by rarsa
I know that you said that you only had access to awk and sed. but... do you have access to grep?

Code: Select all

grep -o 'ramdisk_size[ ]*=[ ]*[0-9]*'  %pupdrv%/syslinux.cfg
This gives you exactly what you are asking for. In any event, That's the regular expression you want to use to find the ramdisk_size even if you are not using grep.

Posted: Sun 30 Oct 2005, 16:51
by rarsa
With awk there are different ways of doing it as it is a rich language.

Here is a one liner that does what you want:

Code: Select all

awk '{ if(match($0,"ramdisk_size[ ]*=[ ]*[0-9]*")) printf(substr($0,RSTART,RLENGTH))}'  %pupdrv%/syslinux.cfg
That will handle spaces around the = sign and will return the ramdisk_size=63488 without a newline at the end.

I'm also a novice in awk (I've just written two awk commands in my life... this being the second one) but I've found some good resources like this tutorial http://www.grymoire.com/Unix/Awk.html#uh-47

Posted: Sun 30 Oct 2005, 19:03
by pakt
rarsa, thanks for your responses.
rarsa wrote:With awk there are different ways of doing it as it is a rich language.
Yes, it certainly seems to be a cryptic but powerful language. I've searched high and low on the internet and found ways of doing just about anything...except what I want :?

As I just needed it to solve one problem, I've tried to avoid having to learn the details - there is quite a learning curve - and it would just take too long.
rarsa wrote:Here is a one liner that does what you want:

Code: Select all

awk '{ if(match($0,"ramdisk_size[ ]*=[ ]*[0-9]*")) printf(substr($0,RSTART,RLENGTH))}'  %pupdrv%/syslinux.cfg
That will handle spaces around the = sign and will return the ramdisk_size=63488 without a newline at the end.
Tried your one-liner but got the following error:

Code: Select all

{ if(match($0,ramdisk_size[
awk: line 0: syntax error
rarsa wrote:I'm also a novice in awk (I've just written two awk commands in my life... this being the second one) but I've found some good resources like this tutorial http://www.grymoire.com/Unix/Awk.html#uh-47
Thanks, I'll take a look at it.

Otherwise, while I have been waiting for a response to my post, I actually managed to patch together something that works and will give the right parameter regardless of which field it is in:

awk "/ramdisk_size/{for(i=1;i<=NF;i++){if($i~/ramdisk_size/)print "set rdsize=" $i};exit;}" isolinux.cfg >tmp_.bat
call tmp_.bat
del tmp_.bat >NUL

I got this working after looking at many AWK examples (books & internet) and a lot of trial and error. So it looks like I'll be able to finish my updated WakePup soon after all.

:D

Posted: Sun 30 Oct 2005, 19:11
by pakt
rarsa wrote:Can there be spaces around the '=' ?

e.g.

Code: Select all

append root =/dev/ram0 initrd= image.gz ramdisk_size = 63488 PFILE = pup001-none-262144
In response to your question about spaces, AWK uses spaces as delimiters by default. Therefore the single field 'ramdisk_size=63488' would become three fields instead.

I hope that that variation (having spaces around the '=') in isolinux.cfg or syslinux.cfg isn't allowed by Barry's scripts, otherwise it will be back to the drawing board for me.

Posted: Sun 30 Oct 2005, 19:28
by pakt
rarsa wrote:I know that you said that you only had access to awk and sed. but... do you have access to grep?

Code: Select all

grep -o 'ramdisk_size[ ]*=[ ]*[0-9]*'  %pupdrv%/syslinux.cfg
This gives you exactly what you are asking for. In any event, That's the regular expression you want to use to find the ramdisk_size even if you are not using grep.
Just tried your 'grep' suggestion, but I don't get any output. I had to change the ' to " to work in DOS, like this:

grep -o "ramdisk_size[ ]*=[ ]*[0-9]*" syslinux.cfg

This was using 'Turbo GREP 5.5 Copyright (c) 1992, 1999 Inprise Corporation'

EDIT: Used wrong syslinux file with above example :oops:. With isolinux.cfg, the grep expression outputs all four _complete_ lines containing the parameter :(

I also tried with another grep I had (GNU grep 2.0 (MS-DOS rev A)), but it didn't recognize the -o flag. :(

Posted: Mon 31 Oct 2005, 01:26
by BarryK
Anywhere my scripts have generated ramdisk_size=65536, it is without spaces.
Of course, if someone has manually edited the file that's another matter, but nowhere in any examples or documentation do I show it with spaces, so I reckon you will be safe to assume no spaces.

Posted: Mon 31 Oct 2005, 03:45
by rarsa
patk wrote:Tried your one-liner but got the following error:

Code: Select all

{ if(match($0,ramdisk_size[ 
awk: line 0: syntax error
I just tried it again and it works in puppy, that tells me that the problem is with the awk you are using or the regular expressions evaluator it uses.

I was going for the loop too, but I found that a one liner was better. Anyway, I'm glad it's working for you.
patk wrote:With isolinux.cfg, the grep expression outputs all four _complete_ lines containing the parameter
The -o parameter tells grep to output just the text that matches the regular expression, not the whole line. Is there another option in turbo grep that would do this?

Posted: Mon 31 Oct 2005, 04:37
by MU
try http://unxutils.sf.net
The included grep supports -o

Mark

Posted: Mon 31 Oct 2005, 04:49
by MU
with unxutils you can do:
grep ramdisk_size isolinux.cfg|sed s/^.*ramdisk_size=\([0-9]*\).*$/\1/ >test.txt

At least on WinXP.

Mark

Posted: Mon 31 Oct 2005, 09:47
by pakt
MU wrote:with unxutils you can do:
grep ramdisk_size isolinux.cfg|sed s/^.*ramdisk_size=\([0-9]*\).*$/\1/ >test.txt

At least on WinXP.
Thanks Mark. Seems that unxutils is for win32, but I did find some more grep's that worked with DOS (unfortunately I can't find one that supports the -o flag).

Here is the output I get with your example where isolinux.cfg contains three lines with the parameter ramdisk_size=13312:
13312
13312
13312

This would be a good solution except that I want just a single '13312' - not three. Is there a way of doing that?

Posted: Mon 31 Oct 2005, 10:08
by Guest
in linux, you could pipe it through head -n1
there should be a port of head somewhere

Posted: Mon 31 Oct 2005, 10:40
by pakt
Seems I managed to solve it myself :)

Using Mark's example and 'GNU UNIQ, textutils version 1.2' for DOS like this:

grep ramdisk_size isolinux.cfg|sed s/^.*ramdisk_size=\([0-9]*\).*$/\1/ | uniq >test.txt

...outputs a single '13312' instead of three.

Now I have two solutions...

Thanks to everyone for suggestions & info :D

Posted: Mon 31 Oct 2005, 10:48
by MU
ftp://garbo.uwasa.fi/pc/unix/gnuegrep.zip
ftp://garbo.uwasa.fi/pc/unix/sed15.zip
ftp://garbo.uwasa.fi/pc/unix/tail20.zip

from http://garbo.uwasa.fi/pc/unix.html

They work in Dosbox, but I can't get the "|" -character on my keyboard in Dosbox, so I can't test the whole expression.

try
grep ramdisk_size isolinux.cfg | sed "s/^.*ramdisk_size=\([0-9]*\).*$/\1/" | tail -n 1

Mark

Posted: Mon 31 Oct 2005, 10:51
by MU
oh, you already got a solution, great :)

I did not see that when sending my last post.

Greets, Mark

Posted: Mon 31 Oct 2005, 19:35
by pakt
Thanks anyway :D