Solved

Removing first 4 characters from date field in csv file

Posted on 2016-10-19
7
44 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 24

Expert Comment

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

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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
LVL 84

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 84

Accepted Solution

by:
oBdA earned 500 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

737 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