To search multiple string in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To search multiple string in file
# 1  
Old 01-09-2014
To search multiple string in file

hi ,

i am having a file where i need to take ignore the data from file1.txt and redirect to another file

for eg:

Code:
 
file1.txt
AUS
USA
 
file2.txt
AUS,123
NZ,11
USA,12

i am using the below code

Code:
for i in file1.txt
do
grep -v ^$i file2.txt>out.txt
done

i want to check in grep OR condition from file1.txt with file2.txt
# 2  
Old 01-09-2014
Doesn't
Code:
grep -vf file1.txt file2.txt >out.txt

give the wanted result ?

(check the options available depending on your OS implementation of grep)

Last edited by ctsgnb; 01-09-2014 at 06:53 AM.. Reason: switched file1 & file2 ...and f instead of F
# 3  
Old 01-09-2014
Quote:
Originally Posted by rohit_shinez
hi ,

i am having a file where i need to take ignore the data from file1.txt and redirect to another file

for eg:

Code:
 
file1.txt
AUS
USA
 
file2.txt
AUS,123
NZ,11
USA,12

i am using the below code

Code:
for i in file1.txt
do
grep -v ^$i file2.txt>out.txt
done

i want to check in grep OR condition from file1.txt with file2.txt

u can do it this way

Code:
cat file1.txt|while read a
do
grep -v ^$i file2.txt > out.txt
done

# 4  
Old 01-09-2014
Quote:
Originally Posted by zozoo
u can do it this way

Code:
cat file1.txt|while read a
do
grep -v ^$i file2.txt > out.txt
done

I would not recommend this because this solution :
- do a useless use of cat (UUOC)
- and may lead to duplicate entries since it loops many times on file2.

You have to consider whether it can happen in fil1 that some entries are substring of other entries and may potentially match line that are in file2 but that we don't want to filter out.

Consider for example :

file1 :
Code:
US
AUS

file2 :
Code:
AUS,123
NZ,11
US,13
USA,12

then something like this may works:
Code:
$ sed 's/.*/^&,/' f1 >f1.tmp ; grep -vEf f1.tmp f2 ; rm -f f1.tmp
NZ,11
USA,12

of course it's just an example. It depends on what you may have as input and what you want as output.

But can it happend that the pattern appear somewhere else than at the beginning of the line ?

Last edited by ctsgnb; 01-09-2014 at 08:03 AM..
This User Gave Thanks to ctsgnb For This Post:
# 5  
Old 01-10-2014
Hi

yes the patten may appear at any of the file but i need to check the first string to be checked only and extact string from file1 needs to be matched
what i actually needed is

input file

Code:
AUS,123
NZ,11
US,13
USA,12
USA,12,AUS
AUSA,11,12

output file

Code:
NZ,11
USA,12
USA,12,AUS
AUSA,11,12


Last edited by rohit_shinez; 01-11-2014 at 11:00 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Shell Programming and Scripting

Command to Search a FILE for a STRING in multiple DIRECTORIES

Hi, Can someone please help me with a Command to Search a FILE for a STRING in multiple DIRECTORIES. I am searching for the VIP in HTTPD.CONF in multiple httpd instances. I am using find ./ -name "httpd.conf" | xargs grep 10.22.0.141 cut -d: -f3- | cut -d ' ' -f4 | sort | uniq -c ... (1 Reply)
Discussion started by: crosairs
1 Replies

3. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

4. Shell Programming and Scripting

Multiple search strings replaced with single string

Hi, I need someone's help in writing correct perl code. I implemented following code for "multiple search strings replaced with single string". ========================================================= #!/usr/bin/perl my $searchStr = 'register_inst\.write_t\(' |... (2 Replies)
Discussion started by: chettyravi
2 Replies

5. Shell Programming and Scripting

String search and replace in multiple files.

Hello. I have five config files in /etc that I want to edit in one click for testing. I would like to make a script like this : #!/bin/bash # a_file="/etc/file_1" src_str="src_string_1" rpl_str="rpl_string_1" calling_sed_or_awk_or_whatelse $a_file search_for_all $src_str replace_with... (4 Replies)
Discussion started by: jcdole
4 Replies

6. Shell Programming and Scripting

Search multiple strings on a file and copy the string next to it

I tried awk for this, but failed <or my code is not correct? I dont know>. Can anyone help me on this? ---------- Post updated at 08:34 PM ---------- Previous update was at 08:29 PM ---------- my working file looks like this: <empty> <empty> <empty> NAME :ABC AGE :15 GENDER... (6 Replies)
Discussion started by: kingpeejay
6 Replies

7. Shell Programming and Scripting

Multiple search string in multiple files using awk

Hi, filenames: contains name of list of files to search in. placelist contains the names of places to be searched in all files in "filenames" for i in $(<filenames) do egrep -f placelist $i if ] then echo $i fi done >> outputfile Output i am getting: (0 Replies)
Discussion started by: pinnacle
0 Replies

8. Shell Programming and Scripting

search for a string spread in multiple line

Hi, I have to look for the full string below in a large file(>5GB) , but that string may be spread in two lines. string :-- "f8f8 f5f0 f0f0 f1f7 f4f7 f7c1" By grep "f8f8 f5f0 f0f0 f1f7 f4f7 f7c1" file | tail -1 , I am getting incorrect result because there is a line after this also which... (1 Reply)
Discussion started by: finder255
1 Replies

9. UNIX for Dummies Questions & Answers

problem with grep on search string in a txt file over multiple files

I have a couple of things I got stuck on 1) I have a text file containing 25k search string that I need to search against compressed file. I have used this command but somehow it doesn't seems to use all the search terms. I have put one search string per line in the txt file (I clean up... (2 Replies)
Discussion started by: m00
2 Replies

10. UNIX for Dummies Questions & Answers

string search in folders with particular multiple file extension

Hi, I am newbie in UNIX so please excuse for my questions. Is there a a way to search for string in files within folder and sub folder in particluar file extensions. Ex. search for ABC in folder 'A'(including it's sub folders) in html, xml files. Thanks, Ani (2 Replies)
Discussion started by: anikanch
2 Replies
Login or Register to Ask a Question