Reading the data from CSV and performing search through shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading the data from CSV and performing search through shell script
# 1  
Old 07-28-2011
Reading the data from CSV and performing search through shell script

Hello,

I am working on building a script that does the below actions together in my Linux server.

1) First, have to read the list of strings mentioned in CSV and store it in the shell script

2) Second, pick one by one from the string list, and search a particular folder for files that contains that particular string.

3) Copy those files to another folder and rename them, ending with the string used for search inorder to identify.

CSV file format
String1
String2
String3
.
.
.
.
etc

I have listed below the script that i have come up so far and need to make it dynamic.

Script

Code:
#!/bin/sh

source_folder="/home/search/files/"
target_folder="/home/search/result/"
search_string="String1"
find $source_folder -exec grep -l $search_string {} \; -exec cp {} $target_folder \;

Any help in regard to this is much appreciated. Thanks in advance.

Last edited by pludi; 07-29-2011 at 03:14 AM..
# 2  
Old 07-29-2011
How about something like this:

Code:
SRC=/home/search/files/
DEST=/home/search/result/
find $SRC -print | while read file
do
     DPATH=`echo $file | sed "s:$SRC:$DEST:"`
     DDIR=`dirname $DPATH`
     while read string
     do
         grep -qi $string $file && {
             mkdir -p $DDIR
             echo Copying $file to ${DPATH}_${string}
             cp -p $file ${DPATH}_${string}
         }
     done < /home/search/search.csv
done

# 3  
Old 07-29-2011
This works great.

But one more thing i missed to mention it clearly in my earlier post, the file name has to be renamed as below,

actual file name = file1.txt
search string = string1

The file should be renamed as file1_string1.txt

Could you please help with this.

Last edited by vikrams; 07-29-2011 at 12:41 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script for .Txt to .csv conversion with data processing

Hi experts, I want to convert a txt file having rows and columns (CNAI_DUMP_raw.txt) by comparing it with another text file (paramaters.txt) and generate a output in CSV which contains only 3rd column from CNAI_DUMP_raw.txt, and the columns mentioned in parameters.txt. FYI: There are two... (16 Replies)
Discussion started by: Gautam Banerjee
16 Replies

2. Shell Programming and Scripting

Shell script to extract data from csv file

Hi everyone, I have a csv file which has data with different heading and column names as below. Static Data Ingested ,,,,,,,,,,,,Known Explained Rejections Column_1,column_2,Column_3,Column_4,,Column_6,Column_7,,% Column_8,,Column_9 ,Column_10 ,... (14 Replies)
Discussion started by: Vivekit82
14 Replies

3. UNIX for Dummies Questions & Answers

Shell script to extract data from csv file

Hi Guys, I am new to shell script.I need your help to write a shell script. I need to write a shell script to extract data from a .csv file where columns are ',' separated. The file has 7 columns having values say column 1,column 2.....column 7 as below along with their values. Name, Address,... (7 Replies)
Discussion started by: Vivekit82
7 Replies

4. Shell Programming and Scripting

Reading a csv file using shell script

Hello All, I have a csv file that looks like below ProdId_A,3.3.3,some text,some/text,sometext_1.2.3 ProdId_B,3.3.3,some text,some/text,sometext_1.2.3 ProdId_C,3.3.3,some text,some/text,sometext_1.2.3 ProdId_A,6.6.6,some text,some/text,sometext_9.9.9 I will get ProdId from... (5 Replies)
Discussion started by: anand.shah
5 Replies

5. Shell Programming and Scripting

Exporting data as a CSV file from Unix shell script

Friends...This is the first time i am trying the report generation using shell script... any suggestions are welcome. Is there a way to set the font size & color when i am exporting the data from unix shell script as a CSV file ? The following sample data is saved as a .csv file in the... (2 Replies)
Discussion started by: appu2176
2 Replies

6. UNIX for Advanced & Expert Users

shell script to format .CSV data

Hi all, I have written a shell script to search a specified directory (e.g. /home/user) for a list of specific words (shown as ${TMPDIR}/wordlist below). The script works well enough, but I was wondering if there was a way to display the line number that the word is found on? Thanks! cat... (1 Reply)
Discussion started by: tmcmurtr
1 Replies

7. Shell Programming and Scripting

Reading data from a file through shell script

There is one Text file data.txt. Data within this file looks like: a.sql b.sql c.sql d.sql ..... ..... want to write a shell script which will access these values within a loop, access one value at a time and store into a variable. can anyone plz help me. (2 Replies)
Discussion started by: Dip
2 Replies

8. Shell Programming and Scripting

Shell script to format a .CSV data

Hi There I needed to write a Unix shell script which will pick up the data from a .CSV file and reformat it as per the requirement and write it to another .CSV file. Currently I am in the proess of Data Import to "Remedy System" (A one kind of incident mangement Application) and this... (8 Replies)
Discussion started by: Uday1982
8 Replies

9. Shell Programming and Scripting

reading data from excel using shell script

Hi all I am new to shell scripting. I need to write a shell script that reads each row of an USER_ID colume in a excel file. the excel has around 10000 rows of data. Can someone gives me some example or advice what's best way to do this thanks (11 Replies)
Discussion started by: tiger99
11 Replies
Login or Register to Ask a Question