Searching a particular string with spaces in a data file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching a particular string with spaces in a data file
# 1  
Old 06-19-2009
Searching a particular string with spaces in a data file

Hi,

I'm new to shell scripting and require your help in achieving the requirement.

I have a data file which stores organization name as one of the column data in a csv data file.

Organization name stored in data file is: Canadian OU CAD

Sample Data file:

1,5,4,5,,
,,,,Canadian OU CAD,Canada
,,,,Canadian OU CAD,Australia
,,,,Canadian OU CAD,Japan


I need to know the count of the occurences of the name Canadian OU CAD from the data file.

I'm getting the organization name that needs to be searched as a parameter to the shell script. Below is the code that i have written:

organization_name=$1

echo "org name = " $organization_name

c=`echo $organization_name | sed 's/^/"/' | sed 's/$/"/'`
echo " c = " $c

b=`grep -c ${c} $HOME/TESTING5.csv`

echo $b


When i try to run the above script it throws error:

org name = Canadian OU CAD
c = "Canadian OU CAD"
grep: can't open Canadian
grep: can't open OU
grep: can't open CAD"


In the above case, i expect the output to be 3 instead of the error.
# 2  
Old 06-19-2009
Quote:
Originally Posted by andy4013
Hi,

I'm new to shell scripting and require your help in achieving the requirement.

I have a data file which stores organization name as one of the column data in a csv data file.

Organization name stored in data file is: Canadian OU CAD

Sample Data file:

1,5,4,5,,
,,,,Canadian OU CAD,Canada
,,,,Canadian OU CAD,Australia
,,,,Canadian OU CAD,Japan


I need to know the count of the occurences of the name Canadian OU CAD from the data file.

I'm getting the organization name that needs to be searched as a parameter to the shell script. Below is the code that i have written:

organization_name=$1

echo "org name = " $organization_name

c=`echo $organization_name | sed 's/^/"/' | sed 's/$/"/'`
echo " c = " $c

b=`grep -c ${c} $HOME/TESTING5.csv`

echo $b


When i try to run the above script it throws error:

org name = Canadian OU CAD
c = "Canadian OU CAD"
grep: can't open Canadian
grep: can't open OU
grep: can't open CAD"


In the above case, i expect the output to be 3 instead of the error.
The bold is wrong
Code:
b=`grep -c \"${c}\" $HOME/TESTING5.csv`

I think it should work.
# 3  
Old 06-19-2009
Code:
an easier way

awk -F"," '{a[$4]++ } END{for (i in a) {print i,a[i]}}'

you can use this code to count anything .
you just need to know the field number of the string that you want to count.. in this case it is field number 4
BR

# 4  
Old 06-19-2009
Hi,

I corrected the syntax to b=`grep -c \"${c}\" $HOME/TESTING5.csv`

but still the same issue. I'm still getting the below error while trying to execute the script.

org name = Canadian OU CAD
c = "Canadian OU CAD"
grep: can't open Canadian
grep: can't open OU
grep: can't open CAD""
/export/home/appwdev/TESTING5.csv:0
# 5  
Old 06-19-2009
Code:
string="Canadian OU CAD"
awk -v v="$string" 'BEGIN{FS=","}$5 ~ v{ d++}END{ print "total: "d}' file

# 6  
Old 06-19-2009
Quote:
Originally Posted by andy4013
Hi,

I corrected the syntax to b=`grep -c \"${c}\" $HOME/TESTING5.csv`

but still the same issue. I'm still getting the below error while trying to execute the script.

org name = Canadian OU CAD
c = "Canadian OU CAD"
grep: can't open Canadian
grep: can't open OU
grep: can't open CAD""
/export/home/appwdev/TESTING5.csv:0
Absolutely my mistake...
it should be '${c}'
# 7  
Old 06-19-2009
Now it returns 0 count after modifying the syntax to '${c}'

org name = Canadian OU CAD
c = "Canadian OU CAD"
0
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching a string in a particular file name

Hello, I have a file name like FIRST_DPF_DAILY_CUST_0826152322.txt i need to extract the string after the third "_" underscore upto timestamp ends i.e CUST_0826152322 can anyone help me with the code Thank you! Regards Srikanth Sagi (3 Replies)
Discussion started by: srikanth_sagi
3 Replies

2. Shell Programming and Scripting

data searching and pasting with in the file

Hi All, I have .csv file in which I am trying to manipulate a column date, I started with awk but i am not sure how to do the below logic in . the file has a 23 columns and in the first row if the value is Trend and in the second column the value is Analysis then the program has to... (3 Replies)
Discussion started by: shruthidwh
3 Replies

3. Shell Programming and Scripting

Searching for a particular string and modifying text within block of data

Hi Forum. Is there a quick way to do the following search/replace within a block of data? I tried to google the solution but didn't really know what to look for. I have the following text file (I want to search for a particular string "s_m_f_acct_txn_daily_a1" and replace the... (5 Replies)
Discussion started by: pchang
5 Replies

4. Shell Programming and Scripting

searching each file for a string

Hi Guys... I want to search for each file that contains a particular string. e.g find . -print | xargs grep -i string_name Now my issue is the files that I search in are gzipped. Will I be able to find the string, using the above commands, even if the files are gzipped? Please... (2 Replies)
Discussion started by: Phuti
2 Replies

5. Shell Programming and Scripting

Searching data files for another file of values

I've used awk for some simple scripting, but having trouble figuring out how to search a couple of data files that have Name/Address/Zip Codes from another file that has list of only Zip Codes, and write out the lines that matched. Zip code field in the data file is 27 I was thinking... (5 Replies)
Discussion started by: matkins99
5 Replies

6. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

7. Shell Programming and Scripting

Searching a string in a file

Hi, I am new to unix shell scripting. I have a requirement. Could anyone help me writing the script for the same? Here goes the requirement: I have a config file let's say temp.config. Here is the data in the config file temp.config : ------------- name=victor age=42 state=texas... (5 Replies)
Discussion started by: badrimohanty
5 Replies

8. UNIX for Dummies Questions & Answers

searching for a string in a file

I need to search for a specific string in a file and if this string exist I need to replace it with something else. I am not sure how I could do this, using an if statement. (2 Replies)
Discussion started by: ROOZ
2 Replies

9. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

10. UNIX for Dummies Questions & Answers

searching for a string though file system

Is there a way to search an entire file system for the occurance of a string..... other than grep. I have a large directory structure and I'm not certain that grep <string> */*/*/*... is all that effective - especially as I can't be sure of the number of levels to go down without heaps of... (3 Replies)
Discussion started by: peter.herlihy
3 Replies
Login or Register to Ask a Question