LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-27-2012, 11:33 AM   #1
aealexanderraj
LQ Newbie
 
Registered: Aug 2012
Posts: 3

Rep: Reputation: Disabled
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
 
Old 08-27-2012, 12:16 PM   #2
torchnw
LQ Newbie
 
Registered: Jun 2008
Distribution: Ubuntu, Debian, Mint, Fedora, Arch
Posts: 23

Rep: Reputation: 4
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
 
Old 08-27-2012, 01:08 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
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?
 
Old 08-27-2012, 09:51 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
to extract all the part of the filename before a particular word in the filename aealexanderraj Programming 1 08-27-2012 12:08 PM
to extract all the part of the filename before a particular word in the file aealexanderraj Linux - Newbie 1 08-27-2012 12:07 PM
bash shell script read file word by word part 2 justina Programming 7 01-25-2011 02:19 PM
[SOLVED] how to release file lock in SAMBA 3.2.5 share (MS Word [filename] is locked for edit. albertwt Linux - Newbie 5 11-04-2010 09:21 AM
Extract part of file name himu3118 Programming 6 03-26-2010 09:52 AM


All times are GMT -5. The time now is 12:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration