This is my sed command:

while ...;
do sed -r "${counter}s/^\S+ /$line /g" $in > $out;

Unfortunately this command isn't doing anything when called from within a bash script/loop. So I thought to check if the variables are being resolved the right way:

do echo ´sed -r "${counter}s/^\S+/$line/g" $in > $out´;

which printed this to the console:

sed -r <line number>/^\S+/<replace pattern>/g <infile> > <outfile>

When executing this very command (without the ´) from the console, I get this:

sed: -e expression #1, char 8: unterminated s' command

I guess this is because the ' are missing around the pattern.

So how do I combine double (for resolving variables in sed command) and single (for completing the search/replace pattern) quotation marks when calling this from a bash script?

  • how could it possibly work? i dont see any input at all, and it looks like youre calling sed to work a single line at a time. if thats the case then $counter wont ever work for any line at all but line 1. you really shouldn't loop like that - its spidery and wasteful. think in terms of a chain instead of in terms of a home base. if you do: while gen ouput; do :; done | sed 's/edit/the_whole_stream/' you'll be a lot better off. – mikeserv Oct 28 '15 at 9:46
  • $counter is being raised after the sed command. So that is one working. Yes, I'm want to call sed for each line, because I got a file A and file B. The beginning of line in file B should be replaced with the beginning of line in file A.Those chars differ in each line. – entenbein Oct 28 '15 at 9:57
  • awk or perl are better tools for doing that. – cas Oct 28 '15 at 10:02
  • 1
    Is there / or \n in $line variable? Any way try to execute sed -r "<line number>/^\S+/<replace pattern>/g" <infile> > <outfile> for testing purpose. Is it doing what you wants? – Costas Oct 28 '15 at 10:16
  • Hi, no such thing in $line. The command itself with expanded/resolved variables works fine. Calling the mentioned command with the line number from a variable isn't working so far. – entenbein Oct 28 '15 at 10:25

Well, that is strange. The redirect to the output file didn't happen, at least not with the expected replacements. A cp <infile> <outfile> with the following sed replace command inplace works like a charm....

Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.