Solved

Split content of a csv file using powershell

Posted on 2011-09-26
6
822 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
Comment Utility
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
Comment Utility
Sorry .... did not see the powershell .....
0
 
LVL 9

Accepted Solution

by:
chrismerritt earned 500 total points
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:SQLM_M
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for your solution..
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

10 Experts available now in Live!

Get 1:1 Help Now