I need help to filter a text file.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • superc0red
    New Member
    • Feb 2007
    • 5

    I need help to filter a text file.

    i have a file:

    date: 02-02-2006
    name: geopar
    doc: test machine
    username: test1
    password: p4ss
    address: test2
    phone: 343534543535
    username: test
    fax: 453453534
    etc.....

    i want to generate a new file with data : (username password)

    (a script to check for structure
    username:
    password:
    in file, and if found dump username & password to outfile..)

    help :)
  • docsnyder
    New Member
    • Dec 2006
    • 88

    #2
    @superc0red

    The basic idea is:
    Code:
    @file = (
      "date: 02-02-2006",
      "name: geopar",
      "doc: test machine",
      "username: test1",
      "password: p4ss",
      "address: test2",
      "phone: 343534543535",
      "username: test",
      "fax: 453453534",
      "",
      "date: 02-02-2006",
      "name: geopar",
      "doc: test machine",
      "username: test2",
      "password: p5ss",
      "address: test2",
      "phone: 343534543535",
      "username: test",
      "fax: 453453534",
    );
    
    for $line ( @file ) {
      if ( $line =~ m/^username:\s+(.*)$/ ) {
        $user = $1;
      }
      elsif ( $line =~ m/^password:\s+(.*)$/ ) {
        $pw = $1;
    
        printf("$user $pw\n");
    
        $user = $pw = undef;
      }
    }
    Reading in the input file and writing the result to the output file instead of printing it is left to you as an exercise ;o)

    Greetz, Doc

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Originally posted by superc0red
      i have a file:

      date: 02-02-2006
      name: geopar
      doc: test machine
      username: test1
      password: p4ss
      address: test2
      phone: 343534543535
      username: test
      fax: 453453534
      etc.....

      i want to generate a new file with data : (username password)

      (a script to check for structure
      username:
      password:
      in file, and if found dump username & password to outfile..)

      help :)
      Do you have any existing code? If so maybe it just needed a small tweak to work. If not, Docs suggestion should get you over the hump.

      Comment

      Working...