[Solved] Sed error - multiple number options to `s' command


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Sed error - multiple number options to `s' command
# 1  
Lightbulb [Solved] Sed error - multiple number options to `s' command

Hi All,

I am having two files (file1 & file2) and a filelist.txt file below.

file1:

Code:
[s_session1]
$$STRINGVAR1=5
$$STRINGVAR2=10
$$LAST_UPD_DT_TBL1=12/12/2010 12:00:00
[s_session2]
$$STRINGVAR3=100
$$LAST_UPD_DT_TBL2=01/01/2010 12:00:00

------------------------------------------------------------------------------------
file 2:

Code:
[s_session1]
$$LAST_UPD_DT_TBL1=31/12/2012 14:00:43
[s_session2]
$$LAST_UPD_DT_TBL2=23/10/1986 14:23:32

--------------------------------------------------------------------------------------
filelist .txt :

Code:
$$LAST_UPD_DT_TBL1
$$LAST_UPD_DT_TBL2

-----------------------------------------------------------------------------

My requirement is to fetch the $$LAST_UPD_DT_TBL1, $$LAST_UPD_DT_TBL2 values from file2 and replace the corresponding values in file1. for that i have written the below code.

Code:
set -vx
echo "execution started"
for table_name in `cat filelist.txt`
do
var1=$table_name
echo "$var1"
var2=`grep $var1 file1`
echo "$var2" 
var3=`grep $var1 file2`
echo "$var3"
sed -i s/"$var2"/"$var3"/ < file1 
done
echo "execution completed"

while executing the above code values are fetched fine and variable subsitutions are happening correct. But sed command giving me the error "multiple number options to `s' command" . Please help to resolve.

PFB execution log

Code:
sh script6.sh
echo "execution started"
+ echo 'execution started'
execution started
for table_name in `cat filelist.txt`
do
var1=$table_name
echo "$var1"
var2=`grep $var1 file1`
echo "$var2"
var3=`grep $var1 file2`
echo "$var3"
sed -i s/"$var2"/"$var3"/ < file1
done
cat filelist.txt
++ cat filelist.txt
+ for table_name in '`cat filelist.txt`'
+ var1='$$LAST_UPD_DT_TBL1'
+ echo '$$LAST_UPD_DT_TBL1'
$$LAST_UPD_DT_TBL1
grep $var1 file1
++ grep '$$LAST_UPD_DT_TBL1' file1
+ var2='$$LAST_UPD_DT_TBL1=12/12/2010 12:00:00'
+ echo '$$LAST_UPD_DT_TBL1=12/12/2010 12:00:00'
$$LAST_UPD_DT_TBL1=12/12/2010 12:00:00
grep $var1 file2
++ grep '$$LAST_UPD_DT_TBL1' file2
+ var3='$$LAST_UPD_DT_TBL1=31/12/2012 14:00:43'
+ echo '$$LAST_UPD_DT_TBL1=31/12/2012 14:00:43'
$$LAST_UPD_DT_TBL1=31/12/2012 14:00:43
+ sed -i 's/$$LAST_UPD_DT_TBL1=12/12/2010 12:00:00/$$LAST_UPD_DT_TBL1=31/12/2012 14:00:43/'
sed: -e expression #1, char 33: multiple number options to `s' command
+ for table_name in '`cat filelist.txt`'
+ var1='$$LAST_UPD_DT_TBL2'
+ echo '$$LAST_UPD_DT_TBL2'
$$LAST_UPD_DT_TBL2
grep $var1 file1
++ grep '$$LAST_UPD_DT_TBL2' file1
+ var2='$$LAST_UPD_DT_TBL2=01/01/2010 12:00:00'
+ echo '$$LAST_UPD_DT_TBL2=01/01/2010 12:00:00'
$$LAST_UPD_DT_TBL2=01/01/2010 12:00:00
grep $var1 file2
++ grep '$$LAST_UPD_DT_TBL2' file2
+ var3='$$LAST_UPD_DT_TBL2=23/10/1986 14:23:32'
+ echo '$$LAST_UPD_DT_TBL2=23/10/1986 14:23:32'
$$LAST_UPD_DT_TBL2=23/10/1986 14:23:32
+ sed -i 's/$$LAST_UPD_DT_TBL2=01/01/2010 12:00:00/$$LAST_UPD_DT_TBL2=23/10/1986 14:23:32/'
sed: -e expression #1, char 33: multiple number options to `s' command
echo "execution completed"
+ echo 'execution completed'
execution completed


