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
# 8  
Old 08-26-2016
Hello imrandec85,

Following may help you in same.
Code:
awk 'FNR==NR{                   #### FNR and NR are the awk's built-in variables, so FNR will be representing the current Input_file's line number same as NR, only deference between them is FNR will be RESET on each Input_file's starting and NR will keep increasing till the last Input_file is completely read. So FNR==NR condition will be TRUE when 1st Input_file1 will be read  not the second one.
A[$0];                          #### Creating an array named A whose index is $0(complete line) of Input_file2.
next}                           #### next is awk's built-in keyword which will be used to skip all further statements. So here it will skip all next mentioned statements.
((substr($0,6,3)) in A)         #### substr is again awk's built in keyword which is mostly used to get specific string out of a line, it's syntax is substr(line/variable's value, starting position number, Number of characters you need to get from the starting position mentioned). So here as per your requirement we have to get 6,7,8 characters in Input_file2 so mentioning ($0,6,3) means complete line's 6th character to 3 characters take and then check if they are present in array A(which we created while Input_file2 was getting read).
{                               #### If that substring is present in array A then execute following statements.
print $0 > "output_file1.txt";  #### print the complete line($0) to file named "output_file1.txt" as per your requirement.
delete A[substr($0,6,3)];       #### Deleting the array's element to remove duplication here.
next}                           #### Using next keyword here to skip all further statements here.
!((substr($0,6,3)) in A)        #### Now checking substring for characters 6,7,8 which are NOT present in array A(means to get lines which are present in Input_file and not in Input_file2)
{print > "output_file2.txt"}    #### printing those lines into file named "output_file2.txt" as per your requirements.
'   Input_file2   Input_file1   #### Mentioning Input_file1 and Input_file2 here.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 9  
Old 08-26-2016
Should the patterns from file2 be recognized more than once? Then: do not delete A[...]!
This User Gave Thanks to RudiC For This Post:
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