Operating System - HP-UX
1835202 Members
2568 Online
110077 Solutions
New Discussion

Re: Replace one single line in files with equal filename

 
Rainer Naber
Occasional Contributor

Replace one single line in files with equal filename

Hi,
Could someone help me with that?
I want to find files (all with the same filename) in a file hierarchy (tree structure). Some files have one equal line that I want to replace by an other line. Problem is, the files are in different directories and subdirectories. So "find" and "grep" alone are not enough, I have to know about the whole pathname.
5 REPLIES 5
Pete Randall
Outstanding Contributor

Re: Replace one single line in files with equal filename

I guess I don't see the problem. Find returns the full path:

$ find /home -name plrtest
$ /home/plr/plrtest

So you use find to identify the files then use -exec or xargs to invoke sed to change the line! Or am I missing something?


Pete

Pete
Fred Ruffet
Honored Contributor

Re: Replace one single line in files with equal filename

you can use grep in find.

find /mypath -type f -exec grep -l pattern {} \;
will give you a list of all files in /mypath containing the string "pattern". You can send its output into sed like this :
find /mypath -type f -exec grep -l pattern {} \; | while read filename
do
cp ${filename} ${filename}.orig
sed "s/pattern/replacement/g" ${filemane}.orig > ${filename}
done

This will replace "pattern" by "replacement" in all files under /mypath. It will also left a .orig file for each file.

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Thierry Poels_1
Honored Contributor

Re: Replace one single line in files with equal filename

hi,

how about:

find /topdir -name filename -exec grep -l equal_line {} \;

"grep -l" will show the filenames you need.

regards,
Thierry Poels.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Jean-Luc Oudart
Honored Contributor

Re: Replace one single line in files with equal filename

I try with find and grep and givve you the list of files (full path) where specific line exist :

find -name "" -exec grep -l "" {} \;

Redirect output into a file to obtain list (with full path) for these files.

Regards
Jean-Luc
fiat lux
hein coulier
Frequent Advisor

Re: Replace one single line in files with equal filename

find /dir -type f -name fname \; |
while read fname
do
ed -s ${fname} <