LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-10-2010, 01:41 AM   #1
sumanch
LQ Newbie
 
Registered: Jan 2010
Posts: 24

Rep: Reputation: 16
How to remove the last \n from a file .


I have a file sample.xml . If I do "vi sample.xml" I can see the following

bash$ cat sample.xml
<content/>
bash$

but I don't want the last \n character so that it becomes following

bash$ cat sample.xml
<content/>bash$

Please let me know how do I achieve that ?

regards
Suman
 
Old 03-10-2010, 02:02 AM   #2
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Very simple
Code:
tr -d '\n'< sample.xml

Last edited by vinaytp; 03-10-2010 at 02:15 AM.
 
Old 03-10-2010, 04:01 AM   #3
kainosnous
Member
 
Registered: Mar 2010
Location: Tennessee, USA
Distribution: Arch, Fedora
Posts: 59

Rep: Reputation: 18
Personally, I would just edit it in vi. Just type G and dd and you should be done. Was this a one time thing, or did you need a script to do this?
 
Old 03-10-2010, 04:05 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by vinaytp View Post
Very simple
Code:
tr -d '\n'< sample.xml
That would delete all line endings from sample.xml. Even if it were modified to remove only the/any line end from the end of the file it would not print the bash prompt after the file contents as the OP wants because bash starts a new line after command output, before displaying the prompt, as in this example:
Code:
c@CW8:~$ < /dev/null
c@CW8:~$
The only time I recall seeing the bash prompt immediately after command output is when the terminal's line end handling has been disabled, usually by accidentally sending the contents of a binary file to the terminal.
 
Old 03-10-2010, 04:21 AM   #5
kainosnous
Member
 
Registered: Mar 2010
Location: Tennessee, USA
Distribution: Arch, Fedora
Posts: 59

Rep: Reputation: 18
catkin is right. I missed that. It will still start the prompt on a new line. What were you trying to do? Perhaps there is another approach.
 
Old 03-10-2010, 05:21 AM   #6
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Quote:
Originally Posted by kainosnous View Post
Personally, I would just edit it in vi. Just type G and dd and you should be done. Was this a one time thing, or did you need a script to do this?
Could you pleas tell me how this works ? Hope this deletes entire last line. Also even after deleting last line you will end up with \n in last but one line.

you may inspect this with Ocatal dump after G and dd
Code:
od -t c sample.xml
I thought \n has to be deleted entirely from the file. If you want to delete it only form the last line why can't you write a simple script to do that. Something similar to below will do

Code:
var=`wc sample.xml | tr -s ' ' | cut -d ' ' -f 2`
lines=`expr $var - 1`
head -n $lines sample.xml ; tail -1 sample.xml | tr -d '\n'

Last edited by vinaytp; 03-10-2010 at 05:22 AM.
 
Old 03-10-2010, 05:58 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
I have some doubts about bash printing a new prompt on a new line after command output. If you print out some text that misses the last newline, the prompt should actually be placed inline. For example, here is an alternative in gawk to remove the last newline (I think sed can do it better, but I cannot catch it, now...):
Code:
[colucix@ocean-4 ~]$ cat testfile
line one
line two
line three
[colucix@ocean-4 ~]$ awk 'NR==1{("cat " FILENAME " | wc -l") | getline NL} NR < NL; END{printf "%s", $0}' testfile
line one
line two
line three[colucix@ocean-4 ~]$
Furthermore, if I redirect the output to a file and print out the content using cat:
Code:
[colucix@ocean-4 ~]$ awk 'NR==1{("cat " FILENAME " | wc -l") | getline NL} NR < NL; END{printf "%s", $0}' testfile > testfile_modified
[colucix@ocean-4 ~]$ cat testfile_modified
line one
line two
line three[colucix@ocean-4 ~]$
In other words apparently bash puts the prompt on a new line when you type a command (that does not produce any output) and press enter: actually, it puts the prompt on the same line with... "nothing".
 
Old 03-10-2010, 01:06 PM   #8
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
Code:
# Remove the last byte in a file
head -c-1 sample.xml > newsample.xml

# Remove the last byte in a file only if it is a newline.
# If the last byte isn't a '\n' then grep adds one, head removes it.
grep '^' sample.xml | head -c-1 - > newsample.xml
 
Old 03-10-2010, 01:25 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by Kenhelm View Post
Code:
# Remove the last byte in a file
head -c-1 sample.xml > newsample.xml

# Remove the last byte in a file only if it is a newline.
# If the last byte isn't a '\n' then grep adds one, head removes it.
grep '^' sample.xml | head -c-1 - > newsample.xml
Nice. Good catch!
 
Old 03-10-2010, 01:32 PM   #10
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
You're all missing the point; the last line break is supposed to be there, its pretty important that it is, though for the life of me I can't remember why; something to do with how the FS works.

Bugger, let me find a link


EDIT:
Thats why, you need it for opening files in append mode, for code (some compilers crap themselves without it), for importing text as input.

The line isn't a '\n' at all. Open it in a hex editor and see

Last edited by jamescondron; 03-10-2010 at 01:35 PM.
 
Old 03-10-2010, 11:59 PM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 7,183

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
chomp!
 
Old 03-11-2010, 12:04 AM   #12
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Quote:
Originally Posted by Kenhelm View Post
Code:
# Remove the last byte in a file
head -c-1 sample.xml > newsample.xml

# Remove the last byte in a file only if it is a newline.
# If the last byte isn't a '\n' then grep adds one, head removes it.
grep '^' sample.xml | head -c-1 - > newsample.xml
Yeah, that's better.
Good sense of switch usage.
 
Old 03-11-2010, 12:27 AM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk '{q=p;p=$0}NR>1{print q}END{ORS = ""; print p}' file
 
  


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
trying to remove a file name \ jlarsen Linux - Newbie 7 12-09-2009 03:47 PM
how to remove this file mokku Linux - Newbie 3 03-29-2008 02:34 PM
File showing with ? when using ls -al. How to remove file with only ? tacdog Red Hat 1 09-18-2007 11:12 AM
Bash remove part of a file based on contents of another file bhepdogg Programming 4 01-31-2007 04:13 PM
c++ file processing -- how to remove a record from a file sharonyiisl Programming 4 09-26-2004 04:54 AM


All times are GMT -5. The time now is 02:10 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration