Solved

script to replace files names and text

Posted on 2001-07-18
9
159 Views
Last Modified: 2013-12-26
hi .

I have many files

each file begins with :

tg_BLA_BLA.pm

each file contain some text begins with tg_ or TG_

1. replace names tg_* > ft_*
2. replace text tg_* or TG_* > ft_* or FT_* accordingly .

Tal
0
Comment
Question by:Talmash
9 Comments
 
LVL 8

Expert Comment

by:mnashadka
Comment Utility
Something like this would probably work for you:

#!/bin/ksh

for file in tg_*.pm
 do
  sed s/tg_/ft_/g $file | sed /TG_/FT_/g  "ft_"${file#tg_}
 done

Hope this helps.
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 100 total points
Comment Utility
find . -name tg_\* -exec "perl -i -pe 's/tg_/ft_/g;s/TG_/FT_/g' {} ;echo {} XXXX {} \;"|sed -s 's#XXXX ./tg_#ft_/'|sh
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
perl -i -pe 's/(tg_)/"ft_"^$1^lc($1)/ieg'
0
 
LVL 8

Expert Comment

by:mnashadka
Comment Utility
I'm sorry, my line should be:
sed s/tg_/ft_/g $file | sed /TG_/FT_/g > "ft_"${file#tg_}

(missed the redirect operator).

ahoffman and ozo: why would you start up the whole perl interpreter for sed commands?  The perl interpreter is by far the fattest, most expensive interpreter (outside of Java).  I'm not attacking you; I'm just curious.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
.. 'cause sed sucks in performance. Even a fat perl is faster in most cases ;-) and works identically on all supported platforms, and is much more comfortable in using regex, and .. and .. and ...
BTW, even (g)awk is faster than sed (at least on all Solaris and IRIX).

Talmash , sorry for off-topic comment.
0
 
LVL 8

Expert Comment

by:mnashadka
Comment Utility
ahoffman, doesn't that really depend upon what you're doing.  If your going through thousands of records, I would agree; but if you're talking about a couple of hundred records, then the invocation of the perl shell takes longer than the whole sed thing.  And you're still using sed in your answer.  Anyway, it doesn't matter, you just have to use the right tool for the job.  Sorry for the comment, and thanks for the info.

Talmash, I'm sorry for the off-topic comment as well.
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
mnashadka, I started using the right tools (like sed:) before Larry started programming perl, and I still prefer it for simple (short) things (see my example), but as soon as things get more complex, sed sucks.
I just gave you a general answer for your comment ;-)

anyway, hope we can focus on Talmash's problem now ..
0
 
LVL 3

Expert Comment

by:mrn060900
Comment Utility
To change the text in a file you could use:

find . -type f -print | xargs file | grep -i text | cut -f1 -d: | while read

  file; do

vi $file >/dev/null 2>&1 <<!

:%s,Linux,GNU/Linux,g

:wq

!

done

Regards Mike
www.unixonline.co.uk
0
 
LVL 6

Author Comment

by:Talmash
Comment Utility
do I have something else to say but thank you very much .

Tal , moshav nechalim
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now