I have a file abc.txt in my home folder.

~$ cat abc.txt

I want want to execute a grep command like
~$ grep 'dat' abc.txt
which gives date in result and date is a shell command. So I want to execute the command which I got in grep result (date) in the same line of grep command.

Is it possible by using | operator like
~$ grep 'dat' abc.txt | [Some command syntax like xargs] which gives the result current time like Fri Nov 4 18:55:06 IST 2016

  • Do you mean cat abc.txt|grep dat? – Katu Nov 4 '16 at 13:52
  • Yes I want the same. I think it gives the same as ~$ grep 'dat' abc.txt – shubham12511 Nov 5 '16 at 8:16

Is this what you need?
note: this is using the bash shell

# cat abc.txt  
test line 1
date +%F  
test line 3

# $(grep dat abc.txt)  

You'll need to be careful on what you filter from the grep command -- all of it will be fired into the shell -- as a single command! So, get a single line match with your filter.

PS: There has to be a better way for this but it escapes me now.
Would help to know what you are really trying to get done here.

  • Yes! Exactly what I needed. Thanx! a lot for helping. – shubham12511 Nov 5 '16 at 8:22
  • Also you can use grep dat abc.txt|bash – makgun Nov 16 '16 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.