Search list of files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search list of files
# 1  
Old 12-11-2013
Search list of files

Hi All,

I Have 2 files.

file1:

Code:
abc.xml.gz
abcd.xml.gz
xyz.xml.gz
X.xml.gz

File2

Code:
abc.
x.
cde.
unix.

My requirment is search File1 using File2 and need below ouput.

Code:
abc.xml.gz
X.xml.gz


Last edited by Don Cragun; 12-11-2013 at 10:20 PM.. Reason: Add CODE tags.
# 2  
Old 12-12-2013
is this what you require ?

Code:
while read a
do
grep -i "$a" file1
done  < file2

# 3  
Old 12-12-2013
Quote:
Originally Posted by zozoo
is this what you require ?

Code:
while read a
do
grep -i "$a" file1
done  < file2

@zozoo small modification to your script

Code:
$ while read a; do a=$a"xml.gz"; grep -i $a file1; done  < file2
abc.xml.gz
X.xml.gz

awk you may try

Code:
$ awk 'FNR==NR{A[tolower($1"xml.gz")];next}(tolower($1) in A)' file2 file1
abc.xml.gz
X.xml.gz


Last edited by Akshay Hegde; 12-12-2013 at 08:15 AM..
# 4  
Old 12-12-2013
Or something like this?
Code:
awk -F\. 'NR==FNR{a[toupper($1)]; next} toupper($1) in a' file2 file1

# 5  
Old 12-12-2013
@ Franklin52 yes., it works but if extension is other than .xml.gz or file without extension with same base name also will be printed.
# 6  
Old 12-12-2013
How about
Code:
grep -iFf file2 file1
abc.xml.gz
X.xml.gz

?
# 7  
Old 12-12-2013
Quote:
Originally Posted by zozoo
is this what you require ?

Code:
while read a
do
grep -i "$a" file1
done  < file2

Without an anchor "^$a" or "$a"xml.gz one can keep it simple
Code:
fgrep -if file2 file1

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Take a list if strings from a file and search them in a list of files and report them

I have a file 1.txt with the below contents. -----cat 1.txt----- 1234 5678 1256 1234 1247 ------------------- I have 3 more files in a folder -----ls -lrt------- A1.txt A2.txt A3.txt ------------------- The contents of those three files are similar format with different data values... (8 Replies)
Discussion started by: realspirituals
8 Replies

3. Shell Programming and Scripting

search from a list of words

Hello, I'm trying to write a bash script that will search for words from one list that may be found in another list. Once the record is found, it will create a new text file for each word. For example, list1.txt contains the following: Dog Cat Fish List2.txt contains Dog - Buddy 14... (3 Replies)
Discussion started by: jl487
3 Replies

4. SuSE

Search all files based on first and in all listed files search the second patterns

Hello Linux Masters, I am not a linux expert therefore i need help from linux gurus. Well i have a requirement where i need to search all files based on first patterns and after seraching all files then serach second pattern in all files which i have extracted based on first pattern.... (1 Reply)
Discussion started by: Black-Linux
1 Replies

5. UNIX for Dummies Questions & Answers

script to search patterns inside list of files

>testfile while read x do if then echo $x >> testfile else fi if then echo $x >> testfile else fi done < list_of_files is there any efficient way to search abc.dml and xyz.dml ? (2 Replies)
Discussion started by: dr46014
2 Replies

6. UNIX for Advanced & Expert Users

Search files with specfic extention and later search content

Hi, I would appriciate if somebody can help me figure out how to search for all the *.xml file under a specific directory and subdirectroies (/home/username) and later search of content "<start>" inside the xml file returned by search. -Lovin.V (2 Replies)
Discussion started by: lovi_v
2 Replies

7. UNIX for Advanced & Expert Users

Search a list of lines in file into files

I have a file (file1) which is like host1 host2 host3 host4 the list goes on............ Now I want the above lines in files to be compared with files under /opt/new/ File names are as below: Dev Prod QA And suppose host1 from file1 is found under Dev(file under /opt/new)... (2 Replies)
Discussion started by: sriram003
2 Replies

8. UNIX for Dummies Questions & Answers

Search a string from list of input files

Hello, I am trying to match a string between line one and line two with in the file. But I dont want to search based on the given filename. Instead I want to search for all available files in the specific directory. Please help me on the above. (2 Replies)
Discussion started by: sivakumarvenkat
2 Replies

9. UNIX for Dummies Questions & Answers

List files that do not match the search pattern

I need to list the files that do not match the search pattern: Example: cat file1 This is how it should work cat file2 This is why I like Unix grep -option? Unix * (or some other command) returns file1 (7 Replies)
Discussion started by: olapxpert
7 Replies

10. IP Networking

List files that do not match the search pattern

I need to list the files that do not match the search pattern: Example: cat file1 This is how it should work cat file2 This is why I like Unix grep -option? Unix * (or some other command) returns file1 (1 Reply)
Discussion started by: olapxpert
1 Replies
Login or Register to Ask a Question