Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to produce this behaviour:

grep 192.168.1 *.txt

By passing a string into grep via Xargs but it is going on the end instead of as the first parameter.

echo 192.168.1 | xargs grep  *.txt

I need to tell xargs (or something similar) to put the incoming string between 'grep' and '*' instead of on the end.

How do I do this?

share|improve this question
    
add comment

2 Answers 2

up vote 2 down vote accepted
$ echo 192.168.1. | xargs -I{} grep {} *.txt

Example

Sample files:

$ cat {1..3}.txt
192.168.1
192.168.1
192.168.1

Example run:

$ echo 192.168.1. | xargs -I{} grep {} *.txt
1.txt:192.168.1.
2.txt:192.168.1.
3.txt:192.168.1.
share|improve this answer
add comment

Why not to use

grep $(comand-producing-output) *.txt
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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