Find the answer to your Linux question:
Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By tpb261
Hi all, I have two files, where one file is already having some changes. and i want to incorporate same changes into another file. So,I need to write one script, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2013
    Posts
    3

    sed/awk scripting help to edit files automatically.


    Hi all,

    I have two files, where one file is already having some changes.
    and i want to incorporate same changes into another file.

    So,I need to write one script, which
    will do following steps sequentially:

    search for specific pattern in a given file, and search the same pattern in another file, and finally copy the text found after 2 lines of searched pattern in first file into second file after 2 lines of the searched pattern.

    so, if it is possible with sed/awk scripting, pls help me out..

    Thanks

  2. #2
    Just Joined!
    Join Date
    Sep 2007
    Posts
    19
    Quote Originally Posted by praveend01 View Post
    Hi all,

    search for specific pattern in a given file, and search the same pattern in another file, and finally copy the text found after 2 lines of searched pattern in first file into second file after 2 lines of the searched pattern.
    This is confusing. Can you give an example.
    mrbruno likes this.

  3. #3
    Just Joined!
    Join Date
    Feb 2013
    Posts
    3
    Hi,

    for example:

    file1.txt : f
    -----------

    hello praveen,
    how r u...
    now we need to test this..
    ok fine ...!!

    hello santosh,
    how r u...
    now we need to test this..
    ok fine ...!!

    hello pranesh,
    how r u...
    now we need to test this..
    ok fine ...!!


    file2.txt :
    ----------------------

    hello praveen,
    how r u...
    now what we have to do..
    ok fine ...!!

    hello raghunath,
    how r u...
    now what we have to do..
    ok fine ...!!

    hello pranesh,
    how r u...
    now what we have to do..
    ok fine ...!!



    Now i want file2.txt to be changed to as shown below:


    hello praveen,
    how r u...
    now we need to test this..
    ok fine ...!!

    hello raghunath,
    how r u...
    now what we have to do..
    ok fine ...!!

    hello pranesh,
    how r u...
    now what we have to do..
    ok fine ...!!

    ---------------------------------------------------


    Means i need to search for pattern "praveen" in file1.txt and file2.txt,
    and copy contents of 2nd line after pattern "praveen" in file1.txt to file2.txt



    Thanks

  4. #4
    Just Joined!
    Join Date
    Sep 2007
    Posts
    19
    Ok. I'm not sure if your example was so good. There were 3 common strings (1) hello XYZ (2) how r u and (3) ok fine ...!!
    I think I can understand what you are trying to do. sed/grep will not help AFAIK. Try using gawk/perl. In gawk you can read both files for one pattern. Then start transferring as many lines as you want from one to other. Then move on to the next pattern. Not easy though. It'll take some time in gawk. Python or Perl or maybe even C(!!), might be faster.
    I'd suggest do one pattern at a time. Run a shell script to complete for all patterns.
    Last edited by tpb261; 02-06-2013 at 01:50 PM. Reason: :oops: Stupid thing to try to quote the entire message

  5. #5
    Just Joined!
    Join Date
    Feb 2013
    Posts
    3
    Yes, you understood correctly. Thanks for giving proper direction..

Posting Permissions

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