Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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:

22c22
<   BELECT MAX(ID) INTO max_id FROM pixels;
---
>   SELECT MAX(ID) INTO max_id FROM pixels;
36c36
<       '(BELECT id FROM pixels WHERE pixel_id=''', new_pixel_id,
---
>       '(SELECT id FROM pixels WHERE pixel_id=''', new_pixel_id,
47c47
< DELIMITER ;
---
> DELIMITER ;
\ 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 askdifferent.stackexchange.com . Thanks –  Anwar Shah Aug 8 '12 at 10:31

1 Answer 1

up vote 2 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')
1c1
< x
\ No newline at end of file
---
> x

$ diff <(printf 'x\n') <(printf 'x')
1c1
< 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

 
discard

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.