I am writing a bash script where i will need to check a directory for existing files and look at the last 4 digits of the first segment of the file name to set the counter when adding new files to the directory.
Naming Scructure:
yymmddHNAZXLCOM0001.835
I need to put the portion in the example 0001 into a CTR variable so the next file it puts into the directory will be
yymmddHNAZXLCOM0002.835
and so on.
what would be the easiest and shortest way to do this?
You can do this with
sed:All three
sedcalls use the same regular expression. The only thing that changes is the group selected to return. There’s probably a way to do all of that in one call, but mysed-fu is rusty.Alternate version, using a Bash array:
Note: This version assumes that the filename does not have spaces in it.