Replacing specific characters using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing specific characters using sed
# 8  
Old 12-31-2012
Code:
sed '/\^volume_name:/s//^\\\\volume_name\\volume_name:/g' file

# 9  
Old 01-01-2013
Quote:
Originally Posted by vnayak
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
If I'm interpreting this correctly, it seems that most of the solutions presented so far are assuming that volume_name is a literal string that you want to duplicate or didn't understand that the circumflexes were literal field delimiters. I think the following sed command will do what you want even if the strings to be copied or replaced contain any of the characters on the normal shifted and unshifted keys on a US keyboard except for the ^ and : characters. Rather than using the normal slash character as the search pattern delimiter and substitute and replacement string delimiters, it uses the alert character as the delimiter (the character you get by pressing the control key and the g key at the same time). As an example, if you give this script:
Code:
sed '\^G[.^.]\([^:]*\):[^^]*[.^.]^Gs^G^G^\\\\\1\\\1^^G' in > out

(where the ^G sequences in the script are alert characters)
an input file named in containing:
Code:
OWNER1^volume_name1:\Volume_name1^LONG1^[Supervisor1]^End1
OWNER2^volume_name2:\Volume_name1\folder2^LONG2^[Supervisor2]^End2
OWNER3^abc~`!@#$%&*()_-+={}|[]\;"'<>?,./DEF:\f1\f2\f3^LONG3^[Supervisor3]^End3

it will produce the output in a file named out containing:
Code:
OWNER1^\\volume_name1\volume_name1^LONG1^[Supervisor1]^End1
OWNER2^\\volume_name2\volume_name2^LONG2^[Supervisor2]^End2
OWNER3^\\abc~`!@#$%&*()_-+={}|[]\;"'<>?,./DEF\abc~`!@#$%&*()_-+={}|[]\;"'<>?,./DEF^LONG3^[Supervisor3]^End3

Since the sed script above will not survive cut and paste operations, here is a printf command that will produce the sed statement above:
Code:
printf "sed '\\\\\a[.^.]\\\\([^:]*\\\\):[^^]*[.^.]\as\a\a^\\\\\\\\\\\\\\\\\\\\1\\\\\\\\\\\\1^\a' in > out\n"

Good luck and happy new year.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command on AIX, replace specific characters

Hi, Im using sed on an AIX machine. I am trying to change the 137-139 characters if they are a ' 36'/'000' to a '036'. The positions that need to be changed are fixed. the source data that I have is$cat v.txt 4000422985400050462239065593606500000007422985707771046154054910075641MC0318AMWAY... (9 Replies)
Discussion started by: dsid
9 Replies

2. Shell Programming and Scripting

Replacing specific entry using sed

Hi guys, I'm new to bash programming, so please pardon me. I'm trying to replace an entry's text in Books.txt This code works perfectly: sed -i "s/$BookTitle/$NewBookTitle/g" Books.txt But problem is, if there are double entries, it will also replace that entry. For example: ... (12 Replies)
Discussion started by: todaealas
12 Replies

3. Shell Programming and Scripting

How delete characters of specific line with sed?

Hi, I have a text file with some lines like this: /MEDIA/DISK1/23568742.MOV /MEDIA/DISK1/87456321.AVI /MEDIA/DISK2/PART1/45753131.AVI /IMPORT/44452.WAV ... I want to remove the last 12 characters in each line that it ends "AVI". Should look like this: /MEDIA/DISK1/23568742.MOV... (12 Replies)
Discussion started by: inaki
12 Replies

4. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

5. Shell Programming and Scripting

sed or tr to remove specific group of special characters

Hi, I have a input of the form: ..., word1, word2, word3... I want out put of the form word1, word2, word3 I tried echo '..., word1, word2, word3...' | tr -d '...,' but that takes out the commas in the middle too so I get word1 word2 word3 but I want the commas in the middle. ... (3 Replies)
Discussion started by: forumbaba
3 Replies

6. Shell Programming and Scripting

SED Replacing all but one regex match on a line or specific matches

Hi, I'm attempting to rename some files that have spaces in them. Without linking sed commands together is it possible to replace the first three "." to " ". File.name.is.long.ext -> File name is long.ext I can get the desired effect with echo "File.name.is.long.ext" | sed 's/\./ /g;s/... (5 Replies)
Discussion started by: vectox
5 Replies

7. Shell Programming and Scripting

Sed - merge lines bw 2 specific characters

Hi, I have a bash script and I am looking for a command that will merge specific lines together. Sample Data: registration time = 1300890272 Id = 1 setd = 0 tagunt = 26 tagId=6, length=8, value= tagId=9, length=5, value= tagId=7, length=2, value= tagId=16, length=2, value= tagId=32,... (8 Replies)
Discussion started by: Winsarc
8 Replies

8. UNIX for Dummies Questions & Answers

sed replacing in vi, / characters in the middle

I tried to replace the following in vi: old: 'e/thesis/pp/zones/zones' new: 'd/so162/fix/pp' For that, I used: :%s/e/thesis/pp/zones/zones/d/so162/fix/pp/g but doesn't work, a trailing character error message appeared. How can I get it? Thanks in advance (3 Replies)
Discussion started by: Gery
3 Replies

9. Shell Programming and Scripting

help on sed replacing special characters

Hello, I have a file with many lines with below format: \abc\\1234 jkl\\567 def\\345 \pqr\\567 \xyz\\234 Here, i need to do 2 things. 1. replace \\ with \ 2. remove starting \ so output to be as below: (11 Replies)
Discussion started by: prvnrk
11 Replies

10. Shell Programming and Scripting

replacing specific characters in a string

Hi Friends, Following is an output of a script OPWQERIUTYKLSADFJHGXZNMCVBWQOPERIUTYKLSADFJHGXZNMCVB I want to replace above string's 11 to 17 character by ******* (7 stars) How can it be done? Please somebody guide me. (6 Replies)
Discussion started by: anushree.a
6 Replies
Login or Register to Ask a Question