Bill McCartney
Bill McCartney

Reputation: 1

Help with Script for Extracting Specific Data from String

Below is a list of data in a single file. I'd like to run this in Powershell.

LESCAR85 07/31/10 1700678991 / 70039536 $35.00
SQUADCT8 07/31/10 1698125739 / 70039539 $35.00
RINGFIEL 07/29/10 11041563 / 70039639 $35.00

The 8 digit number and then the dollar amount at the end I would like convert to csv so that I can use that in an excel file. The first set of longer numbers is not always the same which changes the number of spaces between the 8 digit sequence I need and the dollar amount.

Thanks!

Upvotes: 0

Views: 92

Answers (1)

zdan
zdan

Reputation: 29450

As long as ' / ' (slash bounded by spaces) always seperates the data you are interested in from the beginning of the string, then you can use this:

get-content yourDataFile.txt | foreach{(-split ($_ -split ' / ')[1] ) -join ','} > yourResult.csv

What the foreach loop does:

  1. splits each line at the ' / '
  2. Takes the second part of the split (what you are interested in), and splits again at whitespace.
  3. The resulting elements are then joined together using a comma.

Upvotes: 1

Related Questions