Last edited by Scrutinizer; 11-25-2012 at 07:19 AM.. Reason: code tags
# 2  
Replace
Code:
sed -i s/"$var2"/"$var3"/ < file1

with
Code:
sed -i s#"$var2"#"$var3"# file1

.

Last edited by elixir_sinari; 11-25-2012 at 08:28 AM..
# 3  
Lightbulb Still error

Thanks for your reply . After replacing , i am getting new error "no input files"

PFB log

Code:
$ sh script7.sh
echo "execution started"
+ echo 'execution started'
execution started
for table_name in `cat filelist.txt`
do
var1=$table_name
echo "$var1"
var2=`grep $var1 file1`
echo "$var2"
var3=`grep $var1 file2`
echo "$var3"
sed -i s#"$var2"#"$var3"# < file1
done
cat filelist.txt
++ cat filelist.txt
+ for table_name in '`cat filelist.txt`'
+ var1='$$LAST_UPD_DT_TBL1'
+ echo '$$LAST_UPD_DT_TBL1'
$$LAST_UPD_DT_TBL1
grep $var1 file1
++ grep '$$LAST_UPD_DT_TBL1' file1
+ var2='$$LAST_UPD_DT_TBL1=12/12/2010 12:00:00'
+ echo '$$LAST_UPD_DT_TBL1=12/12/2010 12:00:00'
$$LAST_UPD_DT_TBL1=12/12/2010 12:00:00
grep $var1 file2
++ grep '$$LAST_UPD_DT_TBL1' file2
+ var3='$$LAST_UPD_DT_TBL1=31/12/2012 14:00:43'
+ echo '$$LAST_UPD_DT_TBL1=31/12/2012 14:00:43'
$$LAST_UPD_DT_TBL1=31/12/2012 14:00:43
+ sed -i 's#$$LAST_UPD_DT_TBL1=12/12/2010 12:00:00#$$LAST_UPD_DT_TBL1=31/12/2012 14:00:43#'
sed: no input files
+ for table_name in '`cat filelist.txt`'
+ var1='$$LAST_UPD_DT_TBL2'
+ echo '$$LAST_UPD_DT_TBL2'
$$LAST_UPD_DT_TBL2
grep $var1 file1
++ grep '$$LAST_UPD_DT_TBL2' file1
+ var2='$$LAST_UPD_DT_TBL2=01/01/2010 12:00:00'
+ echo '$$LAST_UPD_DT_TBL2=01/01/2010 12:00:00'
$$LAST_UPD_DT_TBL2=01/01/2010 12:00:00
grep $var1 file2
++ grep '$$LAST_UPD_DT_TBL2' file2
+ var3='$$LAST_UPD_DT_TBL2=23/10/1986 14:23:32'
+ echo '$$LAST_UPD_DT_TBL2=23/10/1986 14:23:32'
$$LAST_UPD_DT_TBL2=23/10/1986 14:23:32
+ sed -i 's#$$LAST_UPD_DT_TBL2=01/01/2010 12:00:00#$$LAST_UPD_DT_TBL2=23/10/1986 14:23:32#'
sed: no input files
echo "execution completed"
+ echo 'execution completed'
execution completed


Last edited by Scrutinizer; 11-25-2012 at 07:19 AM.. Reason: code tagz
# 4  
Quote:
Originally Posted by Chandru_Raj
Code:
sed -i s/"$var2"/"$var3"/ < file1

There are several things wrong (or potentially wrong) with this line:

1. Do not use "sed -i"
You shouldn't use "sed -i" for reasons stated here. Further, if you insist on using the "-i" switch of GNU sed you have to provide a file to be changed. Using "sed < file" means sed receives data via <stdin>, not from a file. sed is simply unable to change its <stdin> and even if it could - you wouldn't notice any effect. The line would have to read:

Code:
sed -i <sed-expr> file


2. Use extensive quoting
The way you intermix fixed and variable parts on the commandline you are not guaranteed to end with a single string as argument to "sed". Always enclose sed-programs in single quotes! To do what you want to do you should enclose your variables in double quotes this way:

Code:
sed '<sed-expr>'"$var1"'<rest-of-expression>' file

