ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Now there are over 500 txt files I need to run this script for.. each should generate a result.txt file (it can contain the orignal filename for easy identification)
How do I do this??
Also to take it to next level..
lets say there are multiple folders which have the INPUTFILE.txt.. it possible to do this same thing as above recursively and create a new output folder (if does not exist) as well for each folder processed with _RESULT in end name of folder.. ofcourse don't want to complicate things .. I can manually go into them and point the result to a manually created folder..
Again solution to 1st problem itself will be great
if it makes any difference, the code will run in RHEL..
I cannot modify the .pl script.. so just want to modify the execution style on bash
#!/bin/sh
# Executes script.pl over all the files specified on the CLI, preserving
# the file extensions of the original files
for FILE in "$@"
do
FILEEXT=`echo "$FILE" | rev | cut -d. -f1 | rev`
script.pl "$FILE" > "`basename "$FILE" $FILEEXT`-RESULT.$FILEEXT
done
find DIR(s)... -name '*.txt' -print0 | while read -rd "" source ; do
target="${source%.*}_RESULT.txt"
./script.pl "$source" > "$target"
done
Above, find will emit the files to work on using ASCII NULs as separators, handling even the weirdest file names correctly. This one looks for all files ending with .txt in DIR(s)... and their subdirectories.
The read -rd "" uses the Bash read built-in to read them into variable source one by one in the while .. ; do ... done loop.
target gets set to the value of source but with everything after the final . replaced with _RESULT.txt.
If you want to skip any existing result files, use
instead. The test is for existence, but || is else/otherwise, so the script is only run if $target does not exist yet. There is a short interval in between, when someone else could create $target. If that is a problem, use set -C to tell Bash to not redirect into existing files.
Would not the best option be to simply extend the perl script to cope with the new requirement?
Depends on how you define 'best', I suppose. My logic is that when a tool that does something very well already exits, I want to use that. In this case, that tool is find. Using the existing perl script also adheres to this principle. It is known to do one thing well, so use it that way, and build around it.
Nominal Animal demonstrates the power of find very nicely, and also demonstrates and explains a well structured way to approach the problem: iteration over a file set, especially with recursion, suggests using find. The output of find is a list, which feeds the while loop, so there is the iteration part of the problem solved. Once that part of the problem is addressed, he assembles a couple of key variables that are the arguments to the perl script. Finally, that last thing in each iteration is to invoke the perl script with arguments that are variables with nice human-readable names.
There is more to learn from Nominal's code than just how to solve the problem. It is a good demonstration of how structure the solution.
--- rod.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.