Solved

PowerShell

Posted on 2013-11-27
7
262 Views
Last Modified: 2013-11-28
I have a text file which have two columns id,name as below
id|name
1|Test1
2|Test2
3|Test3

I want output as
id=1,name=Test1
id=2,name=Test2
id=3,name=Test3

How will I do in Windows PowerShell script. Any help is appreciated.
0
Comment
Question by:SURESH0518
  • 3
  • 3
7 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 39681952
I guess you want to convert it in to a csv..
GC C:\input.txt | Select -Skip 1 | % {
$test = $_ -split "\|" | %{$_.Trim()}
	New-Object PSObject -Property @{
	ID = $test[0]
	Name = $test[1]
	}
} | Export-Csv C:\out.csv -nti

Open in new window


But if you want exactly same as you written then try..
$outfile = "C:\Temp\out.txt"
Set-Content $outfile $null
GC c:\temp\input.txt | Select -Skip 1 | % {
$test = $_ -split "\|" | %{$_.Trim()}
Add-Content $outfile "ID=$($test[0]),Name=$($test[1])"
}

Open in new window

0
 

Author Comment

by:SURESH0518
ID: 39681991
Is there any way can I get column heading ID,name automatically without hard code. Sometimes I have two columns and sometimes I have 3 or more columns and I may not know column name upfront.
Example:
I may have
id|name|description
1|test|test1
2|test2|Test3

Sometime I may have
id|name|description|fundname
1|test|Test1|XXX
2|test2|Test3|YYY
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39681997
Which code you trying? first one or second?
0
Does Powershell have you tied up in knots?

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

 

Author Comment

by:SURESH0518
ID: 39682007
Second one
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39682051
Somewhat clumsy, but working:

Param (
[string]$originalName = "\path\to\file"
[string]$copyName = "\path\to\copy"
[Int32]$noColumns = 2
)
$arrHeader = @()
$header = (Get-Content $originalName -TotalCount 1).Split("|")
for ($i=0; $i -lt $noColumns; $i++) {
  $arrHeader += $header[$i]
}

$text = ""
foreach ($line in get-content $originalName) {
  $list = $line.Split("|")
  for ($i=0; $i -lt $noColumns; $i++) {
    $text = $text + $arrHeader[$i] + "=" + $list[$i] + ","
  }
  $text = $text.Substring(0, $text.Length - 1)
  $text = $text + "`n"
}
echo $text > $copyName

Open in new window


Save it as whatever.ps1 and run it as whatever.ps1 <original_file> <target_copy> <number_of_columns>
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39682095
Try this.. Script should dynamically detect the headings..
$outfile = "C:\Temp\out.txt"
$inputfile = "c:\temp\input.txt"

Set-Content $outfile $null

$head = (GC $inputfile | select -First 1) -split "\|" | %{$_.Trim()}

GC $inputfile | Select -Skip 1 | % {
$test = $_ -split "\|" | %{$_.Trim()}
 $Data = @()
	for ($i=0; $i -lt ($head | Measure).Count; $i++) {
	  $Data += "$($head[$i])=$($test[$i])"
	}
 Add-Content $outfile $($Data -join ",")
}

Open in new window

0
 

Author Closing Comment

by:SURESH0518
ID: 39683268
You are genius and thanks for quick solution and you are really great.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
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 …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

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