Click to See Complete Forum and Search --> : Stripping characters


netfrost
07-25-2002, 01:32 PM
I'm writting a program in PHP, and with the use of exec, I do an ls -al on a directory and grep out a variable....
Unfortunately every file that will be in that directory, has a .xml extension, but my PHP prog, needs just the file name and not the extension.

Is there a way of stripping out the .xml? I'm sure there's a way with awk, but I can't seem to remember.
Any help would be usefull.

X_console
07-25-2002, 01:50 PM
Here's an example:


ls -l somefile.xml | awk -F. '{print $1}'


-F states the delimiter, in this case the dot.

netfrost
07-25-2002, 02:07 PM
AWESOME thank you thank you thank you.

netfrost
07-25-2002, 03:11 PM
one last question...

Say I do a grep -i -l -h $string /some/very.long/path/*

This would return for example

/some/very.long/path/foo.xml


What I'm trying to do, (and yes I really need to brush up on awk)
is just have foo be returned.

So... is there a way of have the grep NOT return the path?
or is there a way of doing this with awk?

baldguy
07-25-2002, 04:41 PM
How about this?

cd /some/very.long/path/
grep -i -l -h $string *

X_console
07-26-2002, 12:01 AM
Originally posted by netfrost
one last question...

Say I do a grep -i -l -h $string /some/very.long/path/*

This would return for example

/some/very.long/path/foo.xml


What I'm trying to do, (and yes I really need to brush up on awk)
is just have foo be returned.

So... is there a way of have the grep NOT return the path?
or is there a way of doing this with awk?

Here's an example:


echo /some/very.long/path/foo.xml | awk -F/ '{print $NF}' | awk -F. '{print $1}'

netfrost
07-26-2002, 12:51 PM
Originally posted by X_console


Here's an example:


echo /some/very.long/path/foo.xml | awk -F/ '{print $NF}' | awk -F. '{print $1}'


see that works GREAT in shell, but the $ is a variable in PHP and it ends up ignoring it.

I'll find a way to specify that $NF isn't a local PHP variable.

Thanks :)

dchidelf
07-26-2002, 01:58 PM
In PHP if you already have the file list, you could do a regex match on each filename to pull out the part you want.
That would be MUCH better than droping to a shell and using awk.

I haven't used regular expressions in PHP, so I don't know what metacharacters are supported.

perl would be like
$filename =~ m/\/([^\/]+)\.xml/; # grab text between / and .xml
$short_filename = $1;

I haven't even thought about trying this regex, but it probably works.

netfrost
07-26-2002, 02:34 PM
Originally posted by dchidelf
[B]In PHP if you already have the file list

See that's the prob, the files are dynamic, (hence why I'm using PHP)

The whole point of this is that there's a page that creates .xml files in this dir...

and then I've got a search page where you type in a word,then the script greps through all the files looking for that variable, and returns the file name.

I had this working, but it returned something like

/some.very/long/path/foo.xml

when I just want foo returned hence why I'm using awk in this, to remove the .xml and the /some.very/long/path

dchidelf
07-26-2002, 02:51 PM
You could create the file list on the fly in PHP ...

Look at the opendir() command and readdir()