Replacing specific characters using sed | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Replacing specific characters using sed

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-28-2012
vnayak vnayak is offline
Registered User
 
Join Date: May 2011
Last Activity: 31 December 2012, 5:38 PM EST
Posts: 12
Thanks: 9
Thanked 0 Times in 0 Posts
Apple Replacing specific characters using sed

Hi,

I have a text file which is output from a server and it lists all the files in a specific volume. However, the volume name appears as volume_name:.
I would like to replace this with \\volume_name\volume_name. This is not a problem in itself as I can use sed to globally look for the volume_name: pattern in the text file and replace it with the correct string.

However, since there are many volume names in different text files and the process to replace them is exactly the same, I was thinking of a more generic solution.

So at the moment the format goes like this:


Code:
^volume_name:\folder_name\folder_name^

Is it possible to use sed to analyse the characters from between ^ and \ and then replace them with \\volume_name\volume_name minus the colon. The ^ sign is the delimiter in this file.

Any suggestion most appreciated.

Regards,

vnayak
Sponsored Links
    #2  
Old 12-28-2012
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 25 September 2014, 5:44 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,422
Thanks: 8
Thanked 541 Times in 519 Posts
Well, it seems almost the norm for sed type stuff, but maybe you have not been to vi/ex/sed school!
Code:
sed '
  s/^\([a-zA-Z0-9_]\{3,33\}\):/\\\\\1\\\1/
 ' in_file1 >out_file1

If you have the gnu sed, you can use -i for update in place, but I would not: disk is cheap and mistakes are expensive, so make new files.
Sponsored Links
    #3  
Old 12-28-2012
vnayak vnayak is offline
Registered User
 
Join Date: May 2011
Last Activity: 31 December 2012, 5:38 PM EST
Posts: 12
Thanks: 9
Thanked 0 Times in 0 Posts
Apple Replacing specific characters using sed

Hi DGPickett,

Thanks for your prompt reply and for the code. However, when I try it does
not do anything to the file I have. So I created a test document (in Pico!) which contained the following data:


Code:
^server_name:\folder\folder name\

and the sed routine did not modify to \\server_name\server_name as I would have expected.

Any ideas?

Many thanks,

vnayak
Quote:
Originally Posted by DGPickett View Post
Well, it seems almost the norm for sed type stuff, but maybe you have not been to vi/ex/sed school!
Code:
sed '
  s/^\([a-zA-Z0-9_]\{3,33\}\):/\\\\\1\\\1/
 ' in_file1 >out_file1

If you have the gnu sed, you can use -i for update in place, but I would not: disk is cheap and mistakes are expensive, so make new files.
    #4  
Old 12-28-2012
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 25 September 2014, 5:44 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,422
Thanks: 8
Thanked 541 Times in 519 Posts
Is the caret mark a literal in your file. I think we need more examples.
Sponsored Links
    #5  
Old 12-28-2012
vnayak vnayak is offline
Registered User
 
Join Date: May 2011
Last Activity: 31 December 2012, 5:38 PM EST
Posts: 12
Thanks: 9
Thanked 0 Times in 0 Posts
Apple Replacing specific characters using sed

Yes. It is setup to be a delimiter. This file will be transferred to Excel at some stage and the caret is only sign that will allow other data in the file to be imported properly.

So for example each line in the file is constructed like this:


Code:
"OWNER^volume_name:\Volume_name^LONG^[Supervisor]^"
"OWNER^volume_name:\Volume_name\folder^LONG^[Supervisor]^"

I hope this make it a bit clearer.

vnayak

Quote:
Originally Posted by DGPickett View Post
Is the caret mark a literal in your file. I think we need more examples.
Sponsored Links
    #6  
Old 12-31-2012
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 25 September 2014, 5:44 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,422
Thanks: 8
Thanked 541 Times in 519 Posts
Case shift and all? volume_name Volume_name
Sponsored Links
    #7  
Old 12-31-2012
vnayak vnayak is offline
Registered User
 
Join Date: May 2011
Last Activity: 31 December 2012, 5:38 PM EST
Posts: 12
Thanks: 9
Thanked 0 Times in 0 Posts
Apple Replacing specific characters using sed

Hi DGPickett,

I am not sure what you mean. Would you mind re-explaining that?

Thanks,

vnayak

Quote:
Originally Posted by DGPickett View Post
Case shift and all? volume_name Volume_name
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed replacing specific characters and control characters by escaping ijustneeda Shell Programming and Scripting 11 05-03-2012 03:40 PM
SED Replacing all but one regex match on a line or specific matches vectox Shell Programming and Scripting 5 05-02-2011 05:01 PM
sed replacing in vi, / characters in the middle Gery UNIX for Dummies Questions & Answers 3 03-08-2010 10:34 AM
help on sed replacing special characters prvnrk Shell Programming and Scripting 11 10-01-2008 11:02 AM
replacing specific characters in a string anushree.a Shell Programming and Scripting 6 08-20-2008 03:49 AM



All times are GMT -4. The time now is 10:29 AM.