0

I have 2 scripts file.txt and file2.txt

file1.txt

|col1|col2| 
|name|mandatory|
|age|mandatory| 
|address|mandatory|
|email|mandatory| 
|country|not-mandatory| 

file2.txt

|col1|col2| 
|name|gabrielle| 
|age|| 
|address|nashville| 
|email|[email protected]| 
|country|| 

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
0

1 Answer 1

0

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

Output

age mandatory

Explanation

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

0

You must log in to answer this question.

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