Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2014
    Posts
    3

    Exclamation Unanswered: 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:
    Code:
             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:
    Code:
      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:
    Code:
              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

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    There are a couple of approaches that should work.
    One is to repeatedly read and write a new file1 for as many times as there are records in file2.
    The other is to add a line number to each record in file1 and file2, sort the files then use a "balanced line update" to create a new file3, and then sort file3 back into its original sequence using the line numbers.

  3. #3
    Join Date
    Feb 2014
    Posts
    3
    That would become..O(n sqr) ,where my n is line number ,which is around 5k.

  4. #4
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    The second method requires one pass of each input file to assign line numbers.
    Another pass of each to sort into data sequence.
    A balanced line update is a merge that generally was used with tape files to read an old master, apply transactions and produce a new master.
    And finally one pass of the new file to sort into its original sequence and discard the line numbers.
    Another solution might be to create a work file3 that is indexed on field one, and each record is made from file2 so that:
    rec1 rec2
    rec2 rec3
    rec3 rec4
    rec4 rec5
    ....
    Then sequentially read file1 and using its record as a key seek file3. If a match is found, write the record from file1 and then field2 from file3 and skip a record in file1.

  5. #5
    Join Date
    Feb 2014
    Posts
    3

    Clarification

    Code:
    Search msgstr "" pattern in file1
           take its previous line, find this line in file2 
    if found then replace the next line of file1 with next line of file2
    Thats the only thing i want to do...for all those msgstr with null value,i want to search its relevant value from file2 and replace the null with the value.
    Please provide me implementation logic.

  6. #6
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Talking

    Quote Originally Posted by madira View Post
    That would become..O(n sqr) ,where my n is line number ,which is around 5k.
    Try this:
    Code:
    #
    awk 'BEGIN{i=x=0;j=1;
      while ( (getline lin < "file2" ) > 0 ){i+=1; f2[i]=lin;}
      close("file2");
      for(i in f2) n+=1;}
    { if(x==1){x=0;next}
      f1=$0; k=0;
      for(i=j;i<=n;++i){if(f1==f2[i]){k=i;continue}}
      if(k>0){print f1; j=k+1; print f2[j];x=1;next}
      print f1;
    }' file1
    Last edited by LKBrwn_DBA; 02-13-14 at 19:03.
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •