-1

I have a couple of 100 source files and 22,000 variable names to replace. Made a sed script with the replace variables, in file ReplaceScript, like this:

#! /usr/bin/sed -f s/foo1/bar1/g s/foo2/bar2/g s/foo3/bar3/g 

Need to replace variable names in all .c files in a tree. In the base directory to be searched, I've used the following command:

find . -type f -name "*.c" -exec sed -i '' -f ./ReplaceScript {} + sed: 1: ./ReplaceScript: bad flag in substitute command: 's' 

Single replacement command did work as intended:

find . -type f -name "*.c" -exec sed -i '' 's/foo1/bar1/g' {} + 

My question: how do I fix my bad flag?

Update:

Using GNU sed results in a different error:

find . -type f -name "*.c" -exec /usr/local/bin/sed -i -f ./ReplaceScript {} + /usr/local/bin/sed: file ./ReplaceScript line 1: unknown option to `s' 

Update

It turns out to be two problems: 1. DOS carriage returns instead of unix ones. 2. little endian UTF 16 file format, must be ASCII for sed to parse correctly.

Update 2 A duplicate? The same answer to a different question, with an extra UTF 16 problem on top?

19
  • 3
    Do you have a / in your patterns in ReplaceScript? Pasting an actual sample could help. Commented Apr 11, 2017 at 3:08
  • Seems to work for me, could it be a whitespace or line ending issue? Commented Apr 11, 2017 at 3:11
  • 1
    I can't reproduce it either. Apart from that, you don't need that hashbang line - that's if you want to create a sed executable that can be called like ./script instead of sed -f script, but that couldn't be combined with -i, so I don't think you want that. Commented Apr 11, 2017 at 3:13
  • What happens when you execute the script without the surrounding find command, e.g. sed -i '' -f ./ReplaceScript file1 file1? Commented Apr 11, 2017 at 3:14
  • 1
    The attempted shebang line is just a comment as far as sed is concerned, it's doing no good but neither is it doing any harm. Chances are foo1 is really /home/foo/bar or something. Commented Apr 11, 2017 at 3:18

2 Answers 2

1

Based on this comment , the file is UTF-16 encoded, in little endian format with a byte order mark. Try running the following command on it to convert it to ASCII:

iconv -f UTF-16 -t ASCII ReplaceScript > ReplaceScript-new 

I notice that it does also have carriage returns. sed seems to be okay with them on my machine, but you can easily get rid of them with dos2unix if they cause problems for you.

Sign up to request clarification or add additional context in comments.

1 Comment

@ jerry Yes that works, thx. Cannot up vote your answer at the moment, not enough credits being a new user.
0

The problem was in the DOS carriage returns in the script file. Had exported Space Delimited Text .prn from an Excel sheet, which turned out to use DOS/Windows carriage returns. These don't show up in Text Editor, only when you vi the script file does it show up as ^M. And those little mad hatters cause problems when unix sed interprets them.

Solutions posted elsewhere on this forum: install dos2unix; instruct vim to convert to unix with command file format; replace patterns; they all did not work for me. I now still export form Excel and paste into TextWrangler.

The comments suggesting that the code I posted did not show any problems, were what led to the solution. Thanks everyone for contributing to my first post on this forum.

8 Comments

Something seems off. Obviously, per my comment above, I considered the possibility that it was line ending related. I tested with various line endings on OS X and could not reproduce. Between that and the fact that you can't fix the problem with some of the standard tools, seems to me that there's something missing from the explanation.
I agree. Something else seems to be wrong but I cannot identify it: after exporting from Excel all seems to be in order when loaded in vim, but sed still sees a '?' in line 1:
sed: 1: ./Replace_8_7: invalid command code ?. Both OSX and GNU sed are the only ones seeing this question mark, I cannot identify or visualise it with any tool.
If only I could find out how, I could post a link to a sample of an Excel output file. Copy-Paste as in my original question does not recreate the problem.
You could run xxd -p <sed script file> and post the output.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.