Shell Script to find common lines and replace next line

I want to find common line in two files and replace the next line of first file with the next line of second file. (sed,awk,perl,bash any solution is welcomed ) Case Ignored. Multiple Occurrence of same line.

File 1:

         hgacdavd
sndm,ACNMSDC
msgid "Rome"
msgstr ""
kgcksdcgfkdsb
msgid ""
hsdvchgsdvc
msgstr ""
dhshfjksdfhmd
msgid "Vidya"
msgstr ""
sdjhcbnd
dcndnv
cfnkdndvrknvkf
dfkvrnkdfnk
snfvrkng
msgid "Rome"
msgstr ""
wdbhkjbcfj
#dmcdmf
f,nvdf,

fvnfnvk
vfmf,mv
vfn

msgid "vid"
msgstr ""
dmcbdmbcvmfbvmkhsdk

file 2:

dfhkvgjbfrvkf  
msgid "Rome"
msgstr "new bie"
sdbsjbcdcbwoido
fjcdcvnm
msgid "vidya"
msgstr "expert"
dvnjfkdvhnkfvnknsbdjh


msgid "vid"
msgstr "newton"
dfenfjdbrfjbvlfnvl
dcnkncvkdfvknfv
fcndkbvknfkv
vfdnkvnfknbvkfn

Later File 1 should be:(after execution of script)

  hgacdavd
sndm,ACNMSDC
msgid "Rome"
msgstr "new bie"
kgcksdcgfkdsb
msgid ""
hsdvchgsdvc
msgstr ""
dhshfjksdfhmd
msgid "Vidya"
msgstr "expert"
sdjhcbnd
dcndnv
cfnkdndvrknvkf
dfkvrnkdfnk
snfvrkng
msgid "Rome"
msgstr "new bie"
wdbhkjbcfj
#dmcdmf
f,nvdf,

fvnfnvk
vfmf,mv
vfn

msgid "vid"
msgstr "newton"
dmcbdmbcvmfbvmkhsdk

Try:

awk 'NF{i=tolower($0)} NR==FNR{A[p]=$0; p=i; next} i in A{getline x; $0=$0 ORS A}1' file2 file1

That would work fine ,if file1 content and file2 contents are different... but it is not working if contents are same only ^msgstr.*$ line is different.. for example..it wont work if my contents are something like this:

file1:

         hgacdavd
sndm,ACNMSDC
msgid "Rome"
msgstr ""
kgcksdcgfkdsb
msgid ""
hsdvchgsdvc
msgstr ""
dhshfjksdfhmd
msgid "Vidya"
msgstr ""
sdjhcbnd
dcndnv
cfnkdndvrknvkf
dfkvrnkdfnk
snfvrkng
msgid "Rome"
msgstr ""
wdbhkjbcfj
#dmcdmf
f,nvdf,

fvnfnvk
vfmf,mv
vfn

msgid "vid"
msgstr ""
dmcbdmbcvmfbvmkhsdk

file2:

         hgacdavd
sndm,ACNMSDC
msgid "Rome"
msgstr "new bie"
sdbsjbcdcbwoido
kgcksdcgfkdsb
msgid ""
hsdvchgsdvc
msgstr ""
dhshfjksdfhmd
msgid "vidya"
msgstr "expert"
sdjhcbnd
dcndnv
cfnkdndvrknvkf
dfkvrnkdfnk
snfvrkng
msgid "Rome"
msgstr "new bie"
sdbsjbcdcbwoido
wdbhkjbcfj
#dmcdmf
f,nvdf,

fvnfnvk
dvnjfkdvhnkfvnknsbdjh


msgid "vid"
msgstr "newton"
dfenfjdbrfjbvlfnvl
dmcbdmbcvmfbvmkhsdk

after execution , file1 content should be:

  hgacdavd
sndm,ACNMSDC
msgid "Rome"
msgstr "new bie"
kgcksdcgfkdsb
msgid ""
hsdvchgsdvc
msgstr ""
dhshfjksdfhmd
msgid "Vidya"
msgstr "expert"
sdjhcbnd
dcndnv
cfnkdndvrknvkf
dfkvrnkdfnk
snfvrkng
msgid "Rome"
msgstr "new bie"
wdbhkjbcfj
#dmcdmf
f,nvdf,

fvnfnvk
vfmf,mv
vfn

msgid "vid"
msgstr "newton"
dmcbdmbcvmfbvmkhsdk

On the first line file 2 differs by 1 space, so there is no match on that line. Also there is an extra line in file1 dhshfjksdfhmd that matches and so the answer is msgid "vidya" and therefore msgid "Vidya" will not get looked up.

I guess it comes down to:

  • is there a format to the file?
  • if so, what is it?
  • if so, do these files conform to that format?
  • if so, what would you like to do in the case above?

As I mentioned on top... We are ignoring the case. So, I think it will do the look-up..