?
Solved

PowerShell

Posted on 2013-11-27
7
Medium Priority
?
268 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:SURESH0518
ID: 39682007
Second one
0
 
LVL 35

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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

770 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