Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse text file to delimited

Status
Not open for further replies.

jgus

Technical User
Aug 3, 2001
15
US
How do I parse a text file (say c:\text.txt) that looks like this.....

User: LINLXB01
Full Name: LORRAINE K. BUCKWALD
User: LIN765
Full Name: 765 print server
User: LINPDN01
Full Name: Patrick D Nespor

.......into delimited text file like this?

LINLXB01~LORRAINE K. BUCKWALD
LIN765~765 print server
LINPDN01~Patrick D Nespor

Thanks
 
You can open a text file with
OPEN "c:\text.txt" FOR INPUT AS #1
new$(100)
WHILE NOT EOF(1)
INPUT #1, name$
name$ = RIGHT$(name$, LEN(name$) - 5) 'eliminates "User: "
INPUT #1, full$
full$ = RIGHT$(full$, LEN(full$) - 12)
'If That is a tab before Full Name:
'If it is five spaces then use LEN(full$) - 16
new$(a) = name$ + "~" + full$
a = a + 1
WEND
CLOSE #1
OPEN "c:\text.txt" FOR OUTPUT AS #1
FOR b = 0 TO a - 1
PRINT #1, new$(b)
NEXT
CLOSE #1
 
I should have mentioned that I'm not very familiar with batch programming, so this isn't making much sense to me. I tried putting this in a .bat file and running it but it doesn't seem to do anything. I'm not understanding how this outputs to a new text file? Lets say the beginning text file is C:\in.txt and the newly formatted text file is C:\out.txt.....can you please explain this in a little more detail? Thanks for your help and sorry to be a pain. FYI.....That is a tab before "Full Name:
 
This isn't batch programming - it's BASIC (see forum title)

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
As johnwm said, this is simple BASIC programming, not batch programming. Save this in a .bas file.

OPEN "c:\in.txt" FOR INPUT AS #1 'opens old file
new$(100)
WHILE NOT EOF(1)
INPUT #1, name$
name$ = RIGHT$(name$, LEN(name$) - 5) 'eliminates "User: "
INPUT #1, full$
full$ = RIGHT$(full$, LEN(full$) - 12)
'If That is a tab before Full Name:
'If it is five spaces then use LEN(full$) - 16
new$(a) = name$ + "~" + full$
a = a + 1
WEND
CLOSE #1 'closes original file
OPEN "c:\out.txt" FOR OUTPUT AS #1
'^^creates a new file and opens it
FOR b = 0 TO a - 1
PRINT #1, new$(b) 'writes tot he new file
NEXT
CLOSE #1

All thisprogram does is eliminates the headers (Name: and Full Name:) then takes the two strings that created and put them together with a ~ in between them.
 
open "text.txt" for input as #1
open "NewText.Txt" for output as #2
while not eof(1)
line input #1,LL$
L1%=INSTR(UCASE$(LL$),"USER:")
IF L1% then
Part1$=LTRIM$(mid$(LL$,L%+5))
end if
L2%=INSTR(UCASE$(LL$),"FULL NAME:")
If L2% then
Part2$=LTRIM$(mid$(LL$,L2%+10))
end if
If L1% and L2% then
OutStr$=Part1$+"~"+Part2$
Print #2,OutStr$
end if
L1%=0:L2%=0:part1$="":part2$="":OutStr$=""
wend
close

This could be run in most basic's Qbasic for example.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top