Solved

How to create a csv from another csv in powershell ?

Posted on 2013-05-21
8
303 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
[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
  • 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
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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 …
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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…
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…

733 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