searching and displaying help

I have two input files like this:

File-1
-----

a1234 abc town
f2345 def village
t5678 pqr county

File-2
------

123456 test1 test2 test3 id-a1234
789012 test2 test4 id-t5678
456789 test7 id-b1234

I want to check the lines that match the first field of File-1 in File-2 (where it appears after the "id") and create two output files as follows (based on whether the item is found in File-2 or not):

Output-1 (when the first field of File-1 is found at File-2)
-------------------------------------------------------

a1234 123456
t5678 789012

Output-2 (when the first field of File-1 is not found in File-2)
----------------------------------------------------------

f2345 Not Found

How to do this in a shell script?

Any help will be really appreciated.

Thanks,

Ajay

Something like this?

awk -F " |-" 'NR==FNR{a[$NF]=$1;next}
a[$1]{print $1,a[$1];next}
{print $1 " not found"
}' File-2 File-1

Regards

hmm, that didn't work :confused:

Be more precisely, what didn't work? No output, wrong output, warnings, errors?

Regards

I get Output-2 but not Output-1.

To print the output to the files you can redirect the outputs like this:

 awk -F " |-" 'NR==FNR{a[$NF]=$1;next}
a[$1]{print $1,a[$1];next}
{print $1 " not found" > "Output-2"
}' File-2 File-1 > Output-1

This is what I get:

$ cat File-1
a1234 abc town
f2345 def village
t5678 pqr county
$
$ cat File-2
123456 test1 test2 test3 id-a1234
789012 test2 test4 id-t5678
456789 test7 id-b1234
$
$ awk -F " |-" 'NR==FNR{a[$NF]=$1;next}
a[$1]{print $1,a[$1];next}
{print $1 " not found" > "Output-2"
}' File-2 File-1 > Output-1
$
$ cat Output-1
a1234 123456
t5678 789012
$
$ cat Output-2
f2345 not found
$

Regards

Thanks for the script.

But, I have issues when using it when the input file-2 has "test-2" instead of "test-2" or "test-3" instead of "test3" (the use of "-" in other columns.)

So, I am looking for the script to look only for the field that has the "id-" but must be able to ignore if the "-" presents in any other column.

Any suggestions?

Please provide a better example of your files and the desired output.

Regards