View Full Version : Can I extract important fields from a lengthy text file and organize them?

01-16-2006, 02:13 AM
I must admit that I'm not really a coder. I do, however, consider myself to be rather adept at "borrowing" and manipulating enough code to meet my needs.

Without SA priveleges, I'm limited to what software I can use on my network at work. Also, for some work I'm limited to using UNIX workstations.

Because of this, I'm hoping to accomplish my task using javascript.

I want to consolidate various data from a lengthy text file and organize it in another window or text file.

Here is an example of the lengthy text file

1. name
2 address
3 phone number
4 sex
5 age
6 ethnicity
7 marital status

1. namex
2 address5
3 phone number3
4 sex7
5 age9
6 ethnicity7
7 marital status6

1. name5
2 address4
3 phone number32
4 sex237
5 age2427
6 ethnicity248
7 marital status5684


I want a script that will examine this data and create a new text that
might look like




Unfortunately the sets of data aren't numbered sequentially or logically and a text file that i want to process may contain more than 100 sets of data. So, if there are 100 sets of data, that means there are 100 1.'s, and 100 2.'s, etc.. the strings following each of these would need to be extracted separately and placed in a different location in the output...

I don't expect anyone to write a script for me, I'd just like to know if I'm wasting my time by attempting this using javascript. If my aim is unclear please let me know... Thanks!


01-16-2006, 06:16 AM
Javascript cannot read and write files to a host computer. The exception is the cookie, but a cookie is limited to 1k in length.

You might try Perl, which is particularly oriented toward string manipulation. I perused a Pearl book once. It's not trivial. I woundn't know if Perl is available on Unix. A guick google would answer that question I suppose.

How do you know what name,address, age go together?

01-16-2006, 03:58 PM
Perl would be an excellent choice for your needs... Perl is native to unix, so you won't have any compatability issues. Also Perl is excellent in text manipulation via regular expressions. Regular expression allows you to look for patterns in text data. Thus Name, Name1, Name241, and NameX91 could all be found with the expression: /Name(.*)?/. You'd also be able to extract those extra numbers after each of the labels (eg "", "1", "241", and "X91" for the above example) by using "capturing groups" in the regular expression (in the above expression, I made those capturing groups by enclosing the .* inside parentheses).

Other languages also allow the use of regular expressions as well... For example, you could use Java, which is also platform independent (so can be used on unix systems) and is free (so you shouldn't have to worry too much about using it at work).

As for javascript, unless the text-files are coming from off of the web, I wouldn't bother. As RadarBob mentioned, javascript is useful mainly for client-side web development; because javascript has very limited "file" capability, it would be unnecessarily difficult to get it to work properly... (perhaps copy / paste the text into a textarea on some web-page, push a button to parse the info., and do another copy / paste from the textarea back into some word-processor in order to save the result).

01-16-2006, 07:44 PM
Thanks guys... I guess I'll see if I can use perl on my unix box at work. I wish I had access to the web or the ability to take discs to work...