Question

if you do grep -n, the output format is like this,

filename:line:

Is it possible for me to tell grep to replace the first : with + so that I can copy paste the string to vim to go to that line?

What I want is this,

filename +line:

Any idea how to achieve this?

Was it helpful?

Solution

Pipe the output to sed.

grep -n needle haystack1 haystack2  | sed 's/:/ +/'

If you then want to strip off the part after, so that it is even easier to paste to vim, you can pipe it to awk.

grep -n needle haystack1 haystack2  | sed 's/:/ +/' | awk -F':' '{print $1}'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top