Solved

Split content of a csv file using powershell

Posted on 2011-09-26
6
829 Views
Last Modified: 2012-08-14
Hi,
    I want to split the content of a csv file based on a delimiter. The csv file has three columns. I want to split the content of first column i.e i want to split the text after last  occurence of "\". Please provide me a sample.

Sample:
F:\CDR_FTP\SMS\Archive\20110923\IE3_CS_22_09_2011_23_49_00_016145.cdr 68 68
F:\CDR_FTP\SMS\Archive\20110923\IE3_CS_22_09_2011_23_52_18_016146.cdr 68 68
F:\CDR_FTP\SMS\Archive\20110923\IE3_CS_22_09_2011_23_55_30_016147.cdr 68 68
F:\CDR_FTP\SMS\Archive\20110923\IE3_CS_22_09_2011_23_59_11_016148.cdr 15 69

Expected Output:
IE3_CS_22_09_2011_23_49_00_016145.cdr 68 68
IE3_CS_22_09_2011_23_49_00_016146.cdr 68 68
IE3_CS_22_09_2011_23_49_00_016147.cdr 68 68
IE3_CS_22_09_2011_23_49_00_016148.cdr 15 68
0
Comment
Question by:SQLM_M
  • 2
  • 2
  • 2
6 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 36599214
here goes, remember to change the path to your file path
        Using sr = New FileIO.TextFieldParser("YouPath") With {.Delimiters = {"\"}}
            While Not sr.EndOfData
                Dim line = sr.ReadFields()
                Dim TheResult = line.Last
            End While
        End Using

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36599218
Sorry .... did not see the powershell .....
0
 
LVL 9

Accepted Solution

by:
chrismerritt earned 500 total points
ID: 36601400
I'm making some assumptions here, based on not know what you want to do with these values etc afterwards.

Assumption 1:

Your CSV file lives is called C:\TEMP\file.csv

Assumption 2:

Your CSV file is laid out like this:

Path
F:\CDR_FTP\SMS\Archive\20110923\IE3_CS_22_09_2011_23_49_00_016145.cdr 68 68
F:\CDR_FTP\SMS\Archive\20110923\IE3_CS_22_09_2011_23_52_18_016146.cdr 68 68
F:\CDR_FTP\SMS\Archive\20110923\IE3_CS_22_09_2011_23_55_30_016147.cdr 68 68
F:\CDR_FTP\SMS\Archive\20110923\IE3_CS_22_09_2011_23_59_11_016148.cdr 15 69

If so this code should split the content and give you the last section of the text:

$CSVFile = Import-CSV "C:\TEMP\file.csv"

foreach ($Line in $CSVFile)
{
	$LastSection = ($Line.Path).Split("\")[-1]
	$LastSection | Out-Host
}

Open in new window

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:SQLM_M
ID: 36707513
Hi,

Thanks for your response, I get the following error when i execute your code.

You cannot call a method on a null-valued expression.
At line:3 char:38
+     $LastSection = ($Line.Path).Split <<<< ("\")[-1]
    + CategoryInfo          : InvalidOperation: (Split:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
0
 
LVL 9

Expert Comment

by:chrismerritt
ID: 36707866
To execute my code the CSV must be formatted exactly the same as I have entered, including the "Path" heading at the top of the list of directories.

$Line.Path is saying saying that for each line of the CSV file, use the value found in the "Path" column.

I have tested the script and it does work, so either it's not linking to the right CSV file location or the CSV file does not contain the data in the same way I have entered it.

You can try stepping through it to make sure that values are set properly, i.e. after executing my code but before closing the window, echo back the value of $CSVFile by simply entering $CSVFile into the prompt and hitting return.

You can do this for any value, powershell will remember what it's assigned to variables in the same window, so for example if you do this:

$a = 10

Then afterwards just put this:

$a

It will echo back to you the value 10.
0
 

Author Closing Comment

by:SQLM_M
ID: 36812824
Thanks for your solution..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
A procedure for exporting installed hotfix details of remote computers using powershell
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now