Searching for strings amongst non-uniform data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching for strings amongst non-uniform data
# 1  
Old 12-13-2011
Searching for strings amongst non-uniform data

Hi Guys,

I have a source file which contains significant strings amongst a lot of dross in non-uniform format, I'd like to search the input file for any examples of data from my reference file, and then output any matches to a list (text file).

I've made something that achieves this, it's just very slow (The reference file contains hundreds of entries) - Is anyone able to suggest a faster / more efficient method of achieving the below output?

Many thanks,

Gary

Code:
#script.sh
cat referencefile.txt | while read HOST
do
check=`grep $HOST input.txt | wc -l`
if [  "$check" -ne 0 ]; then
echo $HOST >> output.txt
fi
done

referencefile.txt:
hostname1
hostname2
hostname3
hostname4
hostname5
hostname6
hostname7
hostname8
hostname9
hostname10

input.txt:
random text random text hostname1 random text
random text hostname3 random text
randomtexthostname6randomtext random text

output.txt:
hostname1
hostname3
hostname6

Last edited by gazza86; 12-13-2011 at 11:33 PM.. Reason: Post had wrapped funny for some reason.
# 2  
Old 12-13-2011
Try this...
Code:
grep -o -f referencefile.txt input.txt

--ahamed
# 3  
Old 12-13-2011
Sorry - Didn't work for me, this is what happened:

Code:
$ grep -o -f referencefile.txt input.txt
grep: illegal option -- o
grep: illegal option -- f
Usage: grep -hblcnsviw pattern file . . .

# 4  
Old 12-13-2011
Modified your code... might be a little faster...
Code:
while read HOST; do
  grep $HOST input.txt >/dev/null 2>&1 && echo $HOST >> output.txt
done < referencefile.txt

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 12-13-2011
That did seem a lot faster!
Thankyou! :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Can I combine below mentioned grep commands using OR (when searching strings having spaces)

Command 1: $script | grep 'Write to ECC( SSID=MARGIN)' Command 2: $script | grep 'is not greater than existing logical processing' The above commands run my script and search the mentioned strings but I do not want to run my script twice. It is increasing run time. Can someone tell me... (3 Replies)
Discussion started by: Tanu
3 Replies

2. Programming

Hsearch() problem when searching for strings.

I have written a code in C where the objective is to search for strings. There are two files: 1. Database file which has more than one billion entries. This file is read in argv in the C code below. The format of the file is like this: a.txt apple b.txt candle c.txt glue 2. There is another... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

3. Shell Programming and Scripting

Searching for a list of strings in a file with Python

Hi guys, I'm trying to search for several strings, which I have in a .txt file line by line, on another file. So the idea is, take input.txt and search for each line in that file in another file, let's call it rules.txt. So far, I've been able to do this, to search for individual strings: ... (1 Reply)
Discussion started by: starriol
1 Replies

4. Shell Programming and Scripting

Searching for string between two different strings

I want to the following results using unix script, can some one help ? Thanks Input: select col1, col2 from tab1 where ......... select col1,.....,coln from tab2, tab3 where.... select clo1,clo2,col3 from tab4 where... output (results) tab1 tab2 tab3 tab4 basically I need... (5 Replies)
Discussion started by: kanagalav
5 Replies

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

6. Shell Programming and Scripting

reformating non-uniform strings

I have a set of free-form phone numbers that are not uniform and I want to reformat them into a standard uniform string. These are embedded at the end of a colon seperated file built by a large nawk + tr piping like such: XXXXX:YYYYY:ZZZZZ:(333)333-3333x33333 XXXXX:YYYYY:ZZZZZ:x44444... (9 Replies)
Discussion started by: lordsmiter
9 Replies

7. Shell Programming and Scripting

Need help in searching 2 files for strings then perform an action

I have 2 files. I basically want to search both of them to see if the 1st column ($1) matches and if it matches then check to see if the 2nd column ($2) matches, then execute some code showing the results of the matches. File 1: AAA 123 misc blah BBB 456 CCC 789 File 2: ... (2 Replies)
Discussion started by: streetfighter2
2 Replies

8. Solaris

higlighting strings while searching

Hello experts, i am using sun solaris 9 i try to searching string from a file using more command. I wish when i search a string it will higlight the string/strings from the file. Have any idea how to do it..? I use putty. br//purple (9 Replies)
Discussion started by: thepurple
9 Replies

9. Shell Programming and Scripting

searching for filenames with search strings in another file

Hi, I have 5 files in a directory. emp1_usage.txt emp2_usage.txt emp3_usage.txt emp4_usage.txt emp5_usage.txt I am using sqlldr to get the contents of the above 5 files and store it in a temp table and update my original table using temp table. for f in *emp*.txt do sqlldr... (3 Replies)
Discussion started by: pathanjalireddy
3 Replies

10. UNIX for Dummies Questions & Answers

searching for strings/user IP addresses

Hi, I'm trying to write a script, which will perform the following actions. Pick up the IP address of the PC I have used to telnet into the SUN server. Export this. Run the rest of my script. I am struggling with the first part, I know the IP address can be displayed by the command... (2 Replies)
Discussion started by: 30694
2 Replies
Login or Register to Ask a Question