Solved

PowerShell

Posted on 2013-11-27
7
260 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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

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…

862 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

24 Experts available now in Live!

Get 1:1 Help Now