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 |
Very simple
Code:
tr -d '\n'< sample.xml |
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?
|
Quote:
Code:
c@CW8:~$ < /dev/null |
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.
|
Quote:
you may inspect this with Ocatal dump after G and dd Code:
od -t c sample.xml Code:
var=`wc sample.xml | tr -s ' ' | cut -d ' ' -f 2` |
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 Code:
[colucix@ocean-4 ~]$ awk 'NR==1{("cat " FILENAME " | wc -l") | getline NL} NR < NL; END{printf "%s", $0}' testfile > testfile_modified |
Code:
# Remove the last byte in a file |
Quote:
|
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 |
chomp! :hattip:
|
Quote:
Good sense of switch usage. |
Code:
awk '{q=p;p=$0}NR>1{print q}END{ORS = ""; print p}' file |
Helpful idea
It works , thanks a lot !
Quote:
|
With shell builtins
Code:
nl= So more often you want to do the opposite Code:
{ |
All times are GMT -5. The time now is 12:05 AM. |