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?
相关问题
- JQ: Select when attribute value exists in a bash a
- bash print whole line after splitting line with if
- “command not found” errors in expect script execut
- grep using grep results
- UNIX Bash - Removing double quotes from specific s
相关文章
- Check if directory exists on remote machine with s
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
- Bash script that creates a directory structure
- Test if File/Dir exists over SSH/Sudo in Python/Ba
- How can I create a “tmp” directory with Elastic Be
- Spread 'sed' command over multiple lines
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 'tr' utility, or to Perl if preferred:
You can also use command substitution to remove the trailing newline:
Please note that I disagree with the OP's decision to choose the accepted answer. I believe one should avoid using 'xargs' where possible. Yes, it's a cool toy. No, you do not need it here.
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 'head' from 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 'cat' and the 'show-all' flag. The dollar sign character will indicate the end of each line:
There is also direct support for white space removal in Bash variable substitution:
If you assign its output to a variable,
bash
automatically strips whitespace:printf already crops the trailing newline for you:
Detail:
%s
string place holder.%s\n
), it won't.One way:
If you want to print output of anything in Bash without end of line, you echo it with the
-n
switch.If you have it in a variable already, then echo it with the trailing newline cropped:
Or you can do it in one line, instead: