Extract a part of a filename containing a particular word

Hi All,
Thanks in Advance

Shell Script or Perl Script
I am working on a shell script. I need some assistance.

My Requirement:

1) There are some set of files in a directory like given below

OTP_UFSC_20120530000000_acc.csv
OTP_UFSC_20120530000000_faf.csv
OTP_UFSC_20120530000000_prom.csv
OTP_UFSC_20120530000000_subs.csv

I want to check if all thes files exists in the directory

the keywords are acc.csv, faf.csv, prom.csv, subs.csv
apart from this all the other things vary time to time (like the field seperator used in the file (can be.,-,_ etc) and the names OTP UFSC will vary.

2) if the file does not exist I have to touch (Create a empty file by that name)
Example : Imagine the file OTP_UFSC_20120530000000_acc.csv doesnot exist
in the directory
I have to search first if a file *acc.csv exist in the directory or not. If not i will extract the prefix anything before acc.csv (in this case OTP_UFSC_20120530000000_) from an existing file which is common to all the files in the directory
and create a file OTP_UFSC_20120530000000_acc.csv

:slight_smile:
Any suggestions and help

Which shell? Which OS?

If the prefix will always end with "", and if there are no "" but in the prefix, it's trivial.

Something like:

$ prefix="$(ls |grep -o '.*_' |head -n1)
$ [ -f *acc.csv ] || touch "$prefix"acc.csv
$ [ -f *faf.csv ] || touch "$prefix"faf.csv
$ [ -f *prom.csv ] || touch "$prefix"prom.csv
$ [ -f *subs.csv ] || touch "$prefix"subs.csv

--
Bye

1 Like

Or take the first 24 characters of the filename:

prefix=$(ls|head -1| sed 's/^\(.\{24\}\).*/\1/')
for file in acc faf prom subs ; do
        [ -e ${prefix}${file}.csv ] || touch ${prefix}${file}.csv
done
1 Like

Hi Thanks for your quickest reply.
Also the no of letters before the acc.csv varies and it also can have any field seperators like (- . _) etc
Please can you help me.

---------- Post updated at 09:27 PM ---------- Previous update was at 09:24 PM ----------

This is my code and its not working
please provide me a suggestion

 if [ "$VERSION" = "1.1" ]
        then 
              set  "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator" "pam_account";
              for i in 1 2 3 4 5 6 7 8;
              do
              count=`ls /mnt/sf_MY_LAPTOP/SDPDUMP/*.csv|grep -i _\.csv$|wc -l`;
              if [ $count -eq 0 ]
              then
                                      touch <file_prefix>_$1.csv;
             shift
             elif
             exit;
done
fi
done

So your only way to find the prefix is to extract the longest common starting substring among all the names of files in the directory, isn't it?
--
Bye

In what way is it "not working"? Be specific.

awk can have a string delimiter supplied and parse the first part as a separate string. Use that for determining your file names.

ls -1 *acc.csv | awk -F"acc.csv$" '{print $1}'

You also have other open threads with the same question. You may want to close them.

1 Like
#!/usr/bin/perl

@a=qw(acc faf prom subs);
opendir(DH,"/dir/path");
$flg=1;
@files=grep(/.*\.csv/,readdir(DH));
while (<@files>) {
undef $file_Pre;
if (/(.+?[_-]\d{14}[_-])(.+?)\.csv$/){
        for ($i=0;$i<=$#a;$i++) {
                $file_Pre .= $1 . $a[$i]."\.csv";
                if ( ! -e $file_Pre ) {system("touch $file_Pre"); }
                undef $file_Pre;
        }
}
}

close(DH);