Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find and xargs/exec variable 2

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
US
Hi, all

I have not used find extensively, usually because
I could do it another way with greater flexibility.
Yesterday I had a situation where it was perfect.
A number of files in various subdirectories, all named identically, needed one line changed.
Calling find I was able to do what I wanted but unable
to commit the changes in a one-liner because I could
not save finds \{\} subbed variable!
I had to do it this way:
Code:
find $dirname -type f -name filename | xargs echo >> tmp.txt 
    for all in `cat tmp.txt`  
    do
       sed 's/line1/line1againmorestuff/' $all > $tmp        && mv $tmp $all
    done
    rm tmp.txt ; rm $tmp

Is there something basic I was missing?
 
Marsd:

How about:

# untested:
find $dirname -type f -name filename | xargs |while read line; do
sed 's/line1/line1againmorestuff/' $line > $tmp && mv $tmp $line
done

Regards,

Ed
 
I'll try it next time ed, thanks.
 
This should do the trick without having to rename the file each time the replacement is made:

for i in `find $dirname -type f -name filename` ; do
perl -p -i -e "s/line1/line1againmorestuff/;" $i
done

-jim
 
I dislike perl with a passion and would never use it for
anything even as trivial as this.

But sure, I should have thought of cutting out the
intermediate file stage and just grabbing the OP of find
instead of getting fixated.

Thanks.
 
Marsd:

Your welcome.

Now, off topic:

As a Perl newbie, I was wondering why you dislike Perl. Would you care to share your thoughts?

Also, this is one person's thoghtful opinion of what's wrong with Perl:


I'm not interested in a flame war - just an exchange of information.

Regards,

Ed
 
J1mbo is rigth, but not simply (not using perl):

for file in `find $dirname -type f -name filename`
do sed -e 's/line1/line1againmorestuff/' $file > output
[ -s $output ] && mv output $file
done

marsd: i agree.
 
Given the use of perl in this case, that makes as much sense as saying i dislike [insert any other unix utility here].

-jim


 
J1mbo,
You have a point but it is not very concise.
Why use a language you truly dislike for any
number of reasons when a solution like iribachs
is more congenial?

olded,
I think any further comments would probably
be too negative. I've posted many tcl/expect
solutions to this forum and everyone has been
polite. I think that's the way to go. ;)
 
A one liner to do exactly what you want, using *gasp* perl ;-)
 
Incipient flamewar averted here.
Have a nice day.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top