LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-29-2013, 11:19 PM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 332

Rep: Reputation: 0
How to remove carriage return from a file without creating any other file?


I have a files which is created with carriage return(line breaks I believe) and I want to remove carriage return without actually creating any other temp file, how to go about this?
 
Old 04-30-2013, 02:47 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
Quicker than searching lQ (which you should do because it's not like this hasn't been asked before) see the "Similar Threads" section at the bottom of this page. You probably want something like 'sed -i "s/^M//" /some/file' where "^M" is CTL+V CTL+M.
 
Old 04-30-2013, 03:00 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Open the file with vi editor and in colon mode type:
Code:
:%s/^M//g
 
Old 04-30-2013, 06:08 AM   #4
sysmicuser
Member
 
Registered: Mar 2010
Posts: 332

Original Poster
Rep: Reputation: 0
@unSpawn

Not with "Ctrl + M" I am looking with line return or carriage return and don't want any other to be created. It was not something available on web
The one which you gave doesn't work

@shivaa,
Doesn't work either.

Guys, I am looking for something like this.

Code:
This a line, I want to remove the the empty line just after this.

New line, should be just above line and without line return in between.
 
Old 04-30-2013, 06:23 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by sysmicuser View Post
I have a files which is created with carriage return(line breaks I believe) and I want to remove carriage return without actually creating any other temp file, how to go about this?
You cannot do that. A temporary file is always created. Maybe not by you, but the command used will do just that.
 
Old 04-30-2013, 06:29 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
...and in addition to what druuna said
Quote:
Originally Posted by sysmicuser View Post
I want to remove the the empty line
well just say so then:
Code:
sed -i '/^$/d' /some/file
 
Old 04-30-2013, 06:31 AM   #7
mddnix
Member
 
Registered: Mar 2013
Location: Bangalore, India
Distribution: Redhat, Arch, Ubuntu
Posts: 505

Rep: Reputation: 137Reputation: 137
Code:
$ cat test.txt -n
     1  This is line 1
     2
     3  This is line 2
     4
     5
     6
     7  This is line 3
     8
     9
    10

$ sed -i '/^$/d' test.txt

$ cat test.txt -n
     1  This is line 1
     2  This is line 2
     3  This is line 3

Last edited by mddnix; 04-30-2013 at 06:48 AM. Reason: -e not required
 
Old 04-30-2013, 06:44 AM   #8
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
If there's no carriage return symbol in your file, but you empty lines, then you can remove empty lines using vi editor:
Code:
:g/^$/d
Or using awk:
Code:
~$ awk 'NF>0' infile.txt
 
Old 04-30-2013, 08:31 AM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Edit to the previous post#3: I missed to add escape character before ^, so just a \ before ^ and try again with:
Code:
:%s/\^M//g
 
Old 04-30-2013, 10:27 AM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by sysmicuser View Post
@unSpawn

Not with "Ctrl + M" I am looking with line return or carriage return
Code:
This a line, I want to remove the the empty line just after this.

New line, should be just above line and without line return in between.
Carriage return IS Ctrl-M. There are several different ways to identify/describe the byte, but it's still the same byte. Now, do you really mean 'delete empty lines', or is this an exercise in converting DOS/Win (CR-LF) line formatting to Linux/Unix (LF) formatting? There's a distinct difference.

Since there is the requirement to produce no intermediate temp file, I will suggest that 'sed -i' or 'perl -i' is probably part of the solution.

--- rod.
 
Old 04-30-2013, 11:42 AM   #11
bop-a-nator
LQ Newbie
 
Registered: Sep 2012
Location: North East USA
Distribution: at work: Red Hat Enterprise Linux Server release 5.8 (Tikanga); at home: what do you recommend?
Posts: 24

Rep: Reputation: Disabled
Hi,

I see these ^M things on the ends of lines in files that come from someone's PC alot.

the dos2unix command seems to clear them:

At the prompt: /usr/bin/dos2unix myfilename

Maybe this is what you are seeing too - and it does not create a new file.

bop-a-nator
 
Old 04-30-2013, 11:53 AM   #12
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by theNbomr View Post
Since there is the requirement to produce no intermediate temp file, I will suggest that 'sed -i' or 'perl -i' is probably part of the solution.
"sed -i" will create a temp file. That is a pretty fundamental requirement for a stream editor that writes its output while reading its input. Most regular editors also write their output to a temp file and rename that file to replace the original only after the writing has completed successfully.

One way to avoid that behavior with a regular editor (not sed) is to make a second hard link to the file. In order to avoid breaking the hard link, the editor is forced to write directly to the file. You can remove the extra link afterward. (This will not work with sed, which will still write to a temp file and then rename it, breaking the hard link. I can't answer for how perl would behave.)
 
Old 04-30-2013, 12:01 PM   #13
normanlinux
Member
 
Registered: Apr 2013
Distribution: Arch and SuSE
Posts: 135

Rep: Reputation: Disabled
Just a thought. Sometimes these blank lines contain spaces or tabs. Just in case you could replace the pattern /^$/ with /^[ ^I]*$/ where ^I is a tab character. in vi it can be entered by pressing ctrl-v then tab key
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find if a file is having return carriage and new line characters ? pradeepdee6 Linux - Newbie 8 01-10-2013 05:28 AM
[SOLVED] Unwanted Carriage Return hossf Programming 8 11-29-2011 09:09 AM
carriage return mike2008 Linux - Newbie 3 10-15-2009 01:03 AM
Korn shell leave only one carriage return at the end of file quadmore Programming 6 07-14-2008 10:46 AM
Remove New Line or Carriage return from Text File DIMonS Programming 13 10-01-2007 11:31 AM


All times are GMT -5. The time now is 02:48 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration