I am wanting to automate a process that includes the step of appending to a filename a string of text that's contained inside the file. I.e. if filename A.fileA contains a string of text that reads 1234 after the phrase ABC, I want the shell script file to rename the file 1234_FileChecked_A.fileA.
Any hints?
Thanks.
#!/bin/ksh
file='A.fileA'
pre='ABC'
post='1234'
pattern="${pre}.*${post}"
[[ $(egrep -c "${pattern}" "${file}") -ge 1 ]] && mv "${file} "${post}_FileChecked_${file}"
Thanks.
For the line reading "file = 'A.fileA', if there are multiple files I want to apply the mentioned task to, how would the value for that field be filled?
I guess the same question applies for the post value as well, since that value would be different from file to file.
Thanks!
how do get a list of the files to process?
you'll need to put a 'loop' around the statement to iterate through all the needed files. For all the files in the current directory:
#!/bin/ksh
pre='ABC'
post='1234'
pattern="${pre}.*${post}"
find . ! -name . -prune -type f | while read file
do
[[ $(egrep -c "${pattern}" "${file}") -ge 1 ]] && mv "${file} "${post}_FileChecked_${file}"
done
How would this value change in conjunction with the applicable files?