LinuxQuestions.org
Review your favorite Linux distribution.
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
 
LinkBack Search this Thread
Old 05-22-2005, 04:12 PM   #1
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Slack 10.2, Slack 12, Suse 10.0, DSL 2.2, Xubuntu 7.04
Posts: 920

Rep: Reputation: 30
sed with pathnames


'allo all. Quick question - how do you replace the "." in a ls -lRa directory name with the actual current pwd?

When I try
Code:
[me@localhost ~/temp]$ working_dir='./'
[me@localhost ~/temp]$ echo $working_dir
./
[me@localhost ~/temp]$ pwd=`pwd`
[me@localhost ~/temp]$ echo $pwd
/home/me/temp
[me@localhost ~/temp]$ working_dir=`echo $working_dir | sed -e s/./$pwd/g`
sed: -e expression #1, char 6: Unknown option to `s'
[me@localhost ~/temp]$ working_dir=`echo $working_dir | sed -e s/./\/home\/me\/temp/g`
sed: -e expression #1, char 6: Unknown option to `s'
The first time I tried just using the variable containing an unescaped pathname, the 2nd time I tried excaping the /'s in the pathname. Both times, though, I get the same error message.

Can someone shed some light on this?
 
Old 05-22-2005, 04:21 PM   #2
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282

Rep: Reputation: 61
Try :
Code:
sed -e 's/^\.//'
Edit : err... I am not sure I get it well, sorry

Last edited by keefaz; 05-22-2005 at 04:23 PM.
 
Old 05-22-2005, 04:31 PM   #3
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Slack 10.2, Slack 12, Suse 10.0, DSL 2.2, Xubuntu 7.04
Posts: 920

Original Poster
Rep: Reputation: 30
Hmm, and that works...just not far enough. I realize that I gave a bad example when I first posted, though.
Given a directory /home/me/temp/subdir, that has a couple of files in it;
And given a subdirectory listing, such as would be returned from ls -Ra:

./subdir:
file1
file2

I want to get the fully-qualified pathname of file1 and file2. So what I figure I could do is take "./subdir:", get rid of the ":" with
Code:
for i in `ls -Ra`; do
    if [[ "$i" == *: ]]; then
        dirname=`echo $i | sed -e s/://g`
and then replace the "./" with the real pathname, which is /home/me/temp, giving me
/home/me/temp/subidir..which I could then concatenate onto 'file1' and 'file2' when I needed them.

Is there any way to do this?
 
Old 05-22-2005, 04:38 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
Blog Entries: 1

Rep: Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140
The problem with your first example is the content of the pwd command: It holds forward slashes, this will confuse sed (forward slash being the normal seperator).

Change the seperator and you'll be ok. Also, the . and g can go. The searchstring should be \.

echo $working_dir | sed -e s%\.%$pwd%

Hope this helps.
 
Old 05-22-2005, 04:50 PM   #5
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Slack 10.2, Slack 12, Suse 10.0, DSL 2.2, Xubuntu 7.04
Posts: 920

Original Poster
Rep: Reputation: 30
Oh, wow - that's really a neat trick! I never fail to learn something here

Many thanks keefaz and druuna!
 
Old 05-23-2005, 05:01 PM   #6
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 32
You might also like to get the current working directory with /bin/pwd instead of the shell builtin so you get the real path, unwound of symlinks.

BTW, you can use pretty much any punctuation character as the separator, so you can tailor the sed command appropriately to minimise the amount of character escaping you have to do.

Last edited by eddiebaby1023; 05-23-2005 at 05:03 PM.
 
Old 05-23-2005, 05:25 PM   #7
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Slack 10.2, Slack 12, Suse 10.0, DSL 2.2, Xubuntu 7.04
Posts: 920

Original Poster
Rep: Reputation: 30
Yeah, the problem with using just pwd is that I need to get the full path of a directory that I'm not actually in, so it had to be 'pwd' + 'relative path of the directory I'm interested in minus the ./ and ending /'

And using sed like that is quite nifty, I'm glad to know of it!
 
Old 05-24-2005, 08:03 AM   #8
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 32
Quote:
Originally posted by rose_bud4201
Yeah, the problem with using just pwd is that I need to get the full path of a directory that I'm not actually in, so it had to be 'pwd' + 'relative path of the directory I'm interested in minus the ./ and ending /'
No, you misunderstood what I was saying about "pwd". The shell builtin will show you path you used to get to your current directory, /bin/pwd will show you the real path to it. To demonstrate, "cd /tmp; ln -s /tmp tempo; cd tempo". Then run "pwd", and "/bin/pwd" and spot the difference.
 
Old 05-24-2005, 09:09 AM   #9
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Slack 10.2, Slack 12, Suse 10.0, DSL 2.2, Xubuntu 7.04
Posts: 920

Original Poster
Rep: Reputation: 30
hmm, interesting. I figured out how to solve the problem awhile ago, but this is worth remembering. I'm constantly up against this at work, where I've got directory symlinks all over the freakin' place
 
Old 05-25-2005, 07:44 AM   #10
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 32
In a C program, you can use realpath(3). The pwd-/bin/pwd differences can be a bit confusing the first time you come across it!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using sed ilnli Linux - General 3 07-01-2005 03:59 PM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Pathnames jcai Linux - Newbie 3 09-15-2004 12:50 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
sed !!1 letmein Linux - General 18 07-31-2003 09:36 AM


All times are GMT -5. The time now is 03:50 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration