Search pattern in a file taking input from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search pattern in a file taking input from another file
# 1  
Old 08-26-2016
Search pattern in a file taking input from another file

Hi,

Below is my requirement

Code:
File1:
svasjsdhvassdvasdhhgvasddhvasdhasdjhvasdjsahvasdjvdasjdvvsadjhv
vdjvsdjasvdasdjbasdjbasdjhasbdasjhdbjheasbdasjdsajhbjasbjasbhddjb
svfsdhgvfdshgvfsdhfvsdadhfvsajhvasjdhvsajhdvsadjvhasjhdvjhsadjahs

Code:
File2:
sdh
hgv

I need a command such that it should fetch the rows from file2 and search for matching rows [character 6 to 8th] in file1 and print the matching lines in output_file1.txt and non-matching lines in output_file2.txt.

For example, pattern "sdh" from file2 match with row (6th to 8th character) in file1 hence the row from file1 should be printed to output_file1.txt and 2nd row from file1 does not match with any pattern in file2 hence it should be printed to output_file2.txt.

I have cut the lines from file1 by using cut command as follows by could not reach the desired solution. Please Help!

Code:
cut -c6-8 file1
sdh
dja
hgv

Thanks,
Imran.

Last edited by vbe; 08-26-2016 at 08:16 AM.. Reason: code tags please not ICODE
# 2  
Old 08-26-2016
cut will only give you the char defined by -c6-8 ...
First the search pattern is the command grep!

give it a try
# 3  
Old 08-26-2016
I am not sure on how to use output of a command as search pattern in grep command

cut -c6-8 file1, the output of this command with be my search pattern to search file2.

I am not sure on how to use output of a command as search pattern in grep command.
# 4  
Old 08-26-2016
Sorry I did see properly your request: Matching 3 charaters on position 6-8 against a file containing the 3 char patterns...
more complex... looks like we need some awk...
Unfortunately I am at a remote office with a PC with no access (yet...) to unix boxes ( I am here for that...) other will have help you through as I have no ways of testing what I would suggest...
# 5  
Old 08-26-2016
Try
Code:
awk 'FNR==NR {T[$1]; next} !(substr($0, 6, 3) in T) {print > "output_file2.txt"; next} 1' file2 file1

These 2 Users Gave Thanks to RudiC For This Post:
# 6  
Old 08-26-2016
Hello imrandec85,

Following may help you in same.
Code:
awk 'FNR==NR{A[$0];next} ((substr($0,6,3)) in A){print $0 > "output_file1.txt";delete A[substr($0,6,3)];next} !((substr($0,6,3)) in A){print > "output_file2.txt"}'   Input_file2   Input_file1

Where matching lines(with strings of Input_file2) will be stored into output_file1.txt and non-matching will be stored in output_file2.txt file.
EDIT: Adding a non-one liner form of solution as follows too.
Code:
awk 'FNR==NR{
                A[$0];
                next
            }
     ((substr($0,6,3)) in A){
                                print $0 > "output_file1.txt";
                                delete A[substr($0,6,3)];
                                next
                            }
    !((substr($0,6,3)) in A){
                                print > "output_file2.txt"
                            }
    '  Input_file2   Input_file1

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-26-2016 at 08:55 AM.. Reason: Adding a non-one liner form of solution too now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 08-26-2016
Thank you RudiC and R. Singh

Thank you RudiC and R. Singh.

Both of your solutions meet my requirement.

I am not very fond of unix commands.Could you please explain what is actually happening in awk command.

Code:
awk 'FNR==NR{A[$0];next} ((substr($0,6,3)) in A){print $0 > "output_file1.txt";delete A[substr($0,6,3)];next} !((substr($0,6,3)) in A){print > "output_file2.txt"}'   Input_file2 Input_file1

Thanks once again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reducing input file size after pattern search

I have a very large file with millions of entries identified by @M. I am using the following script to "extract" entries based on specific strings/patterns: #!/bin/bash if ] then file=$1 else echo "Input_file passed as an argument $1 is NOT found." exit; fi MID=(NULL "string-1"... (10 Replies)
Discussion started by: Xterra
10 Replies

2. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Search if file exists for a file pattern stored in array

Hi experts, I have two arrays one has the file paths to be searched in , and the other has the files to be serached.For eg searchfile.dat will have abc303 xyz123 i have to search for files that could be abc303*.dat or for that matter any extension . abc303*.dat.gz The following code... (2 Replies)
Discussion started by: 100bees
2 Replies

4. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

5. Shell Programming and Scripting

Installing a bin file by taking input from a properties file

I need to install a bin file in UNIX which requires user interaction for giving some information like user id , path, sid etc. All these information is stored in a properties file in the same location. So if i give ./file.bin -f propfile.properties will it install the file taking all the... (1 Reply)
Discussion started by: coolmohere
1 Replies

6. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

7. Shell Programming and Scripting

How to ftp multiple files by taking the file name from a input file.

Hi, I'm working on a script which has to copy multiple files from one server to another server. The list of files that are to be copied is present in a file say input.txt. vi input.txt abc.c welcome.c new.c welcome1.c for ftp'ing a single file say 'new.c' the following code... (2 Replies)
Discussion started by: i.srini89
2 Replies

8. Shell Programming and Scripting

Problem taking input from file with for loop

I am trying to take input from a file and direct it into a bash script. This script is meant to be a foreach loop. I would like the script to process each item in the list one by one and direct the output to a file. # cat 1loop #!/bin/bash # this 2>&1 to redirect STDERR & STDOUT to file... (4 Replies)
Discussion started by: bash_in_my_head
4 Replies

9. Shell Programming and Scripting

How to search a pattern inside a zipped file ie (.gz file) with out unzipping it

How to search a pattern inside a zipped file ie (.gz file) with out unzipping it? using grep command.. Bit urgent.. pls..help me (2 Replies)
Discussion started by: senraj01
2 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question