I am using grep
to produce output that will be parsed by another program.
However, that program expects output only to be numeric or zero-bytes.
Now grep
outputs a newline character after its output. I've checked the -Z
option but it doesn't seem to work as I'm using grep for counting (-c
).
I am executing in sh
, not bash
. So nesting it into echo -n "$(grep -c pattern)"
doesn't work either.
How can I get rid off the trailing newline?
The `` or $() will remove the newline from the end, but to do this programatically, use tr . This will remove the carriage return and/or the newline from the string.
To suppress the default grep output and print only the names of files containing the matched pattern, use the -l ( or --files-with-matches ) option.
To match empty lines, use the pattern ' ^$ '. To match blank lines, use the pattern ' ^[[:blank:]]*$ '. To match no lines at all, use the command ' grep -f /dev/null '.
Use tr -d
to delete characters in a string:
$ grep -c ' ' /etc/passwd | tr -d '\n' 69$ grep -c ' ' /etc/passwd | tr -d '\n' | xxd 0000000: 3639 69 $
You can pipe it through tr
and translate the \n
to a \0
character:
tr '\n' '\0'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With