SunQuest
           UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old February 19th, 2004, 07:50 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
regex shell script. replace with command tool's output?

hiyer,

i'm trying to write a shell script (#!/bin/sh) and am having trouble with a bit of it. i want to use a regex search to find a pattern in a text file, pass that find to a command line tool (via the first argument after its name), and that tool processes the input and outputs a replacement, which should be used to replace the originally found bit of text. then do that throughout the whole file. the text file itself should be modified with the new replacements.

so say the text file's path is in $name and processfind is the command line tool that processes the finds and say the pattern that's being searched for in the text is [0-3][0-3][0-3] -- how can i do that?

so that's:

find [0-3][0-3][0-3] in $name
pass that find to command line tool processfind
use processfind's output to replace the find
keep doing until end of $name

possible? how?

any ideas much appreciated. thanks.

Reply With Quote
  #2  
Old February 19th, 2004, 10:55 AM
fpmurphy fpmurphy is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: USA
Posts: 257 fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level)fpmurphy User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 23 h 46 sec
Reputation Power: 6
Humm, this sounds like a job for Perl or awk. Pretty
easy if you have a lookup table of original text strings and
replacement text strings.

Reply With Quote
  #3  
Old February 19th, 2004, 11:55 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
ok, perl. maybe. (lookup table not feasable). i have o'reilly's "mastering regular expressions" and in there there's a bit of perl that does something very similar to what i'm asking (the regex part that is) in the book's introduction to "backreferencing". but, obviously perl can handle the regular expression part, but can perl handle (i'm a beginner to all this scripting buiness so it may well be very easy for perl - i don't know) the file part, like:

Code:
#!/bin/sh

files="atextfile anothertextfile andanother"

for name in $files; do
	/bin/cp $name $name"1"
	# regex find, then process, then replace in $name"1"
done


?

if that's easy in perl then i guess perl it is. also just to say that the above script isn't quite there - i have yet to make it search for files ending in a particular extension in the present working directory, and use those files in the for loop (rather than specifying the within the script). also i'd like to do some file date/time checking and only add files to the list to be done if they've been modified after a particular time. -- it just seems that a perl script isn't so well suited for that sort of thing?? so maybe a good sollution would be to have both - the above shell script should do its thing, call the perl script (from where it says "# regex find,"...) which does the regex part on each file? so a shell script that calls a perl script that calls the command line tool? maybe perl can do the above (directory searching, copying, file date checking)?

thanks.

Last edited by balance : February 19th, 2004 at 11:57 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > regex shell script. replace with command tool's output?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway