Need help with sed replace script to include spaces
I've got a sed shell script I've been using for quite a while to do batch find/replace operations on .xml files containing various types of text entries. This is the script I use:
Accompanying the shell script is a text or xml file with a simple 2-column structure. The left column contains the text to find, the right column contains the text to replace it with, and the two columns are separated with a space.
Is there a way to modify this shell script or the format of the 2-column replacement matrix to accommodate for text strings containing spaces? For example:
Excellent, thank you. I've set it up to use a pipe and it works for plain text entries.
One thing I should have included in my original post was that text_old and text_new may contain forward- and/or back-slashes for paths, so text_old_to_new might contain something like this:
and the actual xml file might contain a line similar to this:
The pink text is what needs to be changed, but is not. I tried escaping the slashes in the trans_old_to_new file, like so:
but then I get the following error when running the script:
Is there some different way of escaping the slashes?
One thing I should have included in my original post was that text_old and text_new may contain forward- and/or back-slashes for paths,
This problem is related but different than the first one: the first problem (spaces) was that the character you wanted to use was special in some say to the shell - the solution was to move that special meaning around to another character (pipe instead of space) by redefining the "IFS" (internal field separator) char. Note that - once you might need the pipe char in your expressions - you can move the special meaning somewhere else by using even another character you will not need otherwise at the moment to fill that role.
Back to your problem at hand: the backslash (and many other) character(s) have an equally special meaning to sed as the space char has to the shell, but in this case you cannot shift these around. All these are used in "regular expressions" and are called "metacharacters". Here is a list of the most oftenly used ones: ".*&\[{]}/"
The backslash is used to strip their special meaning from these characters and make them ordinary characters again. For example the the dot "." is used to denote one character, regardless of which it is. The sequence "\." instead means a single dot char. The same way "\x" means a literal "x" (even though "x" would mean that anyway) and "\\" means a literal backslash. The easiest way therefore is to just escape any character on the list prior to using it in an expression. The only character we leave out is the "|" which will be consumed by the shell anyway:
Notice, that i used a pipeline and set "lastpipe" for this so that "$sed_script" is not local to the while-loop. This is only possible for bash upwards of some version i do not know for sure (IIRC 4.2), but i find this way of writing a loop fed by some datastream easier to read.
I also introduced a third variable "junk" into the read-statement to filter out unwanted artefacts that might be on some input lines.
Bakunin, that script throws a bunch of errors:
Also, I think in your script you may have duplicated the column reference here:
Just to be clear, there is no file named text_old nor text_new. There are 3 files in total:
1. the xml file containing the text to be replaced
2. the translate.sh script containing the sed command
3. the trans_old_to_new file, containing two columns: old text and new text.
Sorry, copied that off from the terminal without double-checking: "text_old" should be "text_new" of course, and "local" is an alias in my environment - use "typeset" instead.
Quote:
Originally Posted by Agreppa
This, i am afraid, i can do nothing about: as i told you only relatively recent bash-versions know this option, yours might be not recent enough. As i explained, i used it only to avoid the ugly bottom-steered while-loop, which you seem to have to use in this case.
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)
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)
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)
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)
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)
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)
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)
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)
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)