Solved

How to create a csv from another csv in powershell ?

Posted on 2013-05-21
8
302 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 7

Expert Comment

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

nicely done, Subsun
0
 
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 69

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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