Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

awk pattern match by looping through search patterns

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago
ananan ananan is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 12 July 2017, 2:38 AM EDT
Posts: 22
Thanks: 5
Thanked 0 Times in 0 Posts
awk pattern match by looping through search patterns

Hi
I am using Solaris 5.10 & ksh

Wanted to loop through a pattern file by reading it and passing it to the awk to match that value present in column 1 of rawdata.txt , if so print column 1 & 2 in to Avlblpatterns.txt. Using the following code but it seems some mistakes and it is running for more time and repeatedly printing the matching pattern.. I think like Cartesian loop. Please correct me where I am wrong. Input pattern 1000 lines and data file will be containing 20000 records.

Code:
 
while read line

do

 awk -F"," '{

 if ( $1 == "'$line'" ){print $1,$2}

}' rawdata.txt >> Avlblpatterns.txt

 done < searchpat.txt

---------- Post updated at 11:43 AM ---------- Previous update was at 11:36 AM ----------

Not shared sample files. Please use your own
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 25 July 2017, 1:22 AM EDT
Location: Aachen, Germany
Posts: 11,063
Thanks: 284
Thanked 3,408 Times in 3,138 Posts
Quote:
Originally Posted by ananan View Post
.
.
.
---------- Post updated at 11:43 AM ---------- Previous update was at 11:36 AM ----------

Not shared sample files. Please use your own
And how would you like you tea served, Sir?


Running awk a thousand times is far from being efficient. If you skim through these fora, you'll find many posts dealing with (and solving) exactly your or very similar problems.
The Following User Says Thank You to RudiC For This Useful Post:
RavinderSingh13 (1 Week Ago)
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago
rbatte1 rbatte1 is offline Forum Staff  
Root armed
 
Join Date: Jun 2007
Last Activity: 24 July 2017, 10:22 AM EDT
Location: Lancashire, UK
Posts: 3,168
Thanks: 1,334
Thanked 604 Times in 546 Posts
Although I agree that there are plenty of solutions already on the board, does your file searchpat.txt have a blank line in it or some other line that will match multiple lines?

Perhaps you can better manage your requirement with a grep and a cut. Look at the -f flag in the grep manual page to see if that helps.



Robin
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Egrep patterns in a file and limit number of matches to print for each pattern match ananan Shell Programming and Scripting 10 06-13-2017 01:09 AM
Awk to match a pattern and perform a search after the first pattern RickCharles Shell Programming and Scripting 8 09-06-2012 11:21 PM
Need one liner to search pattern and print everything expect 6 lines from where pattern match made chidori Shell Programming and Scripting 8 03-15-2012 11:46 AM
Perl: Match a line with multiple search patterns Juha Shell Programming and Scripting 10 04-09-2008 01:43 AM
List files that do not match the search pattern olapxpert UNIX for Dummies Questions & Answers 7 04-14-2005 03:49 PM



All times are GMT -4. The time now is 02:34 AM.