Help needed removing two top level folders from path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed removing two top level folders from path
# 1  
Old 04-25-2008
Help needed removing two top level folders from path

Hi,

I am trying to use either awk or sed to drop the first two folders in a path. So if I had path /folder1/folder2/folder3/folder4.... I need to drop folder1&2, so the new path would be /folder3/folder4...

If folder1 and folder2 were the same all the time, this would be easy. But the folder names can change so the following wouldnt work.
echo "/folder1/folder2/folder3/folder4" | sed "s#/folder1/folder2##g"

Any suggestions? I tried reading some of the on line docs but its confusing. Thanks in advance.
# 2  
Old 04-25-2008
This mat be helpful

echo "/folder1/folder2/folder3/folder4" | sed 's#/[0-9a-zA-Z]*/[0-9a-zA-Z]*# #'

/folder3/folder4
# 3  
Old 04-25-2008
If the folder names contain characters only from that set (number and alphabetics), you should be fine. The more general solution is to say "anything which is not a slash":

Code:
sed 's#^/[^/]*/[^/]*##'

# 4  
Old 04-25-2008
awesome!!!

Both worked fine. I was trying to understand the logic, but its difficult for me. The first one sed 's#/[0-9a-zA-Z]*/[0-9a-zA-Z]*# #'
I almost get. Is the * like a wildcard? How come you were able to drop the g from the end of the command?



This one worked too sed 's#^/[^/]*/[^/]*##' Its a slick command, but I am not sure how the syntax works in that one.... Is there an easy reference guide to sed or awk options?

Anyways, thanks to both of you. I'm trying to learn how to use these commands more. Maybe I need to drink a few beers before attempting next time. Smilie
# 5  
Old 04-25-2008
in sed, g is for when you want to replace multiple occurrences of the pattern on the same line. So "echo fee | sed -e 's/e/o/'" produces "foe", but with the /g flag at the end, you get "foo" (all occurrences of e in a line get replaced with o).

* repeats the previous character zero or more times. It's not the same as the shell's wildcard which matches anything, it's a repetition operator, but in broad terms I guess you can call that a sort of wildcard, too.

After the first slash in that regular expression, [^/] means "a character which is not (a newline or) a slash" and we match that zero or more times. Then a slash, then again as many non-slashes as possible, and then a slash again. So three slashes with non-slashes between them, as many as required, and replace that with the empty string (or a space, if you like that better ...?)

The thing you need to understand here is not so much sed or awk as the regular expressions themselves. Friedl's book is The Book here; if you don't want to get into the heavy lifting then the first couple of chapters are still worth it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Top 10 Users in mount level

Hi Members, I'm new to unix. Could you help me in solving my issue. My requirement is I need to pull Top 15 users in every mount. I could able to get the mount level information but I couldn't able to pull the top users in every mount. I see in every mount I could see a lot of nested... (3 Replies)
Discussion started by: UBEE
3 Replies

2. UNIX for Beginners Questions & Answers

Bash script - Remove the 3 top level of a full path filename

Hello. Source file are in : /a/b/c/d/e/f/g/some_file Destination is : /d/e where sub-directories "f" and "g" may missing or not. After copying I want /a/b/c/d/e/f/g/file1 in /d/e/f/g/file1 On source /a is top-level directory On destination /d is top-level directory I would like... (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

How to create 2 different path folders?

Hi all, I am new to this forum. A_NAME=$5 DEPT="abc" DEPT_NAME="cdf" echo " Name:" $A_NAME | tee -a ${LOG_FILE_NAME} echo "dept :" $DEPT | tee -a ${LOG_FILE_NAME} echo "dname :" $DEPT_NAME | tee -a ${LOG_FILE_NAME} echo "parse_parms done" | tee -a ${LOG_FILE_NAME}... (11 Replies)
Discussion started by: Boost
11 Replies

4. Shell Programming and Scripting

How to get top level parent directory

Hi All, I have a directory like this: /u01/app/oracle/11gSE1/11gR203 How do i get the top level directory /u01 from this? Tried dirname and basename but dint help. I can this using echo $ORACLE_HOME | awk -F"/" '{print "/"$2}'. But I am trying to find out if there is a better way of doing it... (4 Replies)
Discussion started by: nilayasundar
4 Replies

5. UNIX for Dummies Questions & Answers

Removing empty folders

Hello, I have a folder that contains all my music. Recently, I started using a different media player, and I let it manage my music folder. It has sorted all my music neatly in folders by artist and album. However, all the old folders that the songs used to be in are still there, yet they are... (2 Replies)
Discussion started by: emveedee
2 Replies

6. UNIX for Dummies Questions & Answers

Top level TCSH while Loop doen't work

Hey guys... I'm learning some shell scripting on OS X using the tcsh shell. For some reason... my while loop isn't executing right (or more likely I am doing something wrong.) Something as simple as this doesn't work: #!/bin/tcsh set g = 0 while ($g <10) echo "this" $g @ g =... (2 Replies)
Discussion started by: sprynmr
2 Replies

7. UNIX for Advanced & Expert Users

Which Base Level Filesets needed by a specific program?

hello... thats a great forum btw :) my problem is that I need a list of the Base Level Filesets (BLF) which are needed by a specific program. Is there any command/tool which shows me that? during the installation I can choose "Preview only" so that I can see what BLF´s are missing etc but... (4 Replies)
Discussion started by: cypher82
4 Replies

8. Shell Programming and Scripting

How to exclude top level directory with find?

I'm using bash on cygwin/windows. I'm trying to use find and exclude the directory /cygdrive/c/System\ Volume\ Information. When I try to use the command below I get the error "rm: cannot remove `/cygdrive/c/System Volume Information': Is a directory. Can someone tell me what I am doing... (3 Replies)
Discussion started by: siegfried
3 Replies

9. UNIX for Dummies Questions & Answers

Removing old folders

All Please help me to remove old files. For example /usr/exp/ - inside this Apr 02 - dir02 Apr 03 - dir03 Apr 04 - dir04 Apr 05 - dir05 Apr 06 - dir06 Apr 07 - dir07 Apr 03 - file03 I want to delete all the folders 2 days before created.Not the fil03 even though it is 2 days old. ... (1 Reply)
Discussion started by: DeepakXavier
1 Replies

10. Shell Programming and Scripting

Is there any way to set env variable in top level Makefile and unset when done

Hello I have compilation directory structure the top level Makefile is the one that contains all the sub directories I want to set in this Makefile env variable say : setenv OPTIMIZATION_LEVEL "1" and when all the sub directories done compiling it will set this variable to different lavel... (0 Replies)
Discussion started by: umen
0 Replies
Login or Register to Ask a Question