loop and extract matching filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting loop and extract matching filename
# 1  
Old 06-29-2010
Java loop and extract matching filename

i am unable to exact matching filename in the loop.

Filename.in file contains

Code:
Customer_Product_
Information_Customer_Product_
sale_Product_

/home_dir contains files


Code:
CUST_INFO_990090_1111.csv
"1","Customer Product Detail","1000","salary"
"1","Information Customer Product Detail","1001","salary"
"1","Customer Product Detail","1002","salary"
"1","Information Customer Product Detail","1003","salary"
"1","saleProduct Detail","1004","salary"

Script:

Code:
for file1 in `ls /home_dir/|grep CUST_INFO|grep csv`
do
  SID=`echo $file1 | cut -d '_' -f4 | awk -F"." '{print $1}'`
  PID=`echo $file1 | cut -d '_' -f3`

    for file in `cat /home_dir/filename.in`
    do
        var1=`echo $file"Detail"`
        var2=`echo $var1|  awk '{gsub(/_/, " ");print}'`
        grep "$var2" /home_dir/$file1 > `echo /home_dir/$file"Detail_"$PID"_"$SID".txt"`
    done
done


Finally i am creating the 3 files as required for business logic.

1) Information_Customer_Product_990090_1111.csv

Code:
"1","Information Customer Product Detail","1001","salary
"1","Information Customer Product Detail","1003","salary

2) sale_Product_990090_1111.csv

Code:
"1","saleProduct Detail","1004","salary

3) Customer_Product_Detail_990090_1111.csv
Code:
"1","Customer Product Detail","1000","salary
"1","Information Customer Product Detail","1001","salary
"1","Customer Product Detail","1002","salary
"1","Information Customer Product Detail","1003","salary

Now the 3) file having both (Customer Product Detail and Information Customer Product Detail) information.
As per the requirement i need to fetch only Customer Product Detail information in this file.


i am exactly having problem here

Tried with many options.i am unable to extract the filename beacuse the file itself having double quotes.It is not fetching the exact value.


Code:
grep "$var2" /home_dir/$file1 > `echo /home_dir/$file"Detail_"$PID"_"$SID".txt"`
grep -w "$var2" /home_dir/$file1 > `echo /home_dir/$file"Detail_"$PID"_"$SID".txt"`
grep -w '"$var2"' /home_dir/$file1 > `echo /home_dir/$file"Detail_"$PID"_"$SID".txt"`
awk -F"," '{ if ( $0 ~ /"$var2"/ ) print $0}'  /home_dir/$file1 > `echo /home_dir/$file"Detail_"$PID"_"$SID".txt"`


Any help greatly appricated.

Thanks
Suri

---------- Post updated at 09:39 PM ---------- Previous update was at 07:16 AM ----------

Hi,

I have 3 files

1) Information_Customer_Product_990090_1111.csv
2) Customer_Product_Detail_990090_1111.csv
3) sale_Product_990090_1111.csv
Example:
Based on file name =”Customer_Product_Detail” I am fetching data.
As I am I getting two files when I am trying to fetch “Customer_Product_Detail” information.
I am fetching two files information when I try to fetch “Customer_Product_Detail”

Information_Customer_Product_990090_1111.csv
Customer_Product_Detail_990090_1111.csv

I want to fetch exact file name in the script.
With in the for loop i using the below command for fetching filename.


grep "$var2" /home_dir/$file1 > `echo /home_dir/$file"Detail_"$PID"_"$SID".txt"`

any help greatly appricated.

Thanks
Suri

Last edited by pludi; 06-29-2010 at 09:24 AM.. Reason: code tags, please. Final Warning
# 2  
Old 06-29-2010
Try...
Code:
grep "[^ ]$var2" .....

# 3  
Old 07-01-2010
MySQL

Thanks Ygor

It is working fine.

This issue is closed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find: filename in every subdirectory matching a pattern

Hi, I have multiple directories built in following manner /app/red/tmp /app/blue/upd /app/blue/tmp /app/green/tmp /app/red/upd /app/green/upd I have filenames having pattern ONE.XXX.dat TWO.ZZZ.dat and so on across the folders listed above My objective is to list all filenames of a... (4 Replies)
Discussion started by: wahi80
4 Replies

2. 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

3. 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

4. UNIX for Dummies Questions & Answers

Help with filename pattern matching

Hi, I have files in a directory with filenames that match three specific patterns: 1) *'.L2_LAC'* 2) *'.L2_LAC_OC'* 3) *'.L2_LAC_SST'* I would like to have an "ls" command that will list only files matching the first two patterns. However, if I type: ls *'.L2_LAC'* I will get files that... (2 Replies)
Discussion started by: msb65
2 Replies

5. Shell Programming and Scripting

How to extract timestamp from the filename?

Hi., My file name is of the format: name_abc_20100531_142528.txt where., my timestamp is of the format: yyyymmdd_hhmmss How to extract the date strring and time string into seperate variables in the shell script, after reading the file as the input? I want to get the variables... (9 Replies)
Discussion started by: av_vinay
9 Replies

6. UNIX for Dummies Questions & Answers

Pattern matching and Printing Filename

Hi, My requirement is to search for a paritcular string from a group of .gz files and to print the lines containing that string and the name of the files in which that string is present. Daily 500 odd .gz files will be generated in a directory(directory name will be in the form of... (4 Replies)
Discussion started by: krao
4 Replies

7. Shell Programming and Scripting

Filename and Pattern matching

Hiiiii every one, I am facing a problem while giving a file name which has space in it. The problem is ... I have to read a file where the set of input files are stored. Now getting that name I have to open the file and i have to extract the name of the user from where it is written like"by... (2 Replies)
Discussion started by: kheyali Mitra
2 Replies

8. UNIX for Dummies Questions & Answers

extract only filename

Hi, I want to display only the file names of the ls command with a delimiter of comma. For example:- ls /etc/dir/N* gives the following output /etc/dir/N1 /etc/dir/N2 /etc/dir/N3 I want the output as N1,N2,N3,-->Here i need the final comma also. Also, here /etc/dir is being... (2 Replies)
Discussion started by: dnat
2 Replies

9. Shell Programming and Scripting

Returning filename and matching lines

What I'm trying to do is to search through a list of files, and output the filename, followed by the lines that matched the pattern. I'm matching the string "letters.moreletters" in any one of searched files, and the output I'm trying to get is: program_1.txt 10 dsdsd sdsd dsd... (2 Replies)
Discussion started by: smb_uk
2 Replies

10. Shell Programming and Scripting

Grep all files matching partial filename

What would be the easiest way to grep all files within a particular directory that match a partial filename? For example, searching all files that begin with "filename.txt" and are appended with the date they were created. I am using Ksh 88, btw. (3 Replies)
Discussion started by: mharley
3 Replies
Login or Register to Ask a Question