-1

Currently I have a folder with 4 files, one script.awk script file, the gawk.exe binary which runs the script and a report.csv file in which the script reading the lines from, and one names.txt file which contains usernames, one per line. Sometimes the names contain spaces, so occasionally they are actually two or more separate words, but there is strictly one username per line in the txt file. When I run my awk script, I store some of the data from the csv file in a variable called "name".

Now let's say name = "Pete", and I would like to check if whether the names.txt file contains the username Pete or not, it has to be exactly "Pete", not like "Pete Sampras" etc., and when a match was found, I would like to take further action obviously.

The txt file contains about 500 lines like these:

leopanato
colan321
kamon mdp
BELLAM42
sasieightynine
  • 434
  • 1
  • 5
  • 16

1 Answers1

2

Basic question

Read the names into an array, and use that:

gawk -f script.awk names.txt report.csv

and script.awk might contain:

FNR == NR { names[$0]++; next }
{
    …code to determine name…
    if (name in names)
    {
       …actions for matched name…
    }
}

The FNR == NR line processes the first file, reading the names from that file into the array called, imaginatively, names. The next means that the rest of the code is not processed while the first file (names.txt) is read.

One the code is reading the second file, FNR (file record number) no longer equals NR (overall record number), so the first line is skipped. The action processes the line from report.csv. You've not shown how you're handling the CSV material, which is fine — you say you load a name into name. The if statement checks to see whether the value in name is an index in the array names. If so, the appropriate actions are executed.

Extended question

You can look at the ARGV array and length(ARGV) and also at FILENAME to deduce what you are dealing with. Adapting the code:

BEGIN { if (length(ARGV) != 4) { printf "Usage: %s good.txt bad.txt records.csv\n", ARGV[0]; exit(1) } }
FILENAME == ARGV[1] { good[$0]++; next }
FILENAME == ARGV[2] { bad[$0]++; next }
{
    …code to determine name…
    if (name in good) { …actions for good names… }
    if (name in bad)  { …actions for bad  names… }
}

Note that this coding scheme allows the same name to be both good and bad at the same time. You could decide that people should be treated as good even if they're also listed as bad, or vice versa. You could even check that there are no duplicates between the good and bad lists if you wanted to.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • How many times are you going to change the question? Chameleon questions are unpopular. Ask what you want to know the first time. How are you going to know how many files to process as text files and how many to process as CSV files? What have you tried? What does your code look like? You probably need to look at the FILENAME variable, and maybe the ARGV array. – Jonathan Leffler Sep 11 '15 at 05:34
  • Suppose you have 3 text files. How are you going to identify which arrays each of those files should be read into? What are you going to be doing afterward? Are you going to know enough about the files to be able to use their names as part of the key to an array of names? You've not shown enough of what you're about — what you might need to do after you've read this data into your program — to make it easy for us to make intelligent guesses about what you need. Does the code need to adapt to the number of text files? How is it going to process file1 differently from file2 from file3? – Jonathan Leffler Sep 11 '15 at 05:47
  • To the extent you've described what you want, there is nothing so far that is undoable. But what you want is not sufficiently clear that more help can be given yet. – Jonathan Leffler Sep 11 '15 at 05:49
  • I've extended my answer. I suggest that you transfer the key information from your comments into the question, and then delete your comments above. Leave a message for me saying you've done so and I'll delete my comments, including this one. I'll flag your comment. – Jonathan Leffler Sep 11 '15 at 06:23
  • All right, I have done everything that I needed and it's working flawlessly, I have changed a few things but your code helped me a lot, thank you very much for your time, help and contribution, I appreciate it. – sasieightynine Sep 11 '15 at 07:13