When I execute commands in Bash (or to be specific, wc -l < log.txt), the output contains a linebreak after it. How do I get rid of it?
When I execute commands in Bash (or to be specific, wc -l < log.txt
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If your expected output is a single line, you can simply remove all newline characters from the output. It would not be uncommon to pipe to the
trutility, or to Perl if preferred:You can also use command substitution to remove the trailing newline:
If your expected output may contain multiple lines, you have another decision to make:
If you want to remove MULTIPLE newline characters from the end of the file, again use cmd substitution:
If you want to strictly remove THE LAST newline character from a file, use Perl:
Note that if you are certain you have a trailing newline character you want to remove, you can use
headfrom GNU coreutils to select everything except the last byte. This should be quite quick:Also, for completeness, you can quickly check where your newline (or other special) characters are in your file using
catand the ‘show-all’ flag-A. The dollar sign character will indicate the end of each line: