Computing.Net > Forums > Programming > Text File Parsing

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Text File Parsing

Reply to Message Icon

Name: Bishopz
Date: April 7, 2006 at 15:01:12 Pacific
OS: Windows XP
CPU/Ram: 3.2
Product: 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



Sponsored Link
Ads by Google

Response Number 1
Name: FishMonger
Date: April 7, 2006 at 16:56:48 Pacific
Reply:

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;


0

Response Number 2
Name: Mechanix2Go
Date: April 8, 2006 at 00:55:58 Pacific
Reply:

FM strikes again :*)


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

M2Go



0

Response Number 3
Name: SN
Date: April 8, 2006 at 19:58:34 Pacific
Reply:

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


0

Response Number 4
Name: FishMonger
Date: April 9, 2006 at 08:47:44 Pacific
Reply:

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.


0

Response Number 5
Name: ghostdog
Date: April 13, 2006 at 07:47:34 Pacific
Reply:

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()



0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Text File Parsing

Bash script text file parsing www.computing.net/answers/programming/bash-script-text-file-parsing/17858.html

vbscript to parse text file and com www.computing.net/answers/programming/vbscript-to-parse-text-file-and-com/16691.html

Parsing Text files www.computing.net/answers/programming/parsing-text-files/17102.html