awk search strings from array in textfile


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk search strings from array in textfile
# 1  
Old 10-14-2011
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.

Code:
// Search_strings.txt

tag
string
dummy
stuff
things

// List of files to search in

textfile1.txt
textfile2.txt

The desired output should look like this:

Code:
{print $1, searchstring, Fieldnumber_searchstring_found}

Can anybody help
# 2  
Old 10-14-2011
What have you tried?
# 3  
Old 10-14-2011
Tried to pass the search_string.txt into an array. Count the rows in the array and loop with the FOR syntax through each file. But I get messed up with the code.
# 4  
Old 10-14-2011
Code:
$ cat array.awk

BEGIN { while(getline < "Search_strings.txt") A[$1]=1; }

{
        for(N=1; N<=NF; N++)
        if(A[$N])
                printf("%s, %s, %d\n", $1, $N, N);
}

$ xargs awk -f array.awk < list_of_files.txt

# 5  
Old 10-14-2011
Since I need to use gawk on windows I rewrote the script to:

Code:
-v FILE="Search_strings.txt" "BEGIN { while(getline < FILE) A[$1]=1; }{for(N=1; N<=NF; N++) if(A[$N]) print( $1, $N, N);}" textfiles*.txt > Concordance.txt

Though, the script seems not to end and the filesize remains 0 bytes.Smilie
# 6  
Old 10-14-2011
That would have been nice to know.

Unfortunately windows CMD does not expand * at all. Any glob-like expansion is done inside programs, not CMD, meaning it's an entirely optional feature which most commands don't support, and those which do often don't do it the same way as others.

---------- Post updated at 03:11 PM ---------- Previous update was at 03:07 PM ----------

Also, all your datafiles are going to be full of garbage carriage returns.

---------- Post updated at 03:16 PM ---------- Previous update was at 03:11 PM ----------

CMD doesn't have real quoting, either. Strings and arguments and quotes all get passed as-is. Yes, all quoting in CMD is an optional feature. Smilie Try and avoid spaces in filenames.

I got this to work:

Code:
> gawk -v FILE="strings.txt" "BEGIN { while(getline < FILE) A[$1]=1; }{for(N=1; N<=NF; N++) if(A[$N]) print( FILENAME, $1, $N, N); }" data*.txt
data1.txt string string 1
data1.txt string string 1
data1.txt stuff stuff 1
data2.txt stuff stuff 1
>

I added FILENAME to make sure it was opening all the files correctly. It's a built-in variable.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 10-14-2011
@ corona688

Thanks this works the way I need it!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Assign zero to strings that don't appear in block, store result in AWK array

Hi to all, I have this input: <group> <x "2">Group D</x> <x "3">Group B</x> <x "1">Group A</x> </group> <group> <x "1">Group E</x> <x "0">Group B</x> <x "1">Group C</x> </group> <group> ... (11 Replies)
Discussion started by: Ophiuchus
11 Replies

3. Shell Programming and Scripting

Search strings from array in second file

I have a file search_strings.txt filled with search strings which have a blank in between and look like this: S. g. Erh. o. J. v. d. Chijs g. Ehr.I would like to search the strings in the second given Textfile.txt and it shall return the column number. Can anybody help with the correct... (3 Replies)
Discussion started by: sdf
3 Replies

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

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

6. Shell Programming and Scripting

manipulate the textfile having strings

i have text files named like vho1.txt, vho2.txt... vho15.txt.In all there are 15 in number. each file has strings like \Movies On Demand\Action & Adventure|X-Men: Wolverine|2009-09-29 00:01:00|2010-03-24 23:59:00|Active|3 \Movies On Demand\Free\Free1|My movie 14|2010-01-11 00:00:00|2010-03-01... (5 Replies)
Discussion started by: ramse8pc
5 Replies

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

8. Shell Programming and Scripting

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: awk -v TYP="$1 $2 $3 $4 $5 $6" ' BEGIN { CTYP = split (TYP,TYPP," ") } ... (2 Replies)
Discussion started by: CarlosNC
2 Replies

9. Shell Programming and Scripting

Return an array of strings from user defined function in awk

Hello Friends, Is it possible to return an array from a user defined function in awk ? example: gawk ' BEGIN{} { catch_line = my_function(i) print catch_line print catch_line print catch_line } function my_function(i) { print "echo" line= "awk" line= "gawk"... (2 Replies)
Discussion started by: user_prady
2 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