New Line or Not New Line, that is the question.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting New Line or Not New Line, that is the question.
# 1  
Old 05-01-2009
New Line or Not New Line, that is the question.

Question:

What would be the best way to add a CR|\n|LF to the last line of a file if it doesn't have one and only if it doesn't have one?

Problem:

I get a text file in unix (HP-UX) and the last line gets ignored in processing because it does not have a CR or LF at the end of the line.

TIA,

Jody
# 2  
Old 05-01-2009
Code:
sed '${/^$/!a

}' filename > newfile


cheers,
Devaraj Takhellambam
# 3  
Old 05-01-2009
RE: New Line or Not New Line, That is the question.

Quote:
Originally Posted by devtakh
Code:
sed '${/^$/!a
 
}' filename > newfile


cheers,
Devaraj Takhellambam
I received the following error in SunOS:

sed: command garbled: ${/^$/!a

Sorry, did not work for me.
# 4  
Old 05-01-2009
Quote:
Originally Posted by Jodyman
I get a text file in unix (HP-UX) and the last line gets ignored in processing because it does not have a CR or LF at the end of the line.
If you are trying to process your file in Unix, shouldn't you want only LF at the end of each line ?

What characters do you have in the lines other than the last one ? Is it "CR+LF", only CR, or only LF ?

Do you want to make it consistent throughout the file ? If yes, then which one - "CR+LF", only CR, or only LF ?
I'd assume you want "only LF" throughout your file if it is to be processed in Unix, but I may be wrong.

tyler_durden

____________________________________________________
"Only after disaster can we be resurrected."
# 5  
Old 05-02-2009
Quote:
Originally Posted by Jodyman
I received the following error in SunOS:

sed: command garbled: ${/^$/!a

Sorry, did not work for me.
Did you leave a blank line in between
Code:
sed '${/^$/!a

and
Code:
}' filename > newfile

cheers,
Devaraj Takhellambam
# 6  
Old 05-03-2009
Quote:
Originally Posted by durden_tyler
If you are trying to process your file in Unix, shouldn't you want only LF at the end of each line ?

What characters do you have in the lines other than the last one ? Is it "CR+LF", only CR, or only LF ?

Do you want to make it consistent throughout the file ? If yes, then which one - "CR+LF", only CR, or only LF ?
I'd assume you want "only LF" throughout your file if it is to be processed in Unix, but I may be wrong.

tyler_durden

____________________________________________________
"Only after disaster can we be resurrected."
Right tyler, LF.


I'm only worried about the last line. I'm not sure in what environment the file was created in. So, if DOS, CR/LF, if UNIX/LINUX LF.

We've been able to use the following:

tail -1 filename | wc -l

If it returns a 1, leave it alone, if 0, append a LF. Is there a more elegant way?

Jody
# 7  
Old 05-03-2009
Quote:
Originally Posted by devtakh
Did you leave a blank line in between
Code:
sed '${/^$/!a

and
Code:
}' filename > newfile

cheers,
Devaraj Takhellambam

Thanks for the help Devaraj, the answer was "NO". I tried your solution in Linux and it indeed did append a LF but also appended a blank line which would cause a blank line to be processed.

Jody
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

3. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

4. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

Commenting a specific line and inserting a new line after commented line.

Hello All, I have following file contents cat file #line=aaaaaa #line=bbbbbb #line=cccccc #line=dddddd line=eeeeee #comment=11111 #comment=22222 #comment=33333 #comment=44444 comment=55555 Testing script Good Luck! I would like to comment line line=eeeeee and insert a new line... (19 Replies)
Discussion started by: manishdivs
19 Replies

6. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

7. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

8. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

9. Shell Programming and Scripting

Perl question, parsing line by line

Hello, Im very new to PERL and as a project to work on developing my skills at PERL Im trying to parse poker hands. Ive tried many methods however I cant get the last step. $yourfile= 'FILENAME';#poker hands to parse open (FILE, "$yourfile") or die $!; @lines = <FILE>; for (@lines) ... (1 Reply)
Discussion started by: Ek0
1 Replies
Login or Register to Ask a Question