Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've tried using sed to replace an SELECT with BELECT in a file:

sed 's/SELECT/BELECT/g' add_pixel.sql | diff - add_pixel.sql

Gives me:

<   BELECT MAX(ID) INTO max_id FROM pixels;
>   SELECT MAX(ID) INTO max_id FROM pixels;
<       '(BELECT id FROM pixels WHERE pixel_id=''', new_pixel_id,
>       '(SELECT id FROM pixels WHERE pixel_id=''', new_pixel_id,
\ No newline at end of file

This was confirmed by wc - the fiel after sed is one line shorter than the original.

Why did sed's 's/SELECT/BELECT/g' removed the newline from the last line of the file?

share|improve this question
This does not happen to me. Can you pastebin the add_pixel.sql file for further testing? – izx Aug 8 '12 at 8:04
Strange. The only place it happens is OSX Bash, can't duplicate on Ubuntu either. Can anyone move this to askdifferent.stackexchange please? – Adam Matan Aug 8 '12 at 8:11
Are you asking a migration? If yes, You can delete the question and re-ask at . Thanks – Anwar Shah Aug 8 '12 at 10:31
up vote 3 down vote accepted

It's your sql file that's missing the newline. sed was "nice" and made sure its output had a trailing newline.

$ diff <(printf 'x') <(printf 'x\n')
< x
\ No newline at end of file
> x

$ diff <(printf 'x\n') <(printf 'x')
< x
> x
\ No newline at end of file

Notice how the "\ No newline..."-line is positioned according to which file was missing it.

share|improve this answer
Thanks, great catch! – Adam Matan Aug 8 '12 at 11:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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