LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-01-2002, 02:53 PM   #1
gluon
Member
 
Registered: Jun 2001
Location: Switzerland
Distribution: SuSE 8.[012], Gentoo
Posts: 42

Rep: Reputation: 15
sed


Hello





I'm looking for a way of transforming file paths with sed. For example, is there a way to transform





/home/gluon/programming/file_db/shell/../java/FileTest.java





into





/home/gluon/programming/file_db/java/FileTest.java





by removing "a_dir/.." ???





I'm really not used to using sed, but I know that removing "../a_dir" is much easier than removing "a_dir/..", but it's really useless for me !!!





Thank you in advance !!!





Gluon

Last edited by gluon; 02-01-2002 at 02:55 PM.
 
Old 02-01-2002, 03:31 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
i'd suggest using tr instead of sed, i never could suss out out to deal with /'s in sed...

tr "/shell/.." ""
 
Old 02-01-2002, 08:42 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
It's all about escaping characters that would be intrepreted as a regular expression or the delimeter for sed. ( / )

Code:
DIR1="/home/gulon/programming/file_db/shell/../java/FileTest.java"

echo $DIR1 | sed 's/shell\/\.\.\///'
 
Old 02-02-2002, 03:18 AM   #4
gluon
Member
 
Registered: Jun 2001
Location: Switzerland
Distribution: SuSE 8.[012], Gentoo
Posts: 42

Original Poster
Rep: Reputation: 15
acid_kewpie : thank you, but I want to go on with 'sed' ;-)





crabboy : thank you too for your answer ! It works perfectly well but, as I posted this message, I forgot to precise that this script had to work with multi-'..', eg :





transformation of





/home/gluon/programming/file_db/shell/../../system/ccp.c





into






/home/gluon/programming/system/ccp.c





It could be something like 'recursive', ie crunching every 'dir/..' patern until the desired path hasn't any anymore.





I really hope that you'll be able to help me : I've already spent about 5 days on that problem !!!





Thanks in advance and CU



:smash:
 
Old 02-05-2002, 12:24 AM   #5
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Quick and dirty C program that does what you ask.

Code:
/* stripdir.c */
#include <stdio.h>

void removeDir( char * pNew, char *szOldStr )
{
   char szTempStr[256];
   char * pTmp = pNew;

   while ( --pTmp != szOldStr )
   {
      if ( *pTmp == '/' )
      {
         *pTmp = '\0';
         break;
      }
   }
   strcpy( szTempStr, szOldStr );
   strcat( szTempStr, pNew+3 );
   strcpy( szOldStr, szTempStr );
}

main(int argc, char * argv[] )
{
   char * pNew;
   char szOldStr[256];

   strncpy( szOldStr, argv[1], 255 );

   while( ( pNew = (char *)strstr( szOldStr, "/../" ) ) != (char *) NULL )
   {
      removeDir( pNew, szOldStr );
   }
   printf("%s\n",szOldStr);
}
 
Old 02-05-2002, 12:36 AM   #6
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Oops.. A directory like /home/../src/dude.c would seg fault my program. Here is a fix for that:
Code:
--- stripdir.c  Tue Feb  5 01:33:20 2002
+++ stripdir2.c Tue Feb  5 01:32:56 2002
@@ -5,7 +5,7 @@
    char szTempStr[256];
    char * pTmp = pNew;

-   while ( --pTmp != szOldStr )
+   while ( --pTmp )
    {
       if ( *pTmp == '/' )
       {
 
Old 02-05-2002, 01:51 AM   #7
gluon
Member
 
Registered: Jun 2001
Location: Switzerland
Distribution: SuSE 8.[012], Gentoo
Posts: 42

Original Poster
Rep: Reputation: 15
Hi Crabboy !

Wow ! Your code looks like... wow wow !!! I don't really understand it :-(

In fact, I have to thank you, because it works perfectly well ! I think that I'm going to use it, but in another project, which is completly written in C code.

In the mean time, I also found a bash-solution :

###################
function clean_path () {

local old=$1
local new=`echo $1 | sed -e 's/\/[^\/]*\/\.\.//'`
while [ $old != $new ]
do
old=$new
new=`echo $new | sed -e 's/\/[^\/]*\/\.\.//'`
done
echo $new
}
###################

As an example :

clean_path /users/ei3/gluon/Cours/Infographie/Mentor/../../Chapitres_Choisis/labo_02/../../../softwares/opera-5.0-static.i386/LICENSE

gives

/users/ei3/gluon/softwares/opera-5.0-static.i386/LICENSE

... and that's what I was looking for too...

Nevertheless, thank you for having spent time on my problem, and I hope that one day, I will be able to answer one of your questions ;-)

CU, Gluon

Last edited by gluon; 02-06-2002 at 03:34 AM.
 
  


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 and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Sed q? ky-lab_rat Linux - Software 5 10-26-2004 07:59 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
Help with sed odious1 Linux - General 2 08-29-2003 10:52 AM
help with sed DavidPhillips Programming 3 08-11-2003 04:46 PM

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

All times are GMT -5. The time now is 10:27 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