[SOLVED] processing 9 scripts sequentially or combing them into one
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
i didn't think (and i should have) that there are several csv inputFiles in my directory (all with unique names of course) so the csv outputFiles also need to have unique names (the script that works doesn't do that), and it would be best if the outputFile's name was automated as part of the script (not changed each time i run the script) based on the prefix of the inputFile's name, for example:
i'll give credit to boughtonp for the awk script (up above) that he did almost all of the work on, so i'm willing to do the leg work on this one if someone can point me in the right direction...
eventually i plan to "batch" process all the input files in the directory...
i didn't think (and i should have) that there are several csv inputFiles in my directory (all with unique names of course) so the csv outputFiles also need to have unique names (the script that works doesn't do that), and it would be best if the outputFile's name was automated as part of the script (not changed each time i run the script) based on the prefix of the inputFile's name, for example:
i'll give credit to boughtonp for the awk script (up above) that he did almost all of the work on, so i'm willing to do the leg work on this one if someone can point me in the right direction...
eventually i plan to "batch" process all the input files in the directory...
Tabby
Code:
for INFILE in *.csv
do
OUTFILE=OUT"$INFILE"
Process your scripts here reading from "$INFILE" and sending the output to "$OUTFILE"
done
This will process all the files in the current directory and manage both reading each file and renaming the output.
Once you have one script (or group of scripts) that gives the result you want simply enclose them in a "for" loop like above to 'batch' process all the files.
Would be nice if you learn awk and try to adjust that script to your needs instead of instruct us to do that job.
From the other hand in your last post you did not ask anything, so I don't really know if that is ok for you or do you still need help?
Sorry, now is a good time to backup your data if you don't already have one. You need to post exactly the input file names and the desired output if you want to use posted code verbatim. It took many posts to finally get you to post the exact data more or less and the exact desired output to keep from making the same mistakes throughout this thread.
You also might want to consider saving the files to another directory to keep the input files separate from the output files.
There are many many ways to modify strings to produce any naming scheme you desire.
As a separate script that runs your awk script.
Code:
#!/bin/bash
for infile in *.csv
do
#outfile=${file/input/output}
awk -f script.awk $inFile.csv > /path/to/output_directory/$outFile.csv
done
Just an FYI since you are running your script via awk (awk -f script.awk ...) the #!/bin/bash is treated as a comment.
hi pan64, i guess i'm learning awk as i go, doing my best to search through various resources i find on the net and applying the info i learn as best i can. i'm not a coder by any means and i don't have access to a coder to help me, so i struggle along doing my best. i've never seen an on-line class for awk, or i'd take it. (other programing languages look too intimidating), and really all i ever need to do is data manipulation on csv files and i think awk and sed are well suited for this. hopefully this answers your question?
michaelk your script almost works when i remove the .csv from $infile.csv your script correctly processes the last file (none of the proceeding files) and renames that one outfile as outfile.csv.csv
MadeInGermany your script does not create any outfiles.csv and it deletes all the data in the infiles.csv ooops
tinkering around with what you guys showed me, i wrapped the script.awk with a for loop like this
Code:
#!/bin/bash
for file in *.csv;
do
awk 'BEGIN... the rest of the awk script
}' "$file" > "$(basename "file").csv"
done
and it worked only the output files have a double csv extensions, so they look like blahblablah.csv.csv so i'm really really close. how can i get rid of the double csv extension? or even better would be to rename the outputfile (i'm working on that one). i'm thinking of maybe a call to sed before the done?
tinkering around with what you guys showed me, i wrapped the script.awk with a for loop like this
Code:
#!/bin/bash
for file in *.csv;
do
awk 'BEGIN... the rest of the awk script
}' "$file" > "$(basename "file").csv"
done
and it worked only the output files have a double csv extensions, so they look like blahblablah.csv.csv so i'm really really close. how can i get rid of the double csv extension? or even better would be to rename the outputfile (i'm working on that one). i'm thinking of maybe a call to sed before the done?
thanks soooo much!!!
Tabby
Try changing this
Code:
awk 'BEGIN... the rest of the awk script
}' "$file" > "$(basename "file").csv"
to this
Code:
awk 'BEGIN... the rest of the awk script
}' "$file" > "out$(basename -s .csv "$file").csv"
That will allow the basename command to strip the original .csv from $file and add "out" at the beginning of the original file name for the output file.
YOu could also simplify that with
Code:
awk 'BEGIN... the rest of the awk script
}' "$file" > out"$file"
so all that is added is the prefix out to the original file name.
There are lots of variations possible so you have to decide what works best for your needs.
I have edited my post, so it won't write back to the input file if nothing matched.
--
Code:
#!/bin/bash
for file in *.csv;
do
awk 'BEGIN... the rest of the awk script
}' "$file" > "$(basename "file").csv"
done
Even if it were "$file" I do not see the point of using basename here.
But you must write to a new file, writing back to the original file will trash it.
The following strips .csv and adds .out
Code:
#!/bin/bash
for file in *.csv;
do
awk 'BEGIN... the rest of the awk script
}' "$file" > "${file%.csv}.out"
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.