Extract a part of a filename containing a particular word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract a part of a filename containing a particular word
# 1  
Old 08-27-2012
MySQL Shell/perl Script : 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

Code:
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

Smilie
Any suggestions and help
Moderator's Comments:
Mod Comment code tags please

Last edited by aealexanderraj; 08-27-2012 at 12:40 PM.. Reason: have to tag the quote
# 2  
Old 08-27-2012
Quote:
I am working on a shell script.
Which shell? Which OS?

Quote:
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
If the prefix will always end with "_", and if there are no "_" but in the prefix, it's trivial.

Something like:
Code:
$ 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
This User Gave Thanks to Lem For This Post:
# 3  
Old 08-27-2012
Or take the first 24 characters of the filename:

Code:
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

This User Gave Thanks to Subbeh For This Post:
# 4  
Old 08-27-2012
MySQL

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
Code:
 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

Moderator's Comments:
Mod Comment code tags please
# 5  
Old 08-27-2012
Quote:
Originally Posted by aealexanderraj
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.
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
# 6  
Old 08-27-2012
In what way is it "not working"? Be specific.
# 7  
Old 08-27-2012
awk can have a string delimiter supplied and parse the first part as a separate string. Use that for determining your file names.

Code:
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.

Last edited by bomberman; 08-27-2012 at 03:11 PM.. Reason: noticed other threads
This User Gave Thanks to bomberman For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract date and time part from filename

Hi, I am facing one scenario in which I need to extract exact position of date and time from the name of the files. For example, Below is the record in which I need to extract position of YYYYMMDD,HHMISS and YYMMDD. Date and time variables can come more than once. I need to use these position... (13 Replies)
Discussion started by: Prathmesh
13 Replies

2. UNIX for Dummies Questions & Answers

Extract Date part from the filename

Hi All, I have incoming source files abcmmyy.txt I need to extract the mmyy part from the filename and pass that to a variable . I really appreciate your quick response on this. Thanks raj (7 Replies)
Discussion started by: rajeevm
7 Replies

3. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

4. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance 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... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

5. Shell Programming and Scripting

Extract part of word from XML

Hi All, Can Someone help me in capturing a word from xml Using sed or awk or any other way in unix. i have file abc.xml like this <?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?> - <NREC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> - <HEADER> <SOURCE>MAIL</SOURCE>... (5 Replies)
Discussion started by: naveenkumarc
5 Replies

6. Shell Programming and Scripting

Getting part of a filename

Hi All, I'm trying to get part of a filename and my skill with regular expression are lacking. I know I need to use SED but have no idea how to use it. I'm hoping that someone can help me out. The file names would be: prefix<partwewant>suffix.extension the prefix and suffix are always 3... (4 Replies)
Discussion started by: imonkey
4 Replies

7. Shell Programming and Scripting

extract whole thing in word, leaving behind last word. - perl

Hi, i've a string /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD/TESi T_11_HD_120/hd-12 i need to get string, like /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD the words from HD should get deleted, i need only a string till HD, i dont want to use any built in... (4 Replies)
Discussion started by: asak
4 Replies

8. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

9. Shell Programming and Scripting

Extract Part of a "Word", using AWK or SED????

I have been lurking on this forum for some time now and appreciate Everyone's help. I need to find a way to get the SystemID from this XML file. The file is much larger than just this one line but I can grep and get this line Printed. But really just need the "systemid". <test123: prefintem... (9 Replies)
Discussion started by: elbombillo
9 Replies

10. Shell Programming and Scripting

part of a filename

Hi, I need to extract only a part of the filenames of some files. The files are named this way : .tap_profile_SIT02 I want the "SIT02" part, which is not the same for each file. I was able to get what I want with bash, but not with ksh. Here is the command I used in bash : find... (8 Replies)
Discussion started by: flame_eagle
8 Replies
Login or Register to Ask a Question