jonno
jonno

Reputation: 1

Batch extracting lines from text files to one CSV

So I have a very simple question, but I am a little retarded and so need some help!

I need a command-line batch file that does the following:

I have 5,000 text files, that all have the same structure (i.e. the same fields on each line), but different data. I would like to extract data from lines 3 and 5 in batch for all the files, and put this into one simple CSV, that also labels the filename.

For example, the text files (labled 1.txt through to 5000.txt) look like this:

[data on line 1]
[data on line 2]
[data on line 3]
[data on line 4]
[data on line 5]
[data on line 6]

And the desired output in CSV I am after is something like this:

[Filename 1.txt],[Data from line 3],[Data from line 5]
[Filename 2.txt],[Data from line 3],[Data from line 5]

and so forth...

[Filename 5000.txt],[Data from line 3],[Data from line 5]

Any ideas?

Upvotes: 0

Views: 2388

Answers (2)

OnlyMe
OnlyMe

Reputation: 31

Or maybe use the File System Object from within MS Excel in VBA ?

If programming isn't your thing, you could look into keyboard macro recorders. Typically you hit 'record' and do it manually for one file, Alt+Tab; DownArrow to the next file and hit 'stop'. Then assign the macro to a key combination. Repeatedly hitting the key combination. will do one file at a time. For 5,000 files, you may find 5,000 keystrokes is easier than learning a strange programming language in an unfamiliar environment.

I don't suppose you have line numbers in the text files ? ie Line 1 begins with "1," etc ... that would make life much easier (see LogParser above). There may be something out there that adds line numbers to an existing file ?

Upvotes: 1

OnlyMe
OnlyMe

Reputation: 31

There is a free Microsoft tool called LogParser that lets you use SQL to return data from specified columns in lines of text matching a query, but you want to return specified lines, not columns.

There used to be a line-editor called EDLIN in MS-DOS. It's still available in a command window in Windows 7. It may be possible to use it in a batch file to extract the lines you want.

Good luck !

Upvotes: 1

Related Questions