0

Trying to use awk to search a file for specific lines and writing "matched" after printing each line if it matches.

For example, if I have a file that contains a list of names and emails but I only want to match emails ending in "@yahoo.com" I want it to print out that line with "matched" at the end and if the line does NOT contain @yahoo.com, I just want it to print out that line and continue.

awk -F, '{if($3~/yahoo.com/){ print $1,$2,$3 " matched"}else{ print $1,$2,$3 }}' emails.txt

This returns:

Joe Smith [email protected] matched
John Doe [email protected] 
Sally Sue [email protected]`

So its only matching the first @yahoo.com, then printing the other lines off regardless of their email address. What am I missing?

2
  • 2
    When asking for help writing a script to parse the contents of a file, the most important thing is to show us the contents of the file. So far you've shown us the script you ran and the output it produced - add the input file that you ran it against so we can help you figure out where you went wrong with your script. Also add the expected output as it's not totally clear if you want the non-matching lines printed or not. The text sounds like you don't but then your script does and why bother adding "matched" if it's ONLY matched lines in the output. So show us sample input and expected output.
    – Ed Morton
    Feb 20, 2020 at 20:33
  • You test the third field (comma separator). I assume, that the "Sally Sue" record has any comma related issue. Best is, you provide you file emails.txt for further analysis here.
    – Wiimm
    Feb 21, 2020 at 10:37

4 Answers 4

4

Could you please try following.

awk '/@yahoo\.com/{print $0,"matched"}' Input_file

Explanation: Your shown sample Input_file was not having comma so removed that field separator part from it.

Also a person's name can have more than 2 fields so I am not matching 3rd field in condition, rather I am checking condition on whole line here.

3

This is probably what you want:

awk -F, '{print $0 ($NF ~ /@yahoo\.com$/ ? " matched" : "")}' emails.txt

but without seeing your input file it's just an untested guess.

0
1

I tested the example with the following input: file delimiter given is , in your example and after splitting it u can access $3 record(email) and match your condition.

Input:

Joe,Smith,[email protected]
John,Doe,[email protected]
Sally,Sue,[email protected]```

Script

awk -F, '{if($3~/yahoo.com/){ print $1,$2,$3 " matched"}else{ print $1,$2,$3 }}' emails.txt

Output:

Joe Smith [email protected] matched
John Doe [email protected] 
Sally Sue [email protected] matched
0

If the line ends with @yahoo.com , reassign the entire line with itself followed by string matched.

awk '/@yahoo.com$/{$0=$0 " matched"}1' input

Joe Smith [email protected] matched
John Doe [email protected]
Sally Sue [email protected] matched

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.