Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Split content of a csv file using powershell

Posted on 2011-09-26
6
Medium Priority
?
849 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

 

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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

670 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