Solved

PowerShell

Posted on 2013-11-27
260 Views
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
Question by:SURESH0518
• 3
• 3

LVL 40

Expert Comment

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  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])"
}

0

Author Comment

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

ID: 39681997
Which code you trying? first one or second?
0

Author Comment

ID: 39682007
Second one
0

LVL 34

Expert Comment

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  Save it as whatever.ps1 and run it as whatever.ps1 <original_file> <target_copy> <number_of_columns> 0 LVL 40 Accepted Solution 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 ",")
}
`
0

Author Closing Comment

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

Featured Post

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.
A brief introduction to what I consider to be the best editor for PowerShell.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!