[Gambas-user] OT: BASH help!

Rob sourceforge-raindog2 at ...94...
Mon May 12 05:49:08 CEST 2008


On Sunday 11 May 2008 21:48, Hamish Robertson wrote:
> Yeah I've tried that ls thingy but I need to get rid or the .deb
> extension and list it as csv...i.e package1, package2, package3,
> etc...
>
> is this do-able in bash? or is something like perl or python the go
> here? 

Here's how to remove file extensions and convert a list to a 
comma-separated line, in bash:

/bin/ls *.deb | xargs -ix echo `basename x` | tr "\n" ","; echo

That will end the line with a comma, though, and there's no space 
after each comma.  Using perl (you can paste this into a bash script, 
no need to write the whole thing in perl):

perl -e 'print join(", ", map { s/.deb$//; $_ } split("\n", `ls 
*.deb`)), "\n";'

That produces a normal comma-separated list terminated by a newline.  
You can also use sed and awk to do this, but my skills in those have 
atrophied in the decade since I started using perl.

> Can I do it in gambas? 

Sure.... haven't tried to compile this, but here's the general idea:

dim f as new string[]
dim d as string
f = dir(".", "*.deb")
for each d in f
	d = left(d, length(d) - 4)
next
print f.join(", ")

That should produce the same result as the perl one-liner.

Rob




More information about the User mailing list