The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Which Base Level Filesets needed by a specific program? cypher82 UNIX for Advanced & Expert Users 4 05-29-2008 08:07 AM
Removing empty folders using 'find' deTTo UNIX for Dummies Questions & Answers 5 04-21-2008 09:24 PM
Help needed with backing up Outlook email folders and settings Saanddra Windows & DOS: Issues & Discussions 3 02-21-2008 06:33 AM
removing library path C3000 HP-UX 4 11-19-2007 09:17 AM
Removing old folders DeepakXavier UNIX for Dummies Questions & Answers 1 04-07-2006 11:19 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-25-2008
robertinohio robertinohio is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 2
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 (permalink)  
Old 04-25-2008
penchal_boddu penchal_boddu is offline
Registered User
  
 

Join Date: Apr 2008
Location: Bangalore
Posts: 127
This mat be helpful

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

/folder3/folder4
  #3 (permalink)  
Old 04-25-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 04-25-2008
robertinohio robertinohio is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 2
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.
  #5 (permalink)  
Old 04-25-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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




All times are GMT -4. The time now is 04:26 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0