How to use sed substitution using a $variable for a line containing a word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use sed substitution using a $variable for a line containing a word
# 1  
Old 08-07-2007
Question How to use sed substitution using a $variable for a line containing a word

$ cat ggg

Code:
/E*Fare/testteam/public/pf3/nggfstatic/4k-pf3_3.case
REGION1:
/E*Fare/dist/src/nggfstaticbase/EFare/Server
CODEBASE1:
/dev_tools/LINUXMTP-4/EFS070718E/EFare/Server
DATABASE1: nggfstatic
SCRIPT: /efare1/admin/ezlcn/scripts/pf3_3_scriptlist.input
PROLOGINITSIZE not yet set
You are using: time
PROLOGINITSIZE is  200M

DATABASE CONFIG FILE: config/spserver-nggfstatic
.
.
.
.
.
.
200 - 300 lines

Now substituting a value (given at the same without using a variable), it ran fine:

(1) /efare1/dist/sparc/home/aksutil/pawan
[efare@e1dsgalnggf18] $ sed "/PROLOGINITSIZE is / s/\(.*\)/\1\nTime1\nTime2\nTime3/" ggg

Code:
/E*Fare/testteam/public/pf3/nggfstatic/4k-pf3_3.case
REGION1:
/E*Fare/dist/src/nggfstaticbase/EFare/Server
CODEBASE1:
/dev_tools/LINUXMTP-4/EFS070718E/EFare/Server
DATABASE1: nggfstatic
SCRIPT: /efare1/admin/ezlcn/scripts/pf3_3_scriptlist.input
PROLOGINITSIZE not yet set
You are using: time
PROLOGINITSIZE is  200M
Time1
Time2
Time3
DATABASE CONFIG FILE: config/spserver-nggfstatic
.
.
.
.
.
.
200 - 300 lines

(1) /efare1/dist/sparc/home/aksutil/pawan
[efare@e1dsgalnggf18] $ echo $lines
Code:
Time1 Time2 Time3

(1) /efare1/dist/sparc/home/aksutil/pawan
[efare@e1dsgalnggf18] $ echo "$lines"
Code:
Time1
Time2
Time3


Now, when I substituted the values of line starting with "PROLOGINITSIZE is " at command prompt using exact value...system was good. But when I placed the values inside a variable "$lines" then why it is not giving us the same result.. KINDLY SUGGEST!!!!!!!

See below:

(1) /efare1/dist/sparc/home/aksutil/pawan
[efare@e1dsgalnggf18] $ sed "/PROLOGINITSIZE is / s/\(.*\)/\1\n"$lines"/" ggg

Code:
sed: -e expression #1, char 40: unterminated `s' command

(1) /efare1

/dist/sparc/home/aksutil/pawan
[efare@e1dsgalnggf18] $ sed "/PROLOGINITSIZE is / s/\(.*\)/\1\n'"$lines"'/" ggg

Code:
sed: -e expression #1, char 41: unterminated `s' command

(1) /efare1/dist/sparc/home/aksutil/pawan
[efare@e1dsgalnggf18] $ sed "/PROLOGINITSIZE is / s/\(.*\)/\1\n'"${lines}"'/" ggg

Code:
sed: -e expression #1, char 41: unterminated `s' command

(1) /efare1/dist/sparc/home/aksutil/pawan
[efare@e1dsgalnggf18] $ sed "/PROLOGINITSIZE is / s/\(.*\)/\1\n"${lines}"/" ggg

Code:
sed: -e expression #1, char 40: unterminated `s' command

(1)

/efare1/dist/sparc/home/aksutil/pawan
[efare@e1dsgalnggf18] $


Any helps...while using sed and substituting data that is stored inside a variable...(I have done that in one of my other script...skeptical how that one is working fine????)

Thanks -
__________________
Brgds,
***contact detail removed -- reborg
# 2  
Old 08-07-2007
Error

See here, it's also working when using with variables...but am i missing something in my Q? above:

(1) /efare1/dist/sparc/home/aksutil/pawan
[efare@e1dsgalnggf16] $ tail +15 ~/aksutil/GF_BDY_AKS.htm|head -n 1
Code:
<meta name=Originator content="Microsoft Word 9">

(1) /efare1/dist/sparc/home/aksutil/pawan
[efare@e1dsgalnggf16] $ m=meta;a=arun; sed "15 s/"$m"/"$a"/" ~/aksutil/GF_BDY_AKS.htm|tail +15|head -n 1
Code:
<arun name=Originator content="Microsoft Word 9">

(1) /efare1/dist/sparc/home/aksutil/pawan
# 3  
Old 08-07-2007
Your quotes are mismatched and in the incorrect format, use single quotes around the sed expression and double quotes around the variable.

eg:
Code:
sed 's/SOMTHEING/'"$SOMETHING_ELSE"'/g'

# 4  
Old 08-07-2007
(1) /efare1/dist/sparc/home/aksutil/pawan
[efare@e1dsgalnggf18] $ echo "$lines"
Time1
Time2
Time3
Time4
Time5

(1) /efare1/dist/sparc/home/aksutil/pawan
[efare@e1dsgalnggf18] $ sed '/PROLOGINITSIZE is / s/\(.*\)/\1\n'"$lines"'/' ggg
sed: -e expression #1, char 40: unterminated `s' command

(1) /efare1/dist/sparc/home/aksutil/pawan
[efare@e1dsgalnggf18] $ sed '/PROLOGINITSIZE is / s/\(.*\)/\1\n'"${lines}"'/' ggg
sed: -e expression #1, char 40: unterminated `s' command
# 5  
Old 08-07-2007
Ok, the problem appears to be that you have embedded returns in your variable. This is not going to work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed variable substitution in a script

Hi I am trying to do the following in a script find a string and add in a block of text two lines above on the command line this works fine #/usr/bin/cat /usr/local/etc/dhcpd.conf_subnet | /usr/xpg4/bin/sed -n -e '1h;1\!H;${;g;s/}.*#END of 10.42.33.0/#START of RANGE $dstart\:option... (3 Replies)
Discussion started by: eeisken
3 Replies

2. Shell Programming and Scripting

Sed - variable substitution

Problem with the code below is that the value of the variable is not getting substituted in the sed expression. #/bin/csh set UNIX_ID="rajibd" set X_ID="xrajibd" sed -n 's/$UNIX_ID/$X_ID/g' passwd When run , it is not giving expected output as shown below : ... (4 Replies)
Discussion started by: hiten.r.chauhan
4 Replies

3. Shell Programming and Scripting

Sed variable substitution

Hello, I searched the forum and unable to find a solution for my particular problem. I have a text file I'm trying to insert some text using sed after finding a pattern.. File contains in one line Invoice date: xx/xx/xxxx Balance: $$$$ Name: xxxxxxxxxxxxxx Trying to insert Invoice "Number:... (3 Replies)
Discussion started by: mallu0987
3 Replies

4. Shell Programming and Scripting

Simple sed variable substitution

Give the code: set line = 2 set year = `sed -n '2p' file while ($line < 500) echo $line > f.txt @ line = $line + 1 end How do I utilize the variable $line in the code instead of the number 2. I'm using this in a while loop and counter. I've tried quoting it, double/single... (1 Reply)
Discussion started by: wxornot
1 Replies

5. Shell Programming and Scripting

variable substitution in sed

Hi, I have to insert a line at a particular line number in my file. But the line number is not fixed and it will vary every time. So, I have to use a variable to get the line number I know we can use sed to insert lines at a particular line number but it does not work with variable... (3 Replies)
Discussion started by: sugan
3 Replies

6. Shell Programming and Scripting

SED variable substitution

Hi Chaps... I have a log file as below:- 01 Oct 2009 12:57:56 DEBUG : 01 Oct 2009 12:14:21 DEBUG :..... . . . .05 Oct 2009 14:31:56 DEBUG :.... . . .05 Oct 2009 12:57:56 DEBUG :.... 06 Oct 2009 01:23:11 DEBUG :.... . . .06 Oct 2009 12:53:46 DEBUG :.... (4 Replies)
Discussion started by: rpraharaj84
4 Replies

7. Shell Programming and Scripting

sed variable substitution problem

Hi, I am facing a strange problem. I have a script that used the following to search and replace text: sed 's/'"${find_var_parm}"'/'"${find_var_filter}"'/g' $ParmFile > $TempFile The values of $find_var_parm and $find_var_filter are set based on search criteria. The above seems to be working... (2 Replies)
Discussion started by: arsh
2 Replies

8. Shell Programming and Scripting

whole word substitution in SED

I am trying to substitute something with sed and what I want is to substitute a whole word and not part of a word. ie sed 's/class/room/g' filename will substitute both class and classes into room and roomes which is not what i want Grep for instance can use the -w option or <> grep -w... (7 Replies)
Discussion started by: gikay01
7 Replies

9. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

10. Shell Programming and Scripting

how to do variable substitution in 'sed'

I have a 'sed' editor command trying to read contents of one file into another. v=t1 PX=25 sed '/for/ r /$v/ext/tsfirmfiles.${PX}' /bb/bin/px${PX}.files.2b.deleted > result I cannot come up with the syntax so 'sed' would do a variable substitution. Any idea? Thanks a for help. -A (1 Reply)
Discussion started by: aoussenko
1 Replies
Login or Register to Ask a Question