A question for the "vi" fanatics

aicul

Registered
I have a long file containing different customer data from my business. This file is generated by an "antique" system that works and I don't want to modify.

The problem is that I would like to split this long file into many smaller files based on the different customers.

The file structure is like:

Customer: abc
<data>
Customer: xyz
<data>
and so on..

I was told that vi can recusrively split the file. Maybe even call the files by the customer name.

I gave this a shot and, quickly realized that vi is a little complex.

Can someone give a hand please?
Is vi the right choice?
 
'm not an uber-vi user but I'm quite good in tcl, so I wrote this script:

---CUT HERE----
#!/usr/bin/tclsh

#Ask for the input file and open iti
puts "Give filename"
gets stdin filename
set inputfile [open $filename]

set nr 0
set outputfile ""
while {[gets $inputfile line] >= 0} {
##puts "processing line \"$line\""
#Does this line start with 'Customer?'
if { [regexp ^Customer $line] } {
#Yes it does...
puts "putting Customer $line in file$nr.txt"
set outputfile [open file$nr.txt w]
incr nr
}
puts $outputfile $line
}
puts "done..."
-----CUT HERE----

Put this in a file, e.g. split.tcl
and start using 'tclsh split.tcl'

It will ask your input file and it will create 'file0.txt',
'file1.txt', 'file2.txt', ... each file has one customer record. You
can change the filenames by modifying the line 'set outputfile [open
file$nr.txt w]'

Bert
 
Ok, tried it, it works. But I would need some clarifications:

1. Is TCL available on all unix based systems (even non Apple/OSX)?

2. I would like the customer "name" to appear. I tried modifiying your script and somewhat succeded until I needed to cut-out the customer name from $line. I looked at the TCL forums and really could not make ends meet. I tried this :

regsub -all {\<Customer :\>} $line "" $line
puts "putting Customer $line in file$line.txt"
set outputfile [open file$line.txt w]

But there seems to be a catch.

Thanks for input.
 
1. Is TCL available on all unix based systems (even non Apple/OSX)?

Tcl is available on all modern Linux distributions and on the Solaris and HP/UX systems I use at work. But I'm not sure if Tcl is standardly installed on these latter two systems. Perl is definetly installed on all unix-like systems, but still have to take my first steps in perl...

2. I would like the customer "name" to appear. I tried modifiying your script and somewhat succeded until I needed to cut-out the customer name from $line. I looked at the TCL forums and really could not make ends meet.

Maybe it's more easy to use the tcl 'split' or 'string' function or a 'regexp'-matching for the customer name, but I tried your solution and you're definetly on the right track.

if { [regexp ^Customer $line] } {
set customer [regsub -all "Customer: " $line ""]
puts "Found customer $customer, putting it in file file$customer.txt"
set outputfile [open file$customer.txt w]


This should do the trick,

Gr,
Bert
 
Thanks, for the advice. Actually had sorted the syntax issue myself after many, many tries. This TCL stuff is not bad. I'll use it again as required.

Cheers
 
Back
Top