Need help with sed replace script to include spaces


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need help with sed replace script to include spaces
# 8  
Old 02-06-2015
Hi,
Try this (under bash) if your sed supported -f option:
Code:
find . -type f -name \*.xml -exec sed -i.BAK -f <(sed -e 's/[\\/]/\\&/g' -e 's/^/s\//' -e 's/ /\//' -e 's/$/\/g/' trans_old_to_new) '{}' +

Regards.
# 9  
Old 02-06-2015
Bakunin: bash version is 4.2.37(1)-release running on Crunchbang 11

Disedorgue: running that did nothing but create the backup file. The file which should have been changed was identical to the original. Perhaps it's because the xml contains more than the snippet I included here?

Here is an actual line from the xml in question (I broke the line for easier reading, but it's a single line of xml):
Code:
<Task><Base value="" ref="1234" ser="1234" /><Selection value="" /><Result path="S:\output\Batch\ReplacementName.ext" />
<Source value="C:\Users\username\AppData\Roaming\Project Source 0\Definition File\ReplacementSample.ext" /><Preset Source="0" /></Task>

and the goal is to transform it into this:
Code:
<Task><Base value="" ref="1234" ser="1234" /><Selection value="" /><Result path="S:\output\Batch\ReplacementName.ext" />
<Source value="C:\Documents and Settings\username\Application Data\Project Source 0\Definition File\ReplacementSample.ext" /><Preset Source="0" /></Task>

In this case, all I'm really doing is modifying a user profile path from a windows 7 format to a windows xp format, for an application that's run on a virtualized windows xp instance. Possibly also important is the fact that some of the path may have varying spaces... i.e. "ReplacementSample.ext" might be "Replacement Sample.ext" etc, but they will be consistent from old to new.

(Worst case I can just manually regex the changes before I send the configurations to the virtualized WinXP machine, but was hoping for an easy way to just trigger a script to do the translation/replacement because it'll be a frequently repeated procedure and the less manual text-editing the better.)
# 10  
Old 02-07-2015
Ok,
Could you test this case:
file format of trans_old_to_new:
Code:
$ cat trans_old_to_new
old:\Users\username\AppData\Roaming\
new:\Documents and Settings\username\Application Data\

input file:
Code:
$ cat yy.xml
<Task><Base value="" ref="1234" ser="1234" /><Selection value="" /><Result path="S:\output\Batch\ReplacementName.ext" /><Source value="C:\Users\username\AppData\Roaming\Project Source 0\Definition File\ReplacementSample.ext" /><Preset Source="0" /></Task>

and command to test (with resultat at home):
Code:
$ find . -type f -name yy.xml -exec sed -f <(sed -e 'N' -e 's/[\\/]/\\&/g' -e 's/^old:/s\//' -e 's/\nnew:/\//' -e 's/$/\/g/' trans_old_to_new) '{}' +
<Task><Base value="" ref="1234" ser="1234" /><Selection value="" /><Result path="S:\output\Batch\ReplacementName.ext" /><Source value="C:\Documents and Settings\username\Application Data\Project Source 0\Definition File\ReplacementSample.ext" /><Preset Source="0" /></Task>

Beware: "trans_old_to_new" file mustn't a dos file but an unix file.

PS: in sed, the best separator is the carriage return (\n) because it's always the line terminator for sed.

Regards.

Last edited by disedorgue; 02-08-2015 at 05:32 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed replace one space and leave other spaces untouched

Hi Friends, I looked up online, but couldn't figure out a proper solution. I have an input file where the columns are separated by multiple spaces and the column content is separated by single space. For example, Chr1 hello world unix is fun In the above example, chr1 is first... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

2. Shell Programming and Scripting

How to catch a two word keyword which may contain a new line(may include spaces or tab) in it?

How to catch a two word keyword which may contain a new line(may include spaces or tab) in it. for example there is a file a.txt. $more a.txt create view as (select from ......... .......... ( select .... ( select ...... .. select only no ((( number ( select end (12 Replies)
Discussion started by: neelmani
12 Replies

3. Shell Programming and Scripting

sed to replace the matching pattern with equal number of spaces

Hi I have written a shell script which used sed code below sed -i 's/'"$Pattern"'/ /g' $FileName I want to count the length of Pattern and replace it with equal number of spaces in the FileName. I have used $(#pattern) to get the length but could not understand how to replace... (8 Replies)
Discussion started by: rakeshkumar
8 Replies

4. Shell Programming and Scripting

making find/sed to include directory names with spaces

how can i make find/sed to include directory names with spaces the command is like this for i in `find wp-content/themes -type f -print0 | xargs -0 grep -l -iE 'e'`;do sed -i -e 's/word1/word2/gI' "$i";done but it skips one directory names with spaces sed: can't read ./Nova: No such... (5 Replies)
Discussion started by: vanessafan99
5 Replies

5. Shell Programming and Scripting

How to Use Sed Command to replace white spaces with comma from between two fields - Mayank

SHELL SCRIPT Hi I have a file in the following format Mayank Sushant Dheeraj Kunal ARUN Samir How can i replace the white space in between and replace them with a comma?? The resultant output should be Mayank,Sushant Dheeraj,Kunal ARUN,Samir i tried using sed -e... (8 Replies)
Discussion started by: mayanksargoch
8 Replies

6. Shell Programming and Scripting

Include white spaces while using CUT command

Hi I tried to extract 19 characters (default) enclosed with in tag from a file using cut command. If the characters comprises of double space, the cut command gives the output with a single spacing. file 1 <name>Kumar Rajasekaran</name> cut -c7-26 "file1" the out put i received is ... (48 Replies)
Discussion started by: Sekar1
48 Replies

7. Shell Programming and Scripting

Help with sed matching <tag1> newline spaces <tag2> and replace the value in the same string format

Hi, I'm very new to shell scripting and have searched google and this forum for quite some time now. I have the following in my xml file: <recipients> <member>value1</member> </recipients> I need to find a string <recipients> that follows with a new-line and bunch of spaces and... (5 Replies)
Discussion started by: mgharios
5 Replies

8. Shell Programming and Scripting

sed replace spaces between quotes with a variable

I have lines with: elseif (req.http.host ~ "^(www.)?edificationtube.com$|www.edificationtube.org www.edificationtube.net edificationtube.org www.edificationtube.com edificationtube.net") { elseif (req.http.host ~ "^(www.)?collegecontender.com$|www.collegecontender.com collegecontenders.com... (3 Replies)
Discussion started by: EXT3FSCK
3 Replies

9. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

10. Shell Programming and Scripting

How to include PDF file with spaces

I am able to include a pdf file as an attachment in an email using the following: echo "" > reports elm -s "RW100 PDF Reports" me@myemail.com < reports However, if I have a filename that contains some spaces, I'm not so lucky. I've tried: echo "" > reports but no luck. I keep getting... (1 Reply)
Discussion started by: lawadm1
1 Replies
Login or Register to Ask a Question