Varying number of awk search strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Varying number of awk search strings
# 1  
Old 11-14-2008
Varying number of awk search strings

I've created an awk script that handles a varying number of search strings handed to it as command line parameters ($1 $2 etc). There may be 1, or 2 or 3 or more. A simplified version of the script is:

Code:
awk -v TYP="$1 $2 $3 $4 $5 $6" '
  BEGIN {
       CTYP = split (TYP,TYPP," ")
  }
   {for ( i=1; i <= CTYP; i++ ) {
           if ($0 ~ TYPP[i]) {print; break}
      }}
 ' file1 > file2
exit 0

This works, but is not efficient at all.
Could someone suggested an alternate method to handle this scenario? Any help is appreciated.

Thank you.
# 2  
Old 11-14-2008
Code:
#!/bin/ksh
set -A arr $@
cnt=0
while [[ cnt -lt ${#arr[*]} ]]
do
      echo ${arr[cnt]} 
      cnt=$$(( cnt + 1))
done > tmp.tmp
grep -f tmp.tmp mybigfile

# 3  
Old 11-24-2008
Thanks Jim, that makes sense using grep, but I should have been more clear, it needs to be awk because there is more to my script, but I removed it to simplify my post here. I use awk here because I extract other info related to the hits from the file as well.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to give multiple choices and a varying number of answers

Hello everybody, I use `case' quite a lot but , excellent as it is , it only gives one final result ; can anyone suggest a way whereas , say long lists of choices are given and I , or a user could select either one two or any number of results to be echoed . many thanks in... (3 Replies)
Discussion started by: V686
3 Replies

2. Shell Programming and Scripting

Needed value after the last delimeter in a file with varying number of delimited columns

Hi All, My file has the records as below: aaa\bbb\c\dd\ee\ff\gg zz\vv\ww pp\oo\ii\uu How can I get the value after the last delimeter. My o/p: gg ww uu Thanks in Advance, (5 Replies)
Discussion started by: HemaV
5 Replies

3. Shell Programming and Scripting

awk search an output string between two strings

I would like to search for strings stored in searchstringfile.txt in inputfiles. searchstringfile.txt J./F. Gls. Wal F. Towerinput1.txt What is needed is J./F. 12 var Gls. Wal 16 interp. Tower 12 input2.txt Awk shall search for F. 16 pt. J./F. 22 output.txt input1.txt J./F. = 12 var... (3 Replies)
Discussion started by: sdf
3 Replies

4. Shell Programming and Scripting

Compare strings between 2 arrays and print number in AWK

Hi to everyone, Please some help over here. Hi have array a with 6 elements and array b with 3 elements as shown inside BEGIN{} statement. I need help to get the correct sintax (the part in red) to compare if string from array b is in array a and print the number related for each match.... (3 Replies)
Discussion started by: Ophiuchus
3 Replies

5. Shell Programming and Scripting

awk search strings from array in textfile

I am wanting to take a list of strings and loop through a list of textfiles to find matches. Preferably with awk and parsing the search strings into an array. // Search_strings.txt tag string dummy stuff things // List of files to search in textfile1.txt textfile2.txt The... (6 Replies)
Discussion started by: sdf
6 Replies

6. Shell Programming and Scripting

awk how to search strings within a file from two different lines

Hi, i would really appreciate any help anyone can give with the following info. Thanks in advance. I need to run a search on a file that contains thousands of trades, each trade is added into the file in blocks of 25 lines. i know the search has to take place between a time stamp specified... (4 Replies)
Discussion started by: sp3arsy
4 Replies

7. Shell Programming and Scripting

Using Awk to Search Two Strings on One Line

If i wanted to search for two strings that are on lines in the log, how do I do it? The following code searches for just one string that is one one line. awk '/^/ {split($2,s,",");a=$1 FS s} /failure agaf@fafa/ {b=a} END{print b}' urfile What if I wanted to search for "failure agaf@fafa"... (3 Replies)
Discussion started by: SkySmart
3 Replies

8. Shell Programming and Scripting

Awk search for a element in the list of strings

Hi, how do I match a particular element in a list and replace it with blank? awk 'sub///' $FILE list="AL, AK, AZ, AR, CA, CO, CT, DE, FL, GA, HI, ID, IL, IN, IA, KS, KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC, ND, OH, OK, OR, PA, RI, SC, SD, TN, TX, UT, VT, VA, WA,... (2 Replies)
Discussion started by: grossgermany
2 Replies

9. Shell Programming and Scripting

Parse apart strings of comma separated data with varying number of fields

I have a situation where I am reading a text file line-by-line. Those lines of data contain comma separated fields of data. However, each line can vary in the number of fields it can contain. What I need to do is parse apart each line and write each field of data found (left to right) into a file.... (7 Replies)
Discussion started by: 2reperry
7 Replies

10. Shell Programming and Scripting

awk search for Quoted strings (')

Hi All, I have files: 1. abc.sql 'This is a sample file for testing' This does not have quotations this also does not have quotations. and this 'has quotations'. here I need to list the hard coded strings 'This is a sample file for testing' and 'has quotations'. So i have... (13 Replies)
Discussion started by: kprattip
13 Replies
Login or Register to Ask a Question