?
Solved

Removing first 4 characters from date field in csv file

Posted on 2016-10-19
7
Medium Priority
?
57 Views
Last Modified: 2016-10-19
I have a csv that is an export from a student information system. Lets say field J is the student enrollment date. it comes out as 09/02/2012 i need to change it to just read 2012 (essentially losing the first four characters) for each line of J in the CSV file ?

Does anyone have a quick batch or VB script that would do something like that ?
0
Comment
Question by:sattermc
[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
  • 3
  • 3
7 Comments
 
LVL 25

Expert Comment

by:NVIT
ID: 41850858
Can you submit a sample csv file?
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41850866
Batch can be tricky when handling text files containing special characters.
Powershell is safer (and not as nineties as VB Script).
If the csv has no header line, you need to provide at least the number of columns in the file.
Assuming the csv has a header line, and the column is called "J":
$ColumnHeader = "J"
Get-Content C:\Temp\Input.csv | % {$_.$ColumnHeader = ($_.$ColumnHeader).Split('/')[2]; $_} | Export-Csv C:\Temp\Output.csv -NoTypeInformation

Open in new window

And you'd lose the first 6 characters ...
0
 

Author Comment

by:sattermc
ID: 41850884
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

Expert Comment

by:oBdA
ID: 41850904
This will add quotes around the fields as well; is that a problem?
Import-Csv -Path C:\Temp\Input.csv -Header (1..17 | % {"Col_$($_)"}) |
	ForEach-Object {$_.Col_10 = ($_.Col_10).Split('/')[2]; $_} | 
	Export-Csv C:\Temp\Output.csv -NoTypeInformation

Open in new window

0
 

Author Comment

by:sattermc
ID: 41850918
with ps script get "You cannot call a method on a null-valued expression."

script i'm running is:
$ColumnHeader = "m"
Get-Content d:\ioeducation\iostudent.csv | % {$_.$ColumnHeader = ($_.$ColumnHeader).Split('/')[2]; $_} | Export-Csv D:\ioeducation\student1.csv -NoTypeInformation

quotes would be a problem
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 41850948
Try this then:
$InputFile = "C:\Temp\Input.csv"
$OutputFile = "C:\Temp\Output.csv"
Import-Csv -Path $InputFile -Header (1..17 | % {"Col_$($_)"}) |
	ForEach-Object {$_.Col_10 = ($_.Col_10).Split('/')[2]; $_} | 
	ConvertTo-Csv -NoTypeInformation |
	Select-Object -Skip 1 |
	ForEach-Object {$_ -replace '"', ''} |
	Set-Content -Path $OutputFile

Open in new window


Alternative, string-based split:
$InputFile = "C:\Temp\Input.csv"
$OutputFile = "C:\Temp\Output.csv"
Get-Content -Path $InputFile |
	ForEach-Object {
		$Cols = $_.Split(',')
		$Cols[9] = $Cols[9].Split('/')[2]
		$Cols -join ','
	} |
	Set-Content -Path $OutputFile

Open in new window

0
 

Author Closing Comment

by:sattermc
ID: 41850971
Perfect,

Thank you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

771 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