14

I want to append several strings in a data set with custom strings.

Example

Content of Dataset:

Test1
Test2
Test3

Result after appending:

Test1.com
Test2.com
Test3.com

Would I have to use regex to parse to the end of each Test[n] to be able to append it with a custom string (.com)? Has anyone got an example that describes exactly how to do it?

I am reading from a SQL-Table and writing values into a DataSet which is exported to CSV the following way:

$DataSet.Tables[0] | ConvertTO-Csv -Delimiter ',' -NotypeInformation |`% { $_ -replace '"','' } |  out-file  $outfile -Encoding "unicode"

The DataSet contains of Strings such as:

Banana01
Banana02
Apple01
Cherry01
Cherry02
Cherry03

The thing I want to do is append .com to only Cherry01, Cherry02, and Cherry03, and after appending .com, export it as a CSV file.

TylerH
  • 20,799
  • 66
  • 75
  • 101
t1red
  • 175
  • 1
  • 2
  • 8

3 Answers3

20

There are many ways. Here are a few:

# Using string concatenation
'Test1','Test2','Test3' | Foreach-Object{ $_ + '.com' }

# Using string expansion
'Test1','Test2','Test3' | Foreach-Object{ "$_.com" }

# Using string format
'Test1','Test2','Test3' | Foreach-Object{ "{0}{1}" -f $_,'.com' }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • Thank you. Could you give me an example related to the code that I have posted? – t1red Jul 25 '12 at 13:08
  • What are the column headers for each value you want to append to? – Shay Levy Jul 25 '12 at 13:12
  • All the strings I want to append got the header "vmname" – t1red Jul 25 '12 at 14:20
  • Try something like: ... | Foreach-Object{ $_.vmname + '.com' } – Shay Levy Jul 25 '12 at 16:19
  • $DataSet.Tables[0] | ConvertTO-Csv -Delimiter ',' -NotypeInformation |`% { $_ -replace '"','' }| Foreach-Object {$T=$_.vmname if($T -match "ESX\d\d" -or $T -match "ESX\d\d\d") {$T + '.com'} Else{$T} } | out-file $outfile -Encoding "unicode" is giving me an empty result. – t1red Jul 25 '12 at 16:50
  • If vmname is a column defined in the table[0] :$DataSet.Tables[0] | Foreach-Object{ $_.vmname + '.com' } – Shay Levy Jul 26 '12 at 08:14
  • If I am doing it like this, I am receiving a CSV file with strange content like this: #TYPE System.string "Length" "18" "18 "18" "9" "9" and so on – t1red Jul 30 '12 at 12:12
  • But, do you get the correct values before you pipe to the csv file? If so, try this: $DataSet.Tables[0] | Foreach-Object{ $_.vmname=$_.vmname + '.com'; $_ } – Shay Levy Jul 30 '12 at 12:21
  • yes, I was getting correct results before piping the CSV. It's working the way you wrote it, but now every element in the vmname colum is being appended with .com. How do I integrate this into an IF query, so that only name starting with "Cherry" are getting appended with.com? – t1red Jul 30 '12 at 12:57
  • $DataSet.Tables[0] | Where-Object{ $_.vmname -eq 'Cherry'} | Foreach-Object {$_.vmname = $_.vmname + '.com'; $_ } – Shay Levy Jul 30 '12 at 13:09
  • On a second thought, the previous example will pass on just objects who's vmname equals cherry. Use this instead: $DataSet.Tables[0] | foreach { if($_.vmname -eq 'Cherry'){$_.vmname = $_.vmname + '.com'; $_ } else {$_} } – Shay Levy Jul 30 '12 at 15:16
  • excellent! Instead of "-eq" I was using "-match" since I used regular expressions.. This line is what I am using now: $DataSet.Tables[0] | foreach { if($_.vmname -match '^Cherry\d\d'){$_.vmname = $_.vmname + '.com'; $_ } else {$_} } | ConvertTO-Csv -Delimiter ',' -NotypeInformation |`% { $_ -replace '"','' }| out-file $outfile -Encoding "unicode" – t1red Jul 31 '12 at 06:46
9

You could use something like this:

Example 1

$t = "test"
$t = $t + ".com"

Example 2

$test = @("test1","test2")

$test | ForEach-Object {
$t = $_ + ".com"
Write-Host $t}

With your added code I did this. I don't have a database to test it on, so I made the data set manually, so you might just have to change the $DataSet[0] in my code to $DataSet.Tables[0].

$DataSet[0] | ConvertTO-Csv -Delimiter ',' -NotypeInformation  | Foreach-Object{$T=$_
IF($T -match "(Cherry\d\d)"){$T = $T -replace "(Cherry\d\d)(.+)",'$1.com$2'};$T } |  out-file  $outfile -Encoding "unicode"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
justinf
  • 1,246
  • 2
  • 19
  • 39
  • Thank you. I have posted some code in my post, would be cool if u could have a look at it. – t1red Jul 25 '12 at 13:07
  • Its hard to test cause i dont have access to sql , so just check it out and if its not correct i can try sort it out for you. – justinf Jul 25 '12 at 13:23
  • Thanks. But I think this way each string will be appended, no matter what it is starting with.. But I only want to append those with .com which start with "Cherry#", as I posted in the edit in my initial post. – t1red Jul 25 '12 at 14:24
  • Ok sorry did not see that part try the code now after i edited it,the edited code check if any thing is called cherry + two digits e.g cherry00,cherry01,cherry99 – justinf Jul 25 '12 at 14:49
  • Thank you. I have tested it and it seems to work but what if I am having something like this in the DataSet: Cherry01,fruit,yummy then .com will be attached to yummy what makes it Cherry01,fruit,yummy.com. What if I want to insert it only after the name Cherry01 so it looks like: Cherry01.com,fruit,yummy – t1red Jul 25 '12 at 15:17
  • It looks for any thing that is **cherry** + 2 digits. It would not do any thing to **yummy** .But if you only ever want to change Cherry01 then just change this "(Cherry\d\d)" to be "(Cherry01)" – justinf Jul 25 '12 at 15:28
  • yes, but if it is a row like this: Cherry01,fruit,yummy the .com is attached to the end of the line.. in this case it's Cherry01,fruit,yummy.com. Sorry for being so special about it, but I really want to learn it :) – t1red Jul 25 '12 at 15:35
  • I am not sure could you post all the code and an example of the table in sql so i could try it because in my example $DataSet="Cherry01,fruit,yummy" and the code would check each one – justinf Jul 25 '12 at 17:19
0

$array | %{if($_ -match "^Cherry\d\d"){$_ += ".com"};$_}

SpellingD
  • 2,533
  • 1
  • 17
  • 13