Solved

PowerShell

Posted on 2013-11-27
7
259 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
Comment Utility
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
Comment Utility
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
Comment Utility
Which code you trying? first one or second?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:SURESH0518
Comment Utility
Second one
0
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
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
Comment Utility
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
Comment Utility
You are genius and thanks for quick solution and you are really great.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now