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:

^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

Well, it seems almost the norm for sed type stuff, but maybe you have not been to vi/ex/sed school!

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.

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:

^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

Is the caret mark a literal in your file. I think we need more examples.

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:

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

I hope this make it a bit clearer.

vnayak

Case shift and all? volume_name Volume_name

Hi DGPickett,

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

Thanks,

vnayak

sed '/\^volume_name:/s//^\\\\volume_name\\volume_name:/g' file

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:

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:

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:

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:

printf "sed '\\\\\a[.^.]\\\\([^:]*\\\\):[^^]*[.^.]\as\a\a^\\\\\\\\\\\\\\\\\\\\1\\\\\\\\\\\\1^\a' in > out\n"

Good luck and happy new year.