• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 70
  • Last Modified:

Extracting Data from a CSV - Looping for different data sets

Good Morning Experts,

I am looking for your help with a problem I currently have...

Everyday I have an CSV file that gets created and it contains several sets of data.

Each record has 3 headings - Item Type, Label & Response

The first records always starts under Item Type with Section and Label with Title

Example

ItemType      Label         Response
Section          Title

the record ends when you see the above again and then a another record begins etc... - see attachment with some example data

For each record I would like to extract the data under responses - only certain rows -

List - Client / Site / Job Number - Response (Data Required) - In CSV attachment Example - Data = "User Site 1 - Test Site - Job  Number: 1756"
textsingle - Order Number: - Response (Data Required) - In CSV attachment Example - Data = "HV_000003_992809"
datetime - Completed On: - Response (Data Required) - In CSV attachment Example - Data = "19/04/2018 13:51"
textsingle - Completed By: - Response (Data Required) - In CSV attachment Example - Data = "Nick Test"
signature - Certificate received on behalf of : (Full Name and Signature) - Response (Data Required) - In CSV attachment Example - Data = "Mr Jerry Tester"
textsingle - E-Mail Address: - - Response (Data Required) - In CSV attachment Example - Data ="Test@gmail.com"

For each record set I would like to extract the text as shown above

The final output put into a new text file - so each row is a set of data

Based on the CSV attachment the text file would be like as follows

"User Site 1 - Test Site - Job  Number: 1756", "HV_000003_992809", "19/04/2018 13:51", "Nick Test", "Mr Jerry Tester", "Test@gmail.com"

So a new line for a new set of data

I hope you understand what I am trying to achieve...

Thanks
0
Nick Collins
Asked:
Nick Collins
  • 7
  • 5
2 Solutions
 
aikimarkCommented:
Please post one of these CSV files
0
 
dtyerechCommented:
The file you are getting is from a source, can you configure the source to give you want you want?
0
 
Nick CollinsAuthor Commented:
I can't change the source
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
Nick CollinsAuthor Commented:
File attached
Template1.csv
0
 
aikimarkCommented:
To help make sure I got the logic right, please run this Powershell script and see if the output is what you need.
You will need to point to your own path, not C:\Users\Mark\downloads\
You didn't show a CSV header line in your example output.  Does this mean that you don't want headers?
$TargetLabels = @("Client / Site / Job Number", "Order Number:", 
"Completed On:", "Completed By:", 
"Certificate received on behalf of : (Full Name and Signature)", "E-Mail Address:")

$a = Import-Csv  C:\Users\Mark\downloads\template1.csv

$a | % -begin{$rec=@(); $o=@{}} `
       -process{ `
        if ($_.label -eq "Title Page") {
            if ($o.count -ne 0) {
                $rec += new-object -TypeName psobject -Property $o
                $o=@{}
            }
        }
    if ($TargetLabels -contains $_.label){
        $o += @{$_.label=$_.response}
        }
}
#add the one we were constructing
$rec += new-object -TypeName psobject -Property $o
$rec | Export-Csv -Path C:\Users\Mark\downloads\Q_29095438.csv -NoTypeInformation

Open in new window

1
 
Nick CollinsAuthor Commented:
Thank you.. that has worked perfectly.

On the output file - is it possible to put the YYYYMMDDhhmmss at the end of the file name
0
 
aikimarkCommented:
Do you want to go with a PS solution?

Yes.  It is possible to alter the name of the output file.
0
 
Nick CollinsAuthor Commented:
Yes powershell is good
0
 
aikimarkCommented:
$TargetLabels = @("Client / Site / Job Number", "Order Number:", 
"Completed On:", "Completed By:", 
"Certificate received on behalf of : (Full Name and Signature)", "E-Mail Address:")

$a = Import-Csv  C:\Users\Mark\downloads\template1.csv

$a | % -begin{$rec=@(); $o=@{}} `
       -process{ `
        if ($_.label -eq "Title Page") {
            if ($o.count -ne 0) {
                $rec += new-object -TypeName psobject -Property $o
                $o=@{}
            }
        }
    if ($TargetLabels -contains $_.label){
        $o += @{$_.label=$_.response}
        }
}
#add the one we were constructing
$rec += new-object -TypeName psobject -Property $o
$rec | Export-Csv -Path "C:\Users\Mark\downloads\Q_29095438_$(Get-Date -Format 'yyyyMMddhhmmss').csv" -NoTypeInformation

Open in new window

0
 
aikimarkCommented:
I've added Powershell to the zone list and added the datetime stamp as part of the output file name.
0
 
aikimarkCommented:
go ahead and close out this question
0
 
Nick CollinsAuthor Commented:
Following on with the powershell..

With the same as above fields, on the output file I would like to append the "Client / Site / Job Number" & "Order Number" together then separated by comma delimiter.

Client / Site / Job Number_Order Number

Without the double-quotes on the output file
0
 
aikimarkCommented:
@Nick

Please open a new question
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now