sed inside sed for replacing string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed inside sed for replacing string
# 1  
Old 08-08-2011
sed inside sed for replacing string

My need is :
Want to change
docBase="/something/something/something"
to
docBase="/only/this/path/for/all/files"

I have some (about 250 files)xml files.

Code:
In FileOne it contains
<Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">
In File Two it contains
<Context path="/PPP" displayName="PPP" docBase="/usr/share/somethingelse" reloadable="true" crossContext="true">

etc..

I extracted it by using:
Code:
cat *.xml | awk /"Context path"/ | awk -F"docBase=" '{print $2}' | awk '{print $1}' | sed 's/\//\\\//g' | awk 'sub(".$", "")' | awk 'sub("^", "")'
Output:

/home/me/documents
/usr/share/somethingelse


Then I changed code to get :
cat *.xml | awk /"Context path"/ | awk -F"docBase=" '{print $2}' | awk '{print $1}' | sed 's/\//\\\//g' | awk 'sub(".$", "")' | awk 'sub("^", "")'
Output:

\/home\/me\/documents
\/usr\/share\/somethingelse


Now i Found all the strings to be replaced.

And now i am going to Replace all those by my string.
Code:
sed -ie 's/
`cat *.xml | awk /"Context path"/ | awk -F"docBase=" '{print $2}' | awk '{print $1}' | sed 's/\//\\\//g' | awk 'sub(".$", "")' | awk 'sub("^.", "")'`
/\/only\/this\/path\/for\/all\/files/g' *.xml

It throws an error
bash: syntax error near unexpected token `('

What mistake i did..??

OR

Is there any other way to do the same ??


I found https://www.unix.com/shell-programmin...using-sed.html already. But little confusion..
.

Last edited by linuxadmin; 08-08-2011 at 08:01 AM.. Reason: Found another answer already
# 2  
Old 08-08-2011
Quote:
Originally Posted by linuxadmin
Now i Found all the strings to be replaced.

And now i am going to Replace all those by my string.
Code:
sed -ie 's/
`cat *.xml | awk /"Context path"/ | awk -F"docBase=" '{print $2}' | awk '{print $1}' | sed 's/\//\\\//g' | awk 'sub(".$", "")' | awk 'sub("^.", "")'`
/\/only\/this\/path\/for\/all\/files/g' *.xml

It throws an error
bash: syntax error near unexpected token `('

What mistake i did..??

OR

Is there any other way to do the same ??


I found https://www.unix.com/shell-programmin...using-sed.html already. But little confusion..
.
That would be a quite complex way to do it - manipulating commands inside Sed. Try below if it suits
Code:
 sed '/Context path/s|docBase[^ ]*|docBase="/only/this/path/for/all/files"|' *.xml

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed inside the awk script to replace a string in the array

The requirement is i need to find an array value matching with pattern {5:{ , replace that with 5: and reassign that to same array index and print it. I write something like below and the issue is sed command is not working. If i replace " with "`" the script gives syntax error.how can i... (8 Replies)
Discussion started by: bhagya123
8 Replies

2. Shell Programming and Scripting

sed not replacing

Data not replacing using sed,please check below. Replace_value=$$dbconn_target Search_value=$$dbcon_source   sed -e s/\${Search_value}/\${Replace_value}/g intrepid_sps_val.parm (2 Replies)
Discussion started by: katakamvivek
2 Replies

3. Shell Programming and Scripting

sed inside sed

hey guys, I'm going to make a sed file out of a list of words words.dat: 802.11a 802.11b 802.11g 802.11n my command: awk '{print $0,"/spEC/g"}' words.dat | awk '{print "s/"$0}' current output s/802.11a /spEC/g s/802.11b /spEC/g s/802.11g /spEC/g s/802.11n /spEC/g s/Player... (5 Replies)
Discussion started by: Johanni
5 Replies

4. Shell Programming and Scripting

help - sed - insert space between string of form XxxAxxBcx, without replacing the pattern

If the string is of the pattern XxxXyzAbc... The expected out put from sed has to be Xxx Xyz Abc ... eg: if the string is QcfEfQfs, then the expected output is Qcf Ef Efs. If i try to substitute the pattern with space then the sed will replace the character or pattern with space,... (1 Reply)
Discussion started by: frozensmilz
1 Replies

5. Shell Programming and Scripting

How to use sed to replace the a string in the same file using sed?

How do i replace a string using sed into the same file without creating a intermediate file? (7 Replies)
Discussion started by: gomes1333
7 Replies

6. Shell Programming and Scripting

Appending string (charachters inside the line) to a fixed width file using awk or sed

Source File: abcdefghijklmnop01qrstuvwxyz abcdefghijklmnop02qrstuvwxyz abcdefghijklmnop03qrstuvwxyz abcdefghijklmnop04qrstuvwxyz abcdefghijklmnop05qrstuvwxyz Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number... (6 Replies)
Discussion started by: tamahomekarasu
6 Replies

7. Shell Programming and Scripting

problem with sed while replacing a string with another

Hi, I have a line something like this sys,systematic,system I want to replace only the word system with HI I used sed for this as below echo sys,systematic,system | sed 's/system/HI/' but I got output as sys,HIatic,system I wanted output as sys,systematic,HI Please tell me... (9 Replies)
Discussion started by: friendyboy
9 Replies

8. Shell Programming and Scripting

error while replacing a string by new line character in sed

hi, when i am doing the following things getting error Can anyone please suggest i have a file where there is a line like the following branch=dev sdf dev jin kilii fin kale boyle dev james dev i want to search the existance of dev in the above line. cat "$file" | sed -n... (8 Replies)
Discussion started by: millan
8 Replies

9. Shell Programming and Scripting

Need help in sed command ( Replacing a pattern inside a file with a variable value )

Hello, The following sed command is giving error sed: -e expression #1, char 13: unknown option to `s' The sed command is echo "//-----" | sed "s/\/\/---*/$parChk/g" where parChk="//---ee-" How can i print the variable value from sed command ? And is it possible to replace a... (2 Replies)
Discussion started by: frozensmilz
2 Replies

10. Shell Programming and Scripting

Help needed - Replacing all date & time occurrences in a file with a string using Sed

Hi, I am new to using Sed. I have a file containg lines like the following: INFORM----Test.pc:168:10/11/05 12:34:26 > some text goes here.. TRACE-----Test.pc:197:10/11/05 12:34:26 > some text goes here.. My requirement is to replace 10/11/05 12:34:26 with a string <RUNDATE> (including <... (4 Replies)
Discussion started by: Hema_M
4 Replies
Login or Register to Ask a Question