LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   to extract all the part of the filename before a particular word in the file (https://www.linuxquestions.org/questions/linux-newbie-8/to-extract-all-the-part-of-the-filename-before-a-particular-word-in-the-file-4175424231/)

aealexanderraj 08-27-2012 10:33 AM

to extract all the part of the filename before a particular word in the file
 
Hi All,
Thanks in Advance

I am working on a shell script. I need some assistance.

My 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;

elif [ $count -gt 1 ]
then
echo "Only one <file_prefix>_$1.csv file can be loaded at a time";

fi
shift
done
fi
done

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

:)
Any suggestions and help

I am facing problems with my code

Please correct my code or suggest me with a better code

torchnw 08-27-2012 11:16 AM

Since you didn't print the entire code and didn't specify what problems you're getting, it hard to tell exactly what's wrong, but for starters your test condition is "-gt" ( greater than ), meaning you're testing for 2 or more instances. I'm guessing you probably want to use "-ge" ( greater or equal ).

Also in your for loop, you can use

Code:

for i in $(seq 1 8)
do
    ....
done


grail 08-27-2012 12:08 PM

Firstly, please place your code inside [code][/code] tags so it is legible.

As for your code snippet, I agree with the previous poster in that you have not explained what problems you are having, only what you wish to do?

chrism01 08-27-2012 08:51 PM

Both of the above :)
Also, try putting
Code:

set -xv
after the start of the file; this will show you what the parser is doing.


All times are GMT -5. The time now is 08:31 AM.