Solved

How to create a csv from another csv in powershell ?

Posted on 2013-05-21
8
298 Views
Last Modified: 2013-05-21
Hello,
I have a csv1 file with the following headers: Last Name      First Name      Dept Descrip      Social Security Numbers      Employment Category      Job Title      Work Phone      Room      Clock Number      Date Of Birth      Div Descrip

I would like to create csv2 file from csv1 but excluding the following headers Employment Category, Date of Birth Div Description columns, in other words with the following headers: Last Name      First Name      Dept Descrip      Social Security Numbers      Job Title      Work Phone      Room      Clock Number

Thanks for your help,
0
Comment
Question by:namerg
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Expert Comment

by:BT15
ID: 39185772
crossing my fingers that i have no typos
#replace file1.csv and file2.csv with the names/paths of your choosing


$file = import-csv file1.csv

$header = "Last Name,First Name,Dept Descrip,Social Security Numbers,Job Title,WorkPhone,Room,Clock Number"

$output = @(
$header
)

foreach ($line in $file){

$outline = $line.("last name") + "," + $line.("First name") + "," +$line.("Dept Descrip") + "," +$line.("Social Security Numbers") + "," +$line.("Job Title") + "," +$line.("WorkPhone") + "," +$line.("Room") + "," +$line.("Clock NUmber")

$output = @(
$ouput
$outline
)
}

$outline | sc file2.csv
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39185779
Import the source CSV, select the properties which you want and then export it to new one..
For example..
Import-csv c:\test.csv | Select "Last Name","First Name","Dept Descrip","Social Security Numbers","Job Title","Work Phone","Room","Clock Number" | Export-csv c:\testnew.csv -NTI

Open in new window

0
 

Author Comment

by:namerg
ID: 39185787
@subsun, I have not tested it is that easy ?
0
 
LVL 7

Expert Comment

by:BT15
ID: 39185793
lol where is my head at...

nicely done, Subsun
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 40

Expert Comment

by:Subsun
ID: 39185795
It should work.. just make sure you are typing the correct header names.
0
 

Author Comment

by:namerg
ID: 39185846
Sweet.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39185885
... and don't use the same file name for input and output ;-).
0
 

Author Comment

by:namerg
ID: 39185919
jajja, come on....I was aware of it. :P
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Synchronize a new Active Directory domain with an existing Office 365 tenant
This video discusses moving either the default database or any database to a new volume.
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…

707 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

14 Experts available now in Live!

Get 1:1 Help Now