Computing.Net > Forums > Programming > Text File Parsing

Text File Parsing

Reply to Message Icon

Original Message
Name: Bishopz
Date: April 7, 2006 at 15:01:12 Pacific
Subject: Text File Parsing
OS: Windows XP
CPU/Ram: 3.2
Model/Manufacturer: Dell
Comment:

I would like to take
the following data from a text file called c:\userexp.txt:
===========================================
username1 - Expiration date: Sat Aug 15 00:00:00 PDT 2009
username2 - Expiration date: Tue Jan 01 00:00:00 PST 2030
username3 - Expiration date: Wed Aug 17 00:00:00 PDT 2011
===========================================
and parse out needed information to a batch file called c:\userexpimport.bat
that needs to look like the lines below:
============================
net user username1 /expires:aug,15,2009
net user username2 /expires:jan,01,2030
net user username3 /expires:aug.17,2011
============================


Bishopz


Report Offensive Message For Removal

Response Number 1
Name: FishMonger
Date: April 7, 2006 at 16:56:48 Pacific
Subject: Text File Parsing
Reply: (edit)

You didn't say which language you wanted to use to do the parsing. Here's a Perl script. If you want to use input/output redirection, I can reduce it by 4 lines and just have the while loop. Or, it can even be redueced to a single line executed from the command line instead of the script.

#!perl -w

open (SRC, "c:/userexp.txt") || die $!;
open (BAT, ">c:/userexpimport.bat") || die $!;

while(<SRC>) {
$_ = lc($_);
/^(\S+) [^:]+: \w+ (\w+) (\d\d).*(\d{4})$/;
print BAT "net user $1 /expires:$2,$3,$4\n";
}
close SRC;
close BAT;


Report Offensive Follow Up For Removal

Response Number 2
Name: Mechanix2Go
Date: April 8, 2006 at 00:55:58 Pacific
Subject: Text File Parsing
Reply: (edit)

FM strikes again :*)


=====================================
If at first you don't succeed, you're about average.

M2Go



Report Offensive Follow Up For Removal

Response Number 3
Name: SN
Date: April 8, 2006 at 19:58:34 Pacific
Subject: Text File Parsing
Reply: (edit)

Posts like these make me miss my perl days...It's amazing what a pro like FM can get done in one line of code.

-SN


Report Offensive Follow Up For Removal

Response Number 4
Name: FishMonger
Date: April 9, 2006 at 08:47:44 Pacific
Subject: Text File Parsing
Reply: (edit)

Thanks for the compliment.

Perl is a great language and I enjoy writing the scripts, but you guys definitely have me beat in other areas, such as PHP and batch scripts. I've seen some impressive batch scripting from M2Go and IVO.


Report Offensive Follow Up For Removal

Response Number 5
Name: ghostdog
Date: April 13, 2006 at 07:47:34 Pacific
Subject: Text File Parsing
Reply: (edit)

here's a python script.

import os
root = "c:\\"
inputfile = os.path.join(root,"userexp.txt")
outputfile = os.path.join(root,"userexpimport.bat")
a = open(inputfile)
b = open(outputfile,"a")
while 1:
line = a.readline().strip()
if line == '':
break
user = line.split(':',1)[0].split()[0]
month,day = line.split(':',1)[1].strip().split( )[1:3]
year = line.split(':',1)[1].strip().split( )[-1]
b.write("net user %s /expires: %s,%s,%s\n" %(user,month,day,year))
a.close()
b.close()



Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Text File Parsing

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software