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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in sed command ( Replacing a pattern inside a file with a variable value )
# 1  
Old 12-11-2008
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 specified line without ruining other lines ?

I am trying to write a script that replaces the

//---- (dashes upto < 79 coloumns) pattern to //---- (dashes upto 79 which is standard) inside any *.cpp file.


Kindly help &
Thanks in advance.
SmilieSmilie
# 2  
Old 12-11-2008
Quote:
Originally Posted by frozensmilz
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 specified line without ruining other lines ?

I am trying to write a script that replaces the

//---- (dashes upto < 79 coloumns) pattern to //---- (dashes upto 79 which is standard) inside any *.cpp file.


Kindly help &
Thanks in advance.
SmilieSmilie
Try changing the delimiter from / to +.
Code:
echo "//-----" | sed "s+//---*+$parChk+g"

If you want to change a particular line or a range of lines

Code:
echo "//-----" | sed "L1,L2s+//---*+$parChk+g"

where L1 is the first line number of the range-of-lines you want to change. L2 is the ending line number of the range-of-lines you want to change. If you want only a particular line to be changed, then L1 and L2 will remain the same.
# 3  
Old 12-22-2008
Need help in sed command (using variable inside sed command)

Hello,

The below script is supposed to replace partition lines "//------" in a cpp source code. Based on the previous posts i changed the sed command accordingly. But now i am stuck up with another issue ....the sed command is found not accepting $x,$x before 's'. I gave the line number as such instead of $x and it worked.

Kindly help me in solving this problem. I want to change the multiple lines (line number given by $x from for loop) of a source file. strMatch is another script that returns a set of line numbers where the pattern is found.

srcCode=$1
parStr="//-----------------------------------------------------------------------------"
parStr="--- TESTING-BAS-BAS-TESTING ---"
linNum=`strMatch $1 "//----" | tr -d ':/-'`

for x in $linNum; do
cecho green "Replacing partition on line $x ..."
cat $1 | sed "$x,$xs+//---*+$parStr+g" > $1.tmp
done
exit
SmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of value abc and ccc in the place of them. Input File: go to &abc=ddd; if... (16 Replies)
Discussion started by: saaisiva
16 Replies

2. Shell Programming and Scripting

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. In FileOne it contains <Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">... (1 Reply)
Discussion started by: linuxadmin
1 Replies

3. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

4. Shell Programming and Scripting

Replacing part of a pattern in sed

Hi I have a piece of xml that has a pattern like this <int>159</int><int>30</int> I want to find this pattern but only substitute the second part of the pattern to {rid1}. Is that possible in sed ? Thanks. ---------- Post updated at 12:10 PM ---------- Previous update was at 12:01 PM... (11 Replies)
Discussion started by: vnn
11 Replies

5. Shell Programming and Scripting

CSV: Replacing multiple occurrences inside a pattern

Greatings all, I am coming to seek your knowledge and some help on an issue I can not currently get over. I have been searching the boards but did not find anything close to this matter I am struggling with. I am trying to clean a CSV file and make it loadable for my SQL*Loader. My problem... (1 Reply)
Discussion started by: OCanada
1 Replies

6. Shell Programming and Scripting

pattern replace inside text file using sed

Hi, I have a situation where I want to replace some occurrences of ".jsp" into ".html" inside a text file. For Example: If a pattern found like <a href="http://www.mysite.com/mypage.jsp"> it should be retained. But if a pattern found like <a href="../mypage.jsp"> it should be changed to... (4 Replies)
Discussion started by: meharo
4 Replies

7. Shell Programming and Scripting

Replacing a pattern using variable?

ip1="xxx" ip2="bbb" sed 's/$ip1/$ip2/g' (3 Replies)
Discussion started by: shivarajM
3 Replies

8. Shell Programming and Scripting

Sed and replacing one occurence of pattern

I would like to use sed to replace one occurence of a pattern in a file. When I use the s/// command it replaces all occurences of the pattern in the file. Should I be using something other than sed? Thanks (6 Replies)
Discussion started by: ss9u
6 Replies

9. Shell Programming and Scripting

replacing a number with random variable inside shell script

Hi All. I need help for the below logic. I ve a file like following input file: NopTX(5) // should be remain same as input ----Nop(@100); //1 Nop(90); //2 --Nop(80); //3 @Nop(70); //4 --@Nop(60); //5 @Nop@(@50); //6 --Nop@( 40); ... (3 Replies)
Discussion started by: user_prady
3 Replies

10. Shell Programming and Scripting

Replacing pattern in variable

My String variable is holding value as - abc"def I want to replce " with \" I tried with awk : echo $var | awk '{gsub(/"/,"\"");print}' and I am getting an error, `)' is not expected. (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question