LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-08-2010, 05:40 AM   #1
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Rep: Reputation: 31
Replacing directory variables with sed


Hi, this needs to be done with sed and ideally needs to do this without creating temp files.

TMPDIR="/whatever/you/want/"

This directory eventually gets some tar files written to it, these tar's are then stripped of their directories, I also need to remove whatever the $TMPDIR is.

e.g

/whatever/you/want/importantfiles.tar.gz

becomes importantfiles


I can strip everything ($TMPDIR/*.tar.gz | sed 's/.tar.gz//g') but the directory variable - any ideas? I know this is a real challenge and I'm upto using alternatives to sed (e.g tr or awk or something similar, not perl).

Thanks!
 
Old 02-08-2010, 06:31 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

If the creation of tempfiles is not an option, you should use bash internals (even sed -i s/x/y/g file creates a temp file).

Have a look at this:
Code:
$ xxx="/whatever/you/want/importantfiles.tar.gz"
$ echo ${xxx}
/whatever/you/want/importantfiles.tar.gz

$ echo ${xxx%%.*}
/whatever/you/want/importantfiles

$ echo ${xxx##*/}
importantfiles.tar.gz
The first bold part strips the .tar.gz part, the second one strips the directory part.

Combining the 2 will leave you with the stripped filename:
Code:
$ xxx="/whatever/you/want/importantfiles.tar.gz"
$ yyy="${xxx%%.*}"
$ zzz="${yyy##*/}"
$ echo ${zzz}
importantfiles
For more information about this have a look at the bash man page (the Parameter Expansion section).

Hope this helps.
 
Old 02-08-2010, 06:30 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
How about basename http://linux.die.net/man/1/basename
 
Old 02-08-2010, 06:37 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
Have a look at this:
PE is indeed the best method, but I'd swap the two, so that the basename-equivalent happens first. Consider what happens if
Code:
xxx="/whatever/you.want/importantfiles.tar.gz"
 
Old 02-08-2010, 08:34 PM   #5
genderbender
Member
 
Registered: Jan 2005
Location: US
Distribution: Centos, Ubuntu, Solaris, Redhat
Posts: 396

Original Poster
Rep: Reputation: 31
I like the look of the basename stuff, it's for a script and I'd like to allow the users to specify their own temp directories, so although having a.directory could be problematic, it's the most elegant solution and problems could be put down to user error (I'll write some error checking code).

Actually, I have a small confession to make; the reason I was getting directories in the first place was due to bad coding on my part. I did 'ls -a $DIR/*' and was concerned when the dir's were displayed when really the quickest solution would just be to do 'ls -a $DIR' and then my sed statement to remove the tar.gz. (it's just a standard for loop where each tar has a different action).

Thanks anyway, this basename stuff will probably be used elsewhere as I have numerous similar lines which aren't using ls.
 
Old 02-08-2010, 09:14 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by chrism01 View Post
if OP has only a few files , then its alright. But if going over many files, using shell internals instead of calling basename will be more efficient.
 
Old 02-08-2010, 11:38 PM   #7
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
Actually, I have a small confession to make; the reason I was getting directories in the first place was due to bad coding on my part. I did 'ls -a $DIR/*' and was concerned when the dir's were displayed when really the quickest solution would just be to do 'ls -a $DIR' and then my sed statement to remove the tar.gz. (it's just a standard for loop where each tar has a different action).
Are you actually trying to parse 'ls'? If so, that's your biggest problem right there.
http://mywiki.wooledge.org/ParsingLs
Just use a glob:
Code:
for file in "$dir"/* ; do
   name="${file##*/}"
   name="${name%%.*}"
   echo "$name"
done
As for temporary directories, TMPDIR is the standard variable for the top-level temporary directory (usually /tmp). Well-behaved programs should pay attention to TMPDIR.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed not replacing properly zizou86 Programming 1 01-18-2010 12:39 PM
replacing with sed dipuasks Linux - General 13 12-31-2008 11:24 AM
replacing sed -s option i.you Linux - Software 3 10-28-2007 12:08 PM
[SOLVED] Replacing using sed with variables from another file? eamesj Programming 5 11-24-2006 10:20 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration