I have 2 scripts file.txt and file2.txt




|email|[email protected]| 

I need to create a temporary file to get all the rows in file1 where the column has mandatory . then I need to check if the mentioned mandatory fields are null or not in file2

Expected output:

age mandatory

1 Answer 1


You could use awk for this:

awk -F '|' '
    NR==FNR && $3=="mandatory" {m[$2]++}
    NR>FNR && $3=="" && m[$2] {printf "%s mandatory\n", $2}
' file1.txt file2.txt


age mandatory


awk iterates across each line of both files. The record separator is considered to be |. That means the first field is always empty.

The first line of the script looks for lines in file1.txt (the NR Number of Records read so far matches the line number of the current file), where the third field is mandatory, and saves the second field in an array.

The second line of the script looks for lines in file2.txt (the Number of Records read is more than the line number of the current file) and, when the third field is empty, and it's in the list we created earlier, then we print out the first field's name


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .