Why SED can't see the last newline character?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why SED can't see the last newline character?
# 1  
Old 05-01-2011
Why SED can't see the last newline character?

Removed.
My question does not make sense. and SED does see the last newline character.

But I still have a question:
How to remove the last newline character(the newline character at the end of last line) using SED?

---------- Post updated 05-01-11 at 10:51 AM ---------- Previous update was 04-30-11 at 11:14 PM ----------

I just realized SED is unable to do what I desired, because SED is line oriented, SED removes the newline character when it reads a line into the Pattern Space and adds a newline character when it outputs the data from the Pattern Space, so all the SED commands can't operate on that newline character at all(You may argue the N command can do that, well it can't for the last newline character).

Last edited by kevintse; 05-01-2011 at 06:59 AM..
# 2  
Old 05-01-2011
If you have a multiline file and you really must remove the last newline, try something like:
Code:
last=$(tail -1 old)
sed '$d' < old > new
echo -n "$last" >> new
#or
echo "${last}"\\c  >> new
#depending on your style of echo

This is odd requirement though. Smilie
# 3  
Old 05-01-2011
Thank you, Perderabo
I knew how to remove the last newline character with AWK, or the code you provided.

I was wondering if this could be achieved with a SED one liner, and later I realized that it's not possible.


For the odd requirement, it is because the last newline may cause problems when it appears in a WML file, which might not be displayed correctly in some old mobile devices when opened with a browser.
# 4  
Old 05-02-2011
Code:
In default operation, sed cyclically copies a line of input, 
less its terminating newline character, into a pattern space (unless there is something left after a D command), 
applies in sequence all commands whose addresses select that pattern space, 
and at the end of the script copies the pattern space to standard output (except when -n is specified) and 
deletes the pattern space. Whenever the pattern space is written to standard output or a named file, 
sed will immediately follow it with a newline character

remove last newline you can use
Code:
sed 'N;$s/\n//' file

or
Code:
sed '{$H;x;s/\(.*\)\n\(.*\)/\1\2/;1d}' file

regards
ygemici
# 5  
Old 05-02-2011
Quote:
Originally Posted by ygemici
Code:
In default operation, sed cyclically copies a line of input, 
less its terminating newline character, into a pattern space (unless there is something left after a D command), 
applies in sequence all commands whose addresses select that pattern space, 
and at the end of the script copies the pattern space to standard output (except when -n is specified) and 
deletes the pattern space. Whenever the pattern space is written to standard output or a named file, 
sed will immediately follow it with a newline character

remove last newline you can use
Code:
sed 'N;$s/\n//' file

or
Code:
sed '{$H;x;s/\(.*\)\n\(.*\)/\1\2/;1d}' file

regards
ygemici
They don't work. as you cited the text, sed always appends a newline when it outputs a line of text, so I think there's no way of getting rid of the last newline character in sed.
# 6  
Old 05-02-2011
Quote:
Originally Posted by kevintse
They don't work. as you cited the text, sed always appends a newline when it outputs a line of text, so I think there's no way of getting rid of the last newline character in sed.
What is not working? If you N command then sed append next line to pattern space. Next line is separated from the original pattern space by a newline character..If you H command appends the contents of pattern space to the contents of the holding space.And these are separated by a newline.
# 7  
Old 05-02-2011
Quote:
Originally Posted by ygemici
What is not working? If you N command then sed append next line to pattern space. Next line is separated from the original pattern space by a newline character..If you H command appends the contents of pattern space to the contents of the holding space.And these are separated by a newline.
That newline you mentioned is not the last newline, the last newline is added automatically by sed when it outputs the line, you don't have control over this last newline.

Code:
kevin@kevin-laptop:~ $ cat data
aaa
bbb
ccc
ddd
eeee

kevin@kevin-laptop:~ $ sed 'N;$s/\n//' data | xxd
0000000: 6161 610a 6262 620a 6363 630a 6464 640a  aaa.bbb.ccc.ddd.
0000010: 6565 6565 0a                             eeee.

kevin@kevin-laptop:~ $ sed '{$H;x;s/\(.*\)\n\(.*\)/\1\2/;1d}' data | xxd
0000000: 6161 610a 6262 620a 6363 630a 6464 6465  aaa.bbb.ccc.ddde
0000010: 6565 650a                                eee.

As you can see from the output of the scripts you provided, the newline character(0a) is there.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

2. Shell Programming and Scripting

Remove last newline character..

Hi all.. I have a text file which looks like below: abcd efgh ijkl (blank space) I need to remove only the last (blank space) from the file. When I try wc -l the file name,the number of lines coming is 3 only, however blank space is there in the file. I have tried options like... (14 Replies)
Discussion started by: Sathya83aa
14 Replies

3. Shell Programming and Scripting

replacing by newline character

I have a file (pema)with a single long record which i have to break up into multiple lines Input s1aaaaaaaaaaaaaaaaaaaaaaas1bbbbbbbbbbs1cccccccccc Output s1aaaaaaaaaaaaaaaaaaaaaaa s1bbbbbbbbbb s1cccccccccc m planning to do it by replacing s1 by \ns1 \n is the new line character i... (5 Replies)
Discussion started by: pema.yozer
5 Replies

4. UNIX for Dummies Questions & Answers

newline character in a variable

variable="unix\nlinux" echo $variable expected output: unix linux :wall: can i do that ?? thanks in advance!! (3 Replies)
Discussion started by: sathish92
3 Replies

5. Shell Programming and Scripting

any savant ? using AWK/SED to remove newline character between two strings : conditional removal

I'd like to remove (do a pattern or precise replacement - this I can handle in SED using Regex ) ---AFTER THE 1ST Occurrence ( i.e. on the 2nd occurrence - from the 2nd to fourth occurance ) of a specific string : type 1 -- After the 1st occurrence of 1 string1 till the 1st occurrence of... (4 Replies)
Discussion started by: sieger007
4 Replies

6. UNIX for Dummies Questions & Answers

echo without newline character

hi, I have a for loop where in I write some file name to another file. I want to write all the filenames to another without any newlines. how can i avoid getting new lines with echo? Thanks, Srilaxmi (2 Replies)
Discussion started by: srilaxmi
2 Replies

7. UNIX for Dummies Questions & Answers

newline character

hi, I want to print the below lines "Message from bac logistics The Confirmation File has not been received." When i give like this in the code "Message from bac logistics\n The Confirmation File has not been received." It is giving only Message from bac logistics\n The... (9 Replies)
Discussion started by: trichyselva
9 Replies

8. Shell Programming and Scripting

Concatenate the string with the newline character

Hi , i want to Concatenate a string and use the following code str="i" str="$str am \n" str="$str a \n" str="$str boy \n" echo $str I want to ouput this i am a boy However it outputs i am \n a \n boy \n (3 Replies)
Discussion started by: youareapkman
3 Replies

9. Shell Programming and Scripting

Sed append newline character

Hi All, I am new to Shell scripting.. I have a task to parse the text file into csv format. more then half the things has done. But the problem is when I use the sed command in shell script. it appends newline character at the end of the line. and so when I open the file in CSV it's format... (3 Replies)
Discussion started by: Gaurang033
3 Replies

10. UNIX for Dummies Questions & Answers

How can I replace newline character?

Hi, I am trying to write a script to prepare some text for use as web content. What is happening is that all the newlines in the textfile are ignored, so I want to be able to replace/add a few characters so that for a file containg: This is line 1. This is line two. This is line four.... (1 Reply)
Discussion started by: ghoti
1 Replies
Login or Register to Ask a Question