Raw Input
File1:
dye
knit
bkk
Raw Input Desired Output
File2: each line consists of three fields separated by commas.
kabin:db:N
bkk:db:N
taipei:file:N
dye:file:N
knit:diskless:Y
dye:db:N
kabin:file:N
taipei:vpn:N
dye:vpn:Y
bkk:vpn:N
knit:file:Y
kabin:vpn:N
knit:vpn:N
kabin:db:N
bkk:db:Y
taipei:file:N
dye:file:Y
knit:diskless:Y
dye:db:Y
kabin:file:N
taipei:vpn:N
dye:vpn:Y
bkk:vpn:Y
knit:file:Y
kabin:vpn:N
knit:vpn:Y
Description
For every line of File2, if its first field is listed in File1, update the third field to `Y'.
Script and Comments
Script1
[ 1] /:/!{
[ 2] H
[ 3] d
[ 4] }
[ 5] G
[ 6] s/^(([^:]+):[^:]+):.\n.*\n\2(\n.*|$)/\1:Y/
[ 7] t
[ 8] s/\n.*//
Comments
  1. Use sed -r -f scriptfile file1 file2 to run this script.
  2. Since each line of File1 does not have commas while that of File2 does, Steps [1] thru [4] will be performed on lines of File1 but not on those of File2.
  3. Steps [1] thru [4] copies every line of File1 to HS to construct the update list.
  4. To check whether a line of File2 has to be updated,
    • First, step [5] appends the contents of HS to PS, PS looks like
      field1 : field2 : field3 \n \n update1 \n update2...
    • If RE of Step [6] matches, i.e., the first field exists in the update list, do the necessary substitution, then command `t' of Step [7] makes sed print the updated line, then starts a new cycle.
    • Otherwise, Step [8] removes the update list from PS, prints it, then starts a new cycle.