Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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".
# 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
# 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
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.
# 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
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".
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.