Login or Register to Ask a Question and Join Our Community

sed headache

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed headache
# 1  
Old 10-13-2005
Data sed headache

Hi all,

I've got a thorny sed/regex problem and I thought somebody out there might be able to help.
I'd like to use sed in a Makefile to parse a path string. The path might look like this:
case 1: /mnt/users/bob/build/src
case 2: /mnt/users/bob/build/src/foo
case 3: /mnt/users/bob/build/src/foo/bar/src/more/src

I'd like to write a sed command which returns the string "/mnt/users/bob/build" in all cases.
Here's what I have so far:
sed -e "s^\(.*\)/src/.*^\1^g"

This works well for case 2, but not 1 or 3. I could use brute force repetition:
sed -e "s^\(.*\)/src/.*^\1^g" -e "s^\(.*\)/src/.*^\1^g" -e "s^\(.*\)/src/.*^\1^g" -e "s^\(.*\)/src/.*^\1^g"

to handle case 3, but that's downright ugly and still doesn't handle case 1.
To handle case 1 I tried this:
sed -e "s^\(.*\)/src\$^\1^g"

but this just gives an error message (maybe the end-of-line $ is not escaped properly?)

Can someone please help?
# 2  
Old 10-13-2005
You were so close! Your error is putting a slash after src in your RE.

sed 's^\(.*\)/src.*^\1^'
# 3  
Old 10-13-2005
Thanks, Perderabo! That works for case #1, but still doesn't work for case #3. Is there any way to do a recursive regex search/replace - a search that continues until it doesn't find any more matches?
# 4  
Old 10-13-2005
$ echo $case1
$ echo $case2
$ echo $case3
$ echo "$case1" | sed 's^\([^(src)]*\)/src.*^\1^'
$ echo "$case2" | sed 's^\([^(src)]*\)/src.*^\1^'
$ echo "$case3" | sed 's^\([^(src)]*\)/src.*^\1^'

# 5  
Old 10-14-2005
Dude - that's extremely cool.
But now I'm confused - why does that work?
And after playing around a bit - why does
sed 's^\([^\]\)/src.*^\1^'
also work?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Beginner: script headache

Hi, I am new to shell scripting, but doing my best to learn things. Today doing courses there was exercise like this: #!/usr/bin/bash a=$1 b=$2 if ] then echo "Conditions are met" else echo "Conditions are not met" fi I improved it a little, beacuse of curiosity :D to this: ... (8 Replies)
Discussion started by: Rosentine
8 Replies

2. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

3. UNIX for Advanced & Expert Users

ssh command headache

Good day, I am trying to run a ssh command on a remote server, but when I run the command the server takes FOREVER... and ends up giving me an error.. then the script also kicks server load up by 100% while it is running.. But if I run the same command on the server it goes pretty quickly and... (3 Replies)
Discussion started by: msullivan
3 Replies

4. UNIX for Dummies Questions & Answers

formatting a file headache

Hello does anyone know how to do the following using some simple unix if possible: I have a file with the following info as comma separated. It shows username, id number and all the subjects they are enrolled in on the one line. The subjects are separated by colons. UserName,ID,Subject... (13 Replies)
Discussion started by: ynneycat
13 Replies

5. Shell Programming and Scripting

Friday afternoon headache

Hi all, It's been a long week and my brain is clearly not functioning right so hopefully someone can help me out here. I've got a function in a script which just checks if a MySQL database directory exists or not. Code is as follows: dbCheck2() { if ; then { : # carry... (4 Replies)
Discussion started by: _Spare_Ribs_
4 Replies

6. Shell Programming and Scripting

scripting headache... loops & variables

Surely there's an easier way to do this, lets see if anyone knows! I am new to scripting so go easy on me! I have the following script and at the moment it doesn't work and I believe the problem is that I am using a while loop within a while loop. When I run the script using sh -x I can see... (6 Replies)
Discussion started by: StevePace
6 Replies

7. Shell Programming and Scripting

sed make me headache... need help!

I've still a little problem with sed. So, the content of my file.txt is like: > > > > tpo68-96-22-1.no.no.cox.net.4391: >... (5 Replies)
Discussion started by: nymus7
5 Replies

8. Solaris

A very headache problem

guys, need your help~~~~ I am setting up a Sun Enterprise 450 Ultra 4 server originally I was using Sun solaris 9 1202 version. but after an accident (i guess becoz i deleted something wrongly), the system cannot boot up. and now I wana install a FreeBSD(since it is free and it will... (3 Replies)
Discussion started by: MrDJay
3 Replies

9. IP Networking

DHCP client & ipchains headache

Hi all, I upgraded my SUSE 6.1 to SUSE 7.1 and at once the following things won't work anymore: 1) My DHCP client is not able anymore to retrieve my IP address from the @home server. It times out all the time. If I use a fixed IP I can get on the net, so there is no physical problem. 2) I... (3 Replies)
Discussion started by: Micky
3 Replies

10. UNIX for Dummies Questions & Answers

extremely headache

I need to execute command at remote host from my program. there is rs6000/aix standard command called rexec. unfortunately rexec ask username and password which must supplied interactively, of course it is impossible runned by program. So I have to change rexec command format to make user-name... (5 Replies)
Discussion started by: yatno
5 Replies
Login or Register to Ask a Question

Featured Tech Videos