42

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?

wallyk
  • 56,922
  • 16
  • 83
  • 148
Cobra_Fast
  • 15,671
  • 8
  • 57
  • 102

3 Answers3

46

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
$ 
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • Without tr gdb shows `4\nj\000\000`, with tr it shows `4\346j\000\000` as the value of what it gets back from the command line. 4 is the counted number which I want. – Cobra_Fast Aug 30 '11 at 21:09
  • Are you sure that `grep` is producing those characters after the number? – johnsyweb Aug 30 '11 at 21:13
29

You can pipe it through tr and translate the \n to a \0 character:

tr '\n' '\0'
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
  • 4
    Is not valid, by example: `echo -n 'x' | md5sum` is not same results using `echo 'x' | grep 'x' | tr '\n' '\0' | md5sum`. – e-info128 Jul 17 '21 at 01:46
15

I know this is old, and tr works just as well, but I happened across this question and noticed OP stated: I am executing in sh, not bash. So nesting it into echo -n "$(grep -c pattern)" doesn't work either.

This isn't grep or sh so much as how echo is being used. For future visitors, the only reason this didn't work is due to the double quotes around the substituted command. The following does, in fact, work even using sh.

echo -n $(grep -c pattern)

Examples:

$ ls /dev/sd? #example of formatted output
/dev/sda  /dev/sdc  /dev/sde  /dev/sdg  /dev/sdi  /dev/sdk
/dev/sdb  /dev/sdd  /dev/sdf  /dev/sdh  /dev/sdj

$ echo $(ls /dev/sd?) #without -n, appends \n only at the end
/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh /dev/sdi /dev/sdj /dev/sdk

$ echo -n $(ls /dev/sd?) #with -n, does not append the \n, but still strips the line breaks from the string
/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh /dev/sdi /dev/sdj /dev/sdk

$ echo -n "$(ls /dev/sd?)" #output when double quotes are used
/dev/sda
/dev/sdb
/dev/sdc
/dev/sdd
/dev/sde
/dev/sdf
/dev/sdg
/dev/sdh
/dev/sdi
/dev/sdj
/dev/sdk
user.friendly
  • 2,099
  • 14
  • 12