Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed error - unterminated substitute pattern

Tags:

sed

I am in directory with files consisting of many lines of lines like this:

98.684807 :(float)
52.244898 :(float)
46.439909 :(float)

and then a line that terminates:

[chuck]: cleaning up...

I am trying to eliminate :(float) from every file (but leave the number) and also remove that cleaning up... line.

I can get:

sed -ie 's/ :(float)//g' *

to work, but that creates files that keeps the old files. Removing the -e flag results in an unterminated substitute pattern error.

Same deal with:

sed -ie 's/[chuck]: cleaning up...//g' *

Thoughts?

like image 979
Mark C Avatar asked Feb 16 '10 04:02

Mark C


1 Answers

Solution :

sed -i '' 's/ :(float)//g' *

sed -i '' 's/[chuck]: cleaning up...//g' *

Explanation :

I can get:

sed -ie 's/ :(float)//g' *

to work, but that creates files that keeps the old files.

That's because sed's i flag is supposed to work that way

-i extension

Edit files in-place, saving backups with the specified extension. If a zero-length extension is given, no backup will be saved.

In this case e is being interpreted as the extension you want to save your backups with. So all your original files will be backed up with an e appended to their names.

In order to provide a zero-length extension, you need to use -i ''.

Note: Unlike -i<your extension>, -i'' won't work. You need to have a space character between -i and '' in order for it to work.


Removing the -e flag results in an unterminated substitute pattern error.

When you remove the e immediately following -i, i.e.

sed -i 's/ :(float)//g' *

s/ :(float)//g will now be interpreted as the extension argument to i flag. And the first file in the list of files produced by shell expansion of * is interpreted as a sed function (most probably s/regular expression/replacement/flags function) You can verify this by checking the output of

sedfn=$(echo * | cut -d' ' -f1); [[ ${sedfn:0:1} == "s" ]]; echo $?

If the output of the above chain of commands is 0, our assumption is validated.

Also in this case, if somehow the first filename qualifies as a valid s/regular expression/replacement/flags sed function, the other filenames will be interpreted as regular files for sed to operate on.

like image 191
Bharat Khatri Avatar answered Sep 28 '22 00:09

Bharat Khatri