|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. ![]() |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > regex shell script. replace with command tool's output? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|