grep

How to prevent grep from printing a trailing newline?

六眼飞鱼酱① 提交于 2020-08-21 04:22:01
问题 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? 回答1: You can pipe it through tr and translate the \n to

How to prevent grep from printing a trailing newline?

▼魔方 西西 提交于 2020-08-21 04:21:53
问题 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? 回答1: You can pipe it through tr and translate the \n to

How to prevent grep from printing a trailing newline?

拈花ヽ惹草 提交于 2020-08-21 04:21:09
问题 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? 回答1: You can pipe it through tr and translate the \n to

Git grep print only matching pattern

落花浮王杯 提交于 2020-08-08 06:51:08
问题 In the manual for grep , we have -o to print --only-matching patterns. Say echo foobar | grep foo would return foobar , but adding -o to grep would give only foo . Many grep options like -P , -c , etc, can be used in conjunction with git to search through all files in the Git index. However, git grep -o PAT triggers an error: unknown switch o'`. How can I print only matched string for each file in the Git index? i.e. " git grep -o PAT " My trial: for f in `git ls-files`; do grep -o PAT $f;