1

I use grep to find unique (marker) text among a large number of files.

grep MarkerText -r -C 30 -h ~/helpfiles/*

Is it possible to feed a highlighted text as input to my grep command so that the highlighted text replaces MarkerText in this command? I am hoping to avoid having to copy and paste all the time.

Ravexina
  • 57,256

2 Answers2

1

If you can install xclip package, try:

grep `xclip -o` -r -C 30 -h ~/helpfiles/*

"xclip -o" in above command will get replaced with whatever's on your clipboard.

1

Yes, sure - there is PRIMARY selection in X11.

PRIMARY selection, is used when the user selects some data. X Window: Clipboard

You can use either xsel or xclip cli tools:

TL;DR

  1. Select some text

2a. grep "$(xsel)" -r -C 30 -h ~/helpfiles/*

OR

2b. grep "$(xclip -o)" -r -C 30 -h ~/helpfiles/*

Precondition

  • xsel or xclip package installed: apt get install xsel or apt get install xclip

  • X server is running (i.e. you use X server, not text-mode without X server): xset q > /dev/null && echo "X is running" || echo "start X server"

Explanation

Both xsel and xclip are clipboard management tools. Commands xsel and xclip -o print to STDOUT contents of PRIMARY selection. More info you can find here: 'xclip' vs. 'xsel'

Use double quotes around $(). This allows to highlight more than 1 word.

Yasen
  • 231