The contents of my file are as follows and the desired output is shown below. Using individual sed commands, I am able to modify the file contents. Say
sed -i -e 's!<tag1>FIELD1</tag1>!<tag1>Replaced contents of field1</tag1>! filename
But I am having a tough time trying to replace those individual commands with a single sed script file.
This is a sample file containing a few tags
<tag1>FIELD1</tag1>
<tag2>FIELD2</tag2>
<tag1>FIELD1 Do not change me</tag1>
<tag2>FIELD2 Do not change me</tag2>
<tag1>FIELD1 Do not change me</tag1>
<tag2>FIELD2 Do not change me</tag2>
The desired output is
This is a sample file containing a few tags
<tag1>Replaced contents of field1</tag1>
<tag2>Replaced contents of field2</tag2>
<tag2>Some addition to field2</tag2>
<tag1>FIELD1 Do not change me</tag1>
<tag2>FIELD2 Do not change me</tag2>
<tag1>FIELD1 Do not change me</tag1>
<tag2>FIELD2 Do not change me</tag2>
You can chain the -e expressions.
For example:
sed -e 's!<tag1>FIELD1</tag1>!<tag1>Replaced contents of field1</tag1>!g' -e 's!<tag2>FIELD2</tag2>!<tag2>Replaced contents of field2</tag2>\n<tag2>Some addition to field2</tag2>!g' filename
tag=( "tag1" "tag2" )
find=( "FIELD1" "FIELD2" )
repl=( "Replacement 1" "Replacement 2" )
regex=
I=$'\x01' # sed delimiter
for (( i=0; i<${#find[#]}; i++ )) ;do
regex+="s$I<${tag[i]}>${find[i]}</${tag[i]}>$I<${tag[i]}>${repl[i]}</${tag[i]}>${I}g;"
done
sed "$regex" "$file"
You may or may not want or need the g at the end of each expression.
Here is what worked for me.
The sed_script file (one single file)
/<tag1>FIELD1<\/tag1>/s!<tag1>FIELD1</tag1>!<tag1>Replaced contents of field1</tag1>!
/<tag2>FIELD2<\/tag2>/c\
<tag2\>Replaced contents of field2</tag2>\
<tag2\>Some addition to field2</tag2>
Related
$ cat input.txt
abc
$ sed -e '/PLACE_HOLDER/ {
s/PLACE_HOLDER//g
r input.txt
}' <<< '<div>PLACE_HOLDER</div>'
<div></div>
abc
I try to replace PLACE_HOLDER with the content of a file. But it pastes the file content after the matching line. How to just replace the match?
This is not a duplicate of
Use the contents of a file to replace a string using SED
none of the answers there answer my question specifically. For the 2nd one, which use a bash variable. It is not appropriate when the file is very large. For the first one does not have the problem in my example. In fact, my code is exactly the same as the first answer.
Like you discovered, the r command inserts new lines after the current line.
That's not suitable if you want to embed the contents of another file in the middle of other text on the same line which should not be replaced.
A crude fix is to build a sed script from your input file. Notice then that any & characters in the input file have to be escaped, as well as any literal newlines.
Because we will be escaping ampersands, I decided to use that as the separator for the s command, too.
sed 's/\&/\\&/g
1s/^/s\&PLACE_HOLDER\&/
$!s/$/\\/
$s/$/\&/' input.txt |
sed -f - targetfile
Unfortunately, because standard input is tied to -f - your script can't process standard input for replacements. A simple workaround for that is to save the generated sed script to a temporary file and pass that as the value for the -f option; this will also be necessary if your sed is one which does not accept the script on standard input.
I believe this should be reasonably portable, apart from the notes about -f - above.
Demo: https://ideone.com/oVgIni
Using any awk:
$ awk '
BEGIN { old="PLACE_HOLDER" }
NR==FNR { new=(NR>1 ? new ORS : "") $0; next }
s=index($0,old) { $0=substr($0,1,s-1) new substr($0,s+length(old)) }
{ print }
' input.txt - <<< '<div>PLACE_HOLDER</div>'
<div>abc</div>
The above will work no matter which characters are present in the string you want to match or the file you want to replace it with.
This might work for you (GNU sed):
sed -i 's/PLACE_HOLDER/$(cat input.txt)/g;s/.*/echo "&"/e' file
Substitute the evaluated expression cat input.txt for each match of PLACE_HOLDER globally throughout file.
Suppose I have a config file with some data , example file1.config , whose contents are:
flag_data_to_be_appended=xyz
and I have another file which is a shell script, example file2.sh , whose contents are:
./file.config
flag=abc
echo $flag
Now I need to append the information from file1 to file2 at flag , i.e output for flag has to look like :
flag=abc xyz
How can I do this with the help of "sed" command ?
Why not have sed write its own script?
sed -e "$(sed -e 's|^\(.*\)_data_to_be_appended=\(.*\)|/^\1=.*/ s//\& \2/|' cfg)" script
Inner command reads the config file and emits /^flag=.*/ s//& xyz/
which is then applied to the script file.
Output:
./file.config
flag=abc xyz
echo $flag
The two escaped parenthesis pairs capture key and value as \1 and \2.
In s//& \2/ the // is the null regex which matches the last
regex used (in /^…/) and replaces the entire match (&) followed
by the captured value.
This might work for you (GNU sed):
sed '/^flag=/s#.*#sed "s/.*=/& /" file1#e' file2
Match the line starting flag= in file2 and replace its contents with the singleton lines contents after the = sign by way of a second sed invocation being applied in the RHS of a substitution.
I want to insert a range of lines from a file, say something like 210,221r before the first occurrence of a pattern in a bunch of other files.
As I am clearly not a GNU sed expert, I cannot figure how to do this.
I tried
sed '0,/pattern/{210,221r file
}' bunch_of_files
But apparently file is read from line 210 to EOF.
Try this:
sed -r 's/(FIND_ME)/PUT_BEFORE\1/' test.text
-r enables extendend regular expressions
the string you are looking for ("FIND_ME") is inside parentheses, which creates a capture group
\1 puts the captured text into the replacement.
About your second question: You can read the replacement from a file like this*:
sed -r 's/(FIND_ME)/`cat REPLACEMENT.TXT`\1/' test.text
If replace special characters inside REPLACEMENT.TXT beforehand with sed you are golden.
*= this depends on your terminal emulator. It works in bash.
In https://stackoverflow.com/a/11246712/4328188 CodeGnome gave some "sed black magic" :
In order to insert text before a pattern, you need to swap the pattern space into the hold space before reading in the file. For example:
sed '/pattern/ {
h
r file
g
N
}' in
However, to read specific lines from file, one may have to use a two-calls solution similar to dummy's answer. I'd enjoy knowing of a one-call solution if it is possible though.
I have a folder and sub folder that contains 2000 xml files.
Need to process all the files with BizTalk systems.
But some of the files has wrong tags
streetName Bombay Crescent /addressRegion
/streetName.
I need to you grep to find and replace the worng tags only.
I.e with the for loop.. find any xml file with wrong tag and replace it.
Only the tag "streetName" is affected. And only "addressRegion" is in the wrong place.
will like to do
grep -Po where streetName and *** /addressRegion if the condition is true
replace /addressRegion with /streetName
Thanks in Advance
The following will look for a tag <streetName> that with a matching closing tag of </addressRegion>, and will change addressRegion to streetName. It will replace all occurrences on the line. The street name must not contain any < signs, that would break the matching.
sed -e 's:\(<streetName>[^<]*\)</addressRegion>:\1</streetName>:g'
The command reads its standard input and writes standard output.
Sed -i will do the replacement in-place in all its input files:
sed -i -e 's:\(<streetName>[^<]*\)</addressRegion>:\1</streetName>:g' folder/subfolder/*.xml
I have a couple very large files that will not open up in any file editing program. I want to use sed to edit only the first line of headers to take the header and put a descriptor in front of it. My Files are a combination of pipe,comma and tab delimited.
Example:
Name City State Zip
will be ...
ExampleName ExampleCity ExampleState ExampleZip
Like this if you mean the first line of the file:
sed -i '1 s/^.*$/NEW FIRST LINE/' yourfile
Or if the line you mean is not the first, but you only know it starts with "Name":
sed -i '/^Name/s^.*$/NEW HEADER LINE/' yourfile
Added
You can also do this sort of thing
echo Name City State | sed -E 's/([A-Za-z]+)/Example&/g'