Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2005
    Posts
    19

    Unanswered: Shell - Search/Copy Record - Please Help

    Could someone please offer suggestions:

    I am trying to search a textfile with hundreds of lines (records).

    So I need to know how to search until the end of the file per each line. Within each line, I need to search for a particular string. If the string matches, I want to copy the whole line (record) into a new file (building a new file with the list of matching records).

    Also, when I copy the record, I would like to place a 'insert into table(' statement at the beginning and then put a semicolon at the end so that I can execute the file as sql.

    Please let me know if you have any ideas.

    Thanks a lot!

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    sed -ne '/string2search/s#.*#insert into table(&;#p' oldFile > newFile
    Last edited by vgersh99; 02-03-05 at 16:02.
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  3. #3
    Join Date
    Feb 2005
    Posts
    19
    Quote Originally Posted by vgersh99
    sed -e '/string2search/s#.*#insert into table(&;#g' oldFile > newFile
    Thank you for your help. I tried the following but it copied all records to the new file:

    sed -e '/MFP/s#.*#insert into table(&;#g' file1 > file2


    The string that I am searching for is "MFP", the initial file is file1, and the new file is file2. Could you please tell me what I'm doing wrong. Thanks a lot!

  4. #4
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    sorry for the confusion - I've just modified my original post iwth the correction.

    Code:
    sed -ne '/MFP/s#.*#insert into table(&;#p' file1 > file2
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  5. #5
    Join Date
    Feb 2005
    Posts
    19
    Quote Originally Posted by vgersh99
    sorry for the confusion - I've just modified my original post iwth the correction.

    Code:
    sed -ne '/MFP/s#.*#insert into table(&;#p' file1 > file2
    Wow, you’re a genius. It worked perfectly. It would have taken me forever to figure that out. Thanks a lot!

  6. #6
    Join Date
    Feb 2005
    Posts
    19
    Can you please tell me what I would do if I don't want to insert the statement: 'insert into table (' and if I don't want the semicolon at the end?

  7. #7
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    P: Doctor, it hurts when I do THAT!
    D: Then don't do THAT!

    Code:
    sed -ne '/MFP/s#.*#&#p' file1 > file2
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  8. #8
    Join Date
    Feb 2005
    Posts
    19
    Cool! Thanks a lot

Posting Permissions

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