Hi,

I want to know just the name of the files that contains a given pattern. Then I would open the files in vi and replace the pattern. But using

grep -n '&&&' *

gives all the occurrence of the pattern which is not very helpful.

How do I view only the names?

Recommended Answers

All 3 Replies

Maybe if you gave us an example of filenames and then the filtered list.

This is not very relevant here. Because the same functionality should be agnostic to the pattern and file content. Still if you *insist* I can give you the file and pattern:

file
abcdefg&&&hijklmonp&&&qrs&&&tuv&&&wx&&&yz

pattern
&&&

replace pattern
----

so
abcdefg&&&hijklmonp&&&qrs&&&tuv&&&wx&&&yz

becomes
abcdefg---hijklmonp---qrs---tuv---wx---yz

This in from the manpages

1. How can I list just the names of matching files?

grep -l 'main' *.c

lists the names of all C files in the current directory whose
contents mention `main'.

So this should open each one in vi
vi grep -l 'main' *.c

Actually on my system I found this works.

vim $(grep -l 'main' *.c)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.