LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 12: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, 01:02 AM   #2
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

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

Last edited by vinaytp; 03-10-2010 at 01:15 AM.
 
Old 03-10-2010, 03: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, 03:05 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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, 03: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, 04:21 AM   #6
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

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 04:22 AM.
 
Old 03-10-2010, 04:58 AM   #7
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 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, 12:06 PM   #8
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
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, 12:25 PM   #9
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
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, 12:32 PM   #10
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 70
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 12:35 PM.
 
Old 03-10-2010, 10:59 PM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,609
Blog Entries: 4

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

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-10-2010, 11:27 PM   #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
 
Old 01-01-2019, 09:09 AM   #14
aggresss
LQ Newbie
 
Registered: Jan 2019
Posts: 1

Rep: Reputation: Disabled
Thumbs up Helpful idea

It works , thanks a lot !

Quote:
Originally Posted by colucix View Post
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 01-01-2019, 12:52 PM   #15
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,768

Rep: Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192
With shell builtins
Code:
nl=
while IFS= read line
do
  printf "$nl%s" "$line"
  nl='\n'
done < textfile > incompletefile
Note that the standard text processing tools (like sed and awk) might not properly handle an incomplete text file.
So more often you want to do the opposite
Code:
{
while IFS= read line
do
  echo "$line"
done
[ -n "$line" ] && echo "$line"
} < incompletefile > textfile
 
  


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:16 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