sed help

Kurt Granroth kurt+plug-discuss at granroth.com
Thu Apr 3 18:08:53 MST 2008


Kevin Faulkner wrote:
> First off, I've been trying to do this for a little while, but keep on getting
> dragged off to other things. My goal is to get sed to pull off the .xxx of the
> file. So lets say you have documentation.odt timesheet.ods archive.zip and
> readme.txt I would like sed to pull off these: ods zip odt txt. I figured I
> would do it like this
> ls -l |awk '{print $8}'|sed -e '/$\.+++/p
> ls -l |awk '{print $8}'|sed -e '/^\.+++/p
> (I also used ? in place of the +)
> I have also tried this.
> ls -l |awk '{print $8}'|grep -e "*\.[a-z]
> 
> I'm not sure if I just don't understand sed, or if its a problem with regular
> expressions, but either way, I can't get it work. Even * should work as it is any character. A little guidance would be nice. Thanks folks.

Does it have to be 'sed'?  I'm a big sed fan, but in this case, there 
are easier ways.  Here's one such way in bash:

#!/bin/bash

dir=${1:-.}
while read file;
do
     file=${file#${dir%/}/};
     file=${file%.*};
     echo "file: ${file}";
done < <(find ${dir} -maxdepth 1 -type f -print);

This assumes that you just want to strip off the last extension, no 
matter what.  It's trivial to modify this to only look for files with 
three letter extensions and to save the extension, if necessary.

Kurt

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 254 bytes
Desc: OpenPGP digital signature
Url : http://lists.PLUG.phoenix.az.us/pipermail/plug-discuss/attachments/20080403/d54da66c/attachment.pgp 


More information about the PLUG-discuss mailing list