LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-28-2009, 11:28 PM   #1
KornFire
LQ Newbie
 
Registered: Oct 2008
Location: Bangalore, India.
Posts: 8

Rep: Reputation: 0
Eliminating CR (new lines) from a file.


Hi all, I made a C++ program in dos (in dev-C++) and uploaded it on Solaris box. On opening that file with 'vim' editor i found that there is some extra new lines after each written code line. I tried to find out is the file is in dos or in unix format, with 'file' command,and i got "<file-name>.h: ASCII C++ program text". After which i used 'dos2unix' command which shows 'converting file to unix format' but new-lines didn't disappear. Any help on this will be most welcome.

Thanks,
 
Old 01-29-2009, 12:54 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Its possible that if you saved it in vim, vim converted all the lines, so you've got double (Unix) new-lines.
Just upload from DOS and convert immediately before doing anything else.
Better yet, code on solaris in the first place (or any *nix).
 
Old 01-29-2009, 01:31 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
To see what alien characters are appended to each line, post the result of
Code:
head <file-name>.h | od -c
then we can find a sed regexp to strip them out. As chrism01 already pointed out, download the file again and do the command above before any other attempt.
 
Old 01-29-2009, 01:32 AM   #4
KornFire
LQ Newbie
 
Registered: Oct 2008
Location: Bangalore, India.
Posts: 8

Original Poster
Rep: Reputation: 0
is there any command (may be 'tr' cmd, i think) to remove repeated (Unix) new line to single one. I know it is possible to remove all new line with:

Code:
 tr -s '\n' < infile > outfile
but it's not exactly i wanted. it removes all blank line from code.

Thanks,
 
Old 01-29-2009, 01:39 AM   #5
KornFire
LQ Newbie
 
Registered: Oct 2008
Location: Bangalore, India.
Posts: 8

Original Poster
Rep: Reputation: 0
Hi colucix,
The output of 'head' is as below:
Code:
[xyz@blr api]$ head terminal.h.bak | od -c
0000000   /   /   *   *   *   *   *   *   *   *   *   *   *   *   *   *
0000020   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *
*
0000120   *   *   *   *   *   *  \n  \n   /   /                   N   a
0000140   m   e   :       T   e   r   m   i   n   a   l   .   h  \n  \n
0000160   /   /  \n  \n   /   /                   D   e   s   c   r   i
0000200   p   t   i   o   n   :       T   h   i   s       f   i   l   e
0000220       p   r   o   v   i   d   e   s       t   h   e       d   e
0000240   c   l   a   r   a   t   i   o   n       o   f       t   h   e
0000260       T   e   r   m   i   n   a   l       o   b   j   e   c   t
0000300   .  \n  \n   /   /  \n  \n
0000307
Please have a look.

Thanks,
 
Old 01-29-2009, 02:22 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
It confirms you have two consecutive newlines as Chris already guessed. Anyway, I'd do the tr command to strip them out or eventually a sed one-liner like
Code:
sed -i '/^$/d' file.h
but this actually removes all the blank lines. How do you think to remove some blank lines only and not other? Which is the criterium?
 
Old 01-29-2009, 02:56 AM   #7
KornFire
LQ Newbie
 
Registered: Oct 2008
Location: Bangalore, India.
Posts: 8

Original Poster
Rep: Reputation: 0
If criteria of deletion of new line we are talking about then it should be like deleting only one '\n' among repetition of it. Every Line in unix end with delimiter '\n' and if by any means, i'm getting one more blank line (mean one more '\n' entry) then i think any command that could delete only one '\n' among consecutive '\n' entries will be useful. Though, i'm not sure about it.
 
Old 01-29-2009, 03:48 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
I've got the point. Anyway, a double \n\n equals to blank lines, so removing blank lines using sed should give you the result you want to achieve. For example try this:
Code:
sed '/^$/d' file.h | head | od -c
and see the difference with the previous output of od -c.
 
Old 01-29-2009, 07:33 AM   #9
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
tr "\n" ""
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Substitute specific lines with lines from another file rahmathullakm Programming 4 01-10-2009 05:47 AM
How to modify a field in few lines in a file and save the new file - in Perl rounak94 Programming 1 10-02-2008 07:43 PM
Eliminating ALL passwords 7pack Ubuntu 3 09-02-2008 01:53 PM
Random file lines directed to a new file. In script an error. In command line no err leventis Programming 1 09-28-2006 07:16 AM
erase all the lines of a file Prasun1 Linux - General 9 11-06-2005 10:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration