Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "no previous regular expression" error when running a 'sed' command?

Tags:

sed

I'm trying to run the following expression in a script to find a device and change one of the fields associated with the device in an HTML file.

sed -e "s/$OLDTEST/$TESTING/" -e "s/$CURRENTVALU/$NEWSTATUS---$DATE/" -e "s/$PASSORFAIL/$PASSORFAILNEW/" -e "s/$BGCOLOR/$BGCOLORNEW/$POSITION"  -e "s/$OLDNOTE/$NOTE/"  >> //sysadm/shared/file.tmp

I seem to keep getting a "no previous regular expression". Any ideas?

debug log output:

sed -e s//Yes/ -e s//Resolved---03-25-13/ -e s//Pass/ -e s//#348017/1 -e s///

sed: -e expression #5, char 0: no previous regular expression
like image 289
cycloxr Avatar asked Mar 25 '13 19:03

cycloxr


1 Answers

Your debugging log is telling you the whole story ;-)

sed -e s//Yes/ -e s//Resolved---03-25-13/ -e s//Pass/ -e s//#348017/1 -e s///
 #------^-- empty regular expression

Hence the error message "no previous regular expression".

Not clear about your experience, so there are 2 solutions. Recall that in unix/linux based shells, strings inside of dbl-quotes will be evaluated for variable substitution and that your code, "s/$OLDTEST/$TESTING/" looks like a string requiring variable substitution.

You may need to do

export OLDTEST="something old"
export TESTING="something new"

for that code to work.

If you really want the literal string '$OLDTEST' to be substituted with the literal string '$TESTING', then you have to "hide' the variables from the shell processing. We do that in unix/linux by using the single quote chars., so then your commands would be

sed 's/$OLDTEST/$TESTING/
     s/$CURRENTVALU/$NEWSTATUS---$DATE/
     s/$PASSORFAIL/$PASSORFAILNEW/
     s/$BGCOLOR/$BGCOLORNEW/$POSITION
     s/$OLDNOTE/$NOTE/'  >> //sysadm/shared/file.tmp

But.. I don't see an input file here.

so its really

sed 's/ ...../..../ ; s/../..../; etc; s/.../../' inputFile >> //sysadm/shared/file.tmp

AND you probably don't really want to append to an existing file for something with a tmp extension, so ...

sed 's/ ...../..../ ; s/../..../; etc; s/.../../' inputFile > //sysadm/shared/file.tmp
#-----------------------------------------------only one > -^-------

Using >> means as your testing enhancing your sed script, your appending the results of each run into the same file. You'll wind up with a lot of duplicate info.

I've used the sed shorthand, omitting the -e option. Sed can process a whole group of cmds as one argument as I show here. You could also put all of those commands into a separate file, and then call it like

   sed -f fixer.sed inputFile > //sysadmin/shared/file.tmp

AND finally, if your sed supports the -i option, you can 'in-line' your changes and just have

sed -i -f fixer.sed inputFile

And sed will overwrite the old file with the changes. I'd recommend against this until you're certain that all changes your attempting work and don't cause any unintended consequences. Once your original file is gone, there's no getting it back, so saving to .tmp as your are doing is a good safety belt:-)

IHTH

like image 177
shellter Avatar answered Sep 20 '22 16:09

shellter