If your expected result is one line , you can simply remove all newline characters from the output. It would be no coincidence to connect to the tr utility or to Perl if necessary:
wc -l < log.txt | tr -d '\n' wc -l < log.txt | perl -pe 'chomp'
You can also use command substitution to delete the trailing newline:
echo -n "$(wc -l < log.txt)" printf "%s" "$(wc -l < log.txt)"
Please note that I do not agree with the OP's decision to select the accepted answer. I believe that the use of βhargsβ should be avoided where possible. Yes, this is a cool toy. No, you donβt need it here.
If your expected output may contain multiple lines , then you have another solution:
If you want to remove the MULTIPLE newlines from the end of the file, use the cmd replacement again:
printf "%s" "$(< log.txt)"
If you want to strictly remove the LAST character of the newline from the file, use Perl:
perl -pe 'chomp if eof' log.txt
Note: if you are sure that you have a trailing newline that you want to remove, you can use the "head" from GNU coreutils to select everything except the last byte. This should be pretty fast:
head -c -1 log.txt
In addition, for completeness, you can quickly check where your newlines (or other special characters) are in your file using "cat" and the "show-all" flag. A dollar sign will indicate the end of each line:
cat -A log.txt
Steve Sep 21 2018-12-12T00: 00Z
source share