Applying all this to your line it should look like this (line two preserves the inode information - if you don't need that replace it by a simple "mv"):

Code:
sed 's/'"$var2"'/'"$var3"'/' file1 > outfile
cat outfile > file1 ; rm outfile

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
hah... I never paid attention to the last input redirection and the in-place edit switch...useless me.. Smilie
Post corrected now.
# 6  
Not fixed yet

Guys,

I am sorry to say that still its not working for me Smilie, I am getting the error "multiple number options to `s' command " again which i got earlier.

PFB the modified code and log

Code:
set -vx
echo "execution started"
for table_name in `cat filelist.txt`
do
var1=$table_name
echo "$var1"
var2=`grep $var1 file1`
echo "$var2"
var3=`grep $var1 file2`
echo "$var3"
sed 's/'"$var2"'/'"$var3"'/' file1 > file7
cat file7 > file1
rm file7
done
echo "execution completed"
------------------------------------------------------
$ sh script6.sh
echo "execution started"
+ echo 'execution started'
execution started
for table_name in `cat filelist.txt`
do
var1=$table_name
echo "$var1"
var2=`grep $var1 file1`
echo "$var2"
var3=`grep $var1 file2`
echo "$var3"
sed 's/'"$var2"'/'"$var3"'/' file1 > file7
cat file7 > file1
rm file7
done
cat filelist.txt
++ cat filelist.txt
+ for table_name in '`cat filelist.txt`'
+ var1='$$LAST_UPD_DT_TBL1'
+ echo '$$LAST_UPD_DT_TBL1'
$$LAST_UPD_DT_TBL1
grep $var1 file1
++ grep '$$LAST_UPD_DT_TBL1' file1
+ var2='$$LAST_UPD_DT_TBL1=12/12/2010 12:00:00'
+ echo '$$LAST_UPD_DT_TBL1=12/12/2010 12:00:00'
$$LAST_UPD_DT_TBL1=12/12/2010 12:00:00
grep $var1 file2
++ grep '$$LAST_UPD_DT_TBL1' file2
+ var3='$$LAST_UPD_DT_TBL1=31/12/2012 14:00:43'
+ echo '$$LAST_UPD_DT_TBL1=31/12/2012 14:00:43'
$$LAST_UPD_DT_TBL1=31/12/2012 14:00:43
+ sed 's/$$LAST_UPD_DT_TBL1=12/12/2010 12:00:00/$$LAST_UPD_DT_TBL1=31/12/2012 14:00:43/' file1
sed: -e expression #1, char 33: multiple number options to `s' command
+ cat file7
+ rm file7
+ for table_name in '`cat filelist.txt`'
+ var1='$$LAST_UPD_DT_TBL2'
+ echo '$$LAST_UPD_DT_TBL2'
$$LAST_UPD_DT_TBL2
grep $var1 file1
++ grep '$$LAST_UPD_DT_TBL2' file1
+ var2=
+ echo ''
grep $var1 file2
++ grep '$$LAST_UPD_DT_TBL2' file2
+ var3='$$LAST_UPD_DT_TBL2=23/10/1986 14:23:32'
+ echo '$$LAST_UPD_DT_TBL2=23/10/1986 14:23:32'
$$LAST_UPD_DT_TBL2=23/10/1986 14:23:32
+ sed 's//$$LAST_UPD_DT_TBL2=23/10/1986 14:23:32/' file1
sed: -e expression #1, char 28: unknown option to `s'
+ cat file7
+ rm file7
echo "execution completed"
+ echo 'execution completed'
execution completed

Moderator's Comments:
Mod Comment edit by bakunin: PLEASE! You posted 3 times now and three times a moderator had to edit the missing CODE-tags into your posts. Use them yourself, if you don't mind, because we have better things to do than to clean up behind you. Thank you!

Last edited by bakunin; 11-25-2012 at 10:02 AM..
# 7  
Quote:
Originally Posted by Chandru_Raj
Code:
+ sed 's//$$LAST_UPD_DT_TBL2=23/10/1986 14:23:32/' file1
sed: -e expression #1, char 28: unknown option to `s'

This is the problem: you use "/" to separate the several fields for the s-command of sed. Using "/" is the default, but basically the s-command in sed looks like this:

Code:
sed 's<separator><search-regexp><separator><replacement><separator><options>' infile > outfile

Where "<separator>" can be any character - whatever is used after "s" will be looked for throughout the expression. For instance:

Code:
sed 's/foo/bar/g' infile > outfile

will change any (option "g") "foo" to "bar". The following would do absolutely the same, replacing the "/" with "_":

Code:
sed 's_foo_bar_g' infile > outfile

As your search string contains "/" characters itself "sed" has no possibility to discern what belongs to the search/replacement regexp and what is a separator. Therefore change your separator to some character you don't use in your regexp or insert escape characters into your search regexp before, by prepending "/" (and other metacharacters) with a backslash: "\/"

in ksh:
Code:
var="$(print - "$var" | sed s'/\//\\&/g')"     # "01/02/03" -> "01\/02\/03"

or in bash:
Code:
var="$(echo "$var" | sed s'/\//\\&/g')"


I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #511
Difficulty: Medium
Many programming languages do not allow you to begin a variable name with a number as it would make it more difficult for a compiler or interpreter to parse a program's source code.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] sed command help

Hello all. Im trying very hard to figure this out, but Im a newbie. I have a file that looks like this.... 6315551234 NJ224 5162224567 SUFF Im trying to put a command together that will make it into this.... UM,6315551234,,,,,NJ224,0 UM,5162224567,,,,,SUFF,0 Im all over the... (7 Replies)
Discussion started by: jay11789
7 Replies

2. Shell Programming and Scripting

[Solved] Counting The Number of Lines Between Values with Multiple Variables

Hey everyone, I have a bunch of lines with values in field 4 that I am interested in. If these values are between 1 and 3 I want it to count all these values to all be counted together and then have the computer print out LOW and the number of lines with those values in between 1 and 3,... (2 Replies)
Discussion started by: VagabondGold
2 Replies

3. Shell Programming and Scripting

Multiple search options in find command

Hi, I'd like find multiple file options to fetch different types of files. find /path...// -type f -name "*.log" -o -name "*.req" -o -name "*.txt" -mtime +5 -exec ls -l {} \; Where as in the above command only the last .txt files its retriving but not .log and .req files can body help... (1 Reply)
Discussion started by: Y.balakrishna
1 Replies

4. Shell Programming and Scripting

Processing Multiple Arguments in Command Line Options

Hi All, I am new to scripting. Could you please assist me . Here is my requirement. I have written a script that has 2 option flags defined. -l) calls some function with the arguments passed in front of -l -r) calls second function with the arguments passed in front of -r *) calls the... (7 Replies)
Discussion started by: Jay Deshpande
7 Replies

5. UNIX for Dummies Questions & Answers

UNIX find command - using multiple -name options

Hi all, What am trying do is search for a directory that is owned by cm that only exists in a path that has a particular directory ex: what I'm using now is find . -user cm -name '*.rel' -type d -exec ls -dl {} \; This is giving me stuff like this ./../../foo1/../../*.rel... (2 Replies)
Discussion started by: jtmed
2 Replies

6. Shell Programming and Scripting

[SOLVED] sed command

Help request, I have tsted this line of code for hours. The first line works and the second line returns the message " sed: command garbled.....". This is running on solaris. The "${} variables all have good values when echoed. ## /bin/sed -n '1,25p' ${file} >> ${MailFile} ... (3 Replies)
Discussion started by: millerg225
3 Replies

7. UNIX for Dummies Questions & Answers

[solved]Help with a sed command

So I have a bunch of strings in a file. Example Line ./prcol/trt/conf/conf-app/jobdefinition/trt-pre-extr-trt-step.jdef Intended Result pre-extr-trt-step So far I have parsed it out to the last bit, echo $line | cut -d'/' -f7 | cut -d. -f1Result trt-pre-extr-trt-step So I added a... (2 Replies)
Discussion started by: J-Man
2 Replies

8. Shell Programming and Scripting

[Solved] My sed command not give me a satisfy result

This is my command echo "Test" | sed -f <(sed -e 's/.*/s,&,gI/' mydic) In mydic file,containing 2 columns delimit by comma (,) a,AlphabetA . . . e,AlphabetE . . s,AlphabetS . t,AlphabetT test,testedd . . zebra,zebraaaa The expect result is testedd (0 Replies)
Discussion started by: Runicer
0 Replies

9. Shell Programming and Scripting

[Solved] how to create multiple directory in one mkdir command

Hi, Unix Gurus, - I have a simple question, I need create multiple directory. I use mkdir {dir1, dir2, dir3) I got one directory as {dir1, dir2, dir3} I searched @ google, I got answer as above code.:wall::confused: Anybody has any idea Thanks in advance ---------- Post updated... (3 Replies)
Discussion started by: ken002
3 Replies

10. Shell Programming and Scripting

find command to use multiple -exec options

Hello All, Is there a way to make exec do a couple of operations on a single input from find? For example, find . -type d -exec ls -l "{}" ";" I would like to give the result of each "ls -l" in the above to a wc. Is that possible? I want to ls -l | wc -l inside exec. How do I... (1 Reply)
Discussion started by: prasanna1157
1 Replies

Featured Tech Videos