Part-1: Text Manipulation in PowerShell using Trim, TrimStart , TrimEnd methods
Part-2: Text Manipulation in PowerShell using .ToLower and .ToUpper method
Part-3: Text Manipulation in PowerShell using .StartsWith() and EndsWith() methods
Hi,
Today we are using .replace and .remove methods to play with the string. Lets start. Today i am using some fake email id as text.
$text = Aman.Dhally@xyz.com
$text.Replace("Aman","Sam")
$text.Replace("."," ")
In the screenshot you can se that all “.” are replaced with the “ “ a black space.
lets assume that we have a text file which contain list of all users email id in our “XYZ” company and we want to remove “@xyz.com” and keep the text before “@xyz.com” .
$text.Replace("@xyz.com"," ")
and here is the our output :) ,showing only the text before “@xyz.com”
Remove
Before doing anything else first lets check the length of our text using Length property. The syntax for remove method is to define to keep the number of characters and remove everything else. Not getting my point.. let ,me show to you.
Our text have 19 characters in it.
$text.Length
$text.Remove(4)
You can see that i mentioned 4 characters to keep and remove everything else. and the output is “Aman” which are exactly 4 characters.
$text.Remove(11)
Now i am keeping 11 characters of the string and removing everything else. and you can see that the output is “Aman.Dhally” which are exactly 11 characters .
there is another example of Remove() method.
$text.Remove(4,7)
Video:
Thanks for reading
Aman Dhally
Hello how can i remove the last 2 digits of my string:
ReplyDelete[string]$text = "Archive_Testebene1_20120228_124556"
$text.Length
$text.Remove(4)
With remove it only remove the first 4
Hi Jan
DeleteTo remove the last 2 digits try
$text.Remove(32)
It is not working. Please tell how to remove some starting characters
Delete$text.substring(2) will remove the first 2 characters
DeleteHi...I have one problem..Maybe you could help me :)
ReplyDeleteI have a folder with PDF files in this format...
date_number_email.pdf where date=8 digits , number = 13 digits
I have to create two CSV files...
1. files.csv with sheet files with two columns, A and B where column A will contain date_email.pdf and column B will contain number
2. files.csv with sheet files with two columns, A and B where column A will contain date_email.pdf and column B will contain email
Thanks in advance
Aleksandar
Hi Alex,
DeleteI think that can be done {not sure} , can you give me few sample file names.
thanks
aman
[string[]]$pdfFileNames = @"
Delete20120911_0712978450876_a.blabla@abcd.com.pdf
20120911_1908985424739_a.surname@abcd.com.pdf
20120911_0111973455023_brasno45@yahoo.com.pdf
20120911_2912971455987_m.forfor@abcd.com.mk.pdf
20120911_3110981484777_r.byebye@gmail.com.pdf
20120921_0212984415877_a_igor@hotmail.com.pdf
20120921_2704975455205_i.lalala@abcd.com.pdf
"@ -split "`n"
$pdfObject = foreach ($pdfFileName in $pdfFileNames)
{
$fileParts = $pdfFileName.Split("_",3)
New-Object -Type PSObject -Property @{
Date = $fileParts[0]
Number = $fileParts[1]
Email = $fileParts[2]
}
}
$pdfObject |
Select-Object -Property @{Name = "Date_Email";
Expression = {$_.Date + "_" + $_.Email}},Number |
Export-Csv -Path C:\Users\Owner\Tmp\File1.csv
$pdfObject |
Select-Object -Property @{Name = "Date_Email";
Expression = {$_.Date + "_" + $_.Email}},Email |
Export-Csv -Path C:\Users\Owner\Tmp\File2.csv
Hi Aman,
ReplyDeleteThanks for the reply...
20120911_0712978450876_a.blabla@abcd.com.pdf
20120911_1908985424739_a.surname@abcd.com.pdf
20120911_0111973455023_brasno45@yahoo.com.pdf
20120911_2912971455987_m.forfor@abcd.com.mk.pdf
20120911_3110981484777_r.byebye@gmail.com.pdf
20120921_0212984415877_a_igor@hotmail.com.pdf
20120921_2704975455205_i.lalala@abcd.com.pdf
I think this is all you need to create the csv files.
This is done by command dir /b (I am sure you know :) )
BR
Alex
Hi Aman,
ReplyDeleteGreat work and it answered some questions that I had. I am trying to create a script that will do the following. Reads data from a CSV specifically the teachnam column. The field consists of the users full last name, and the first initial of there first name. It will split this data and combine them to be first initial and last name combined. The longest this field should be is 6 characters. It can be shorter but it cannot be longer. is an example of the csv.
teachname,ccoursenam,csection,crefno
Smith, J,Algebra II,3,J210
Johnson, A,Earth Science,2,S103
I am looking for the script to return
Smith, J as JSmith
Johnson, A as AJohns
Here is what I have for code, and I am recieving errors about jsmith not being the correct length.
ForEach ($entry In $CSV)
{
#This line reads the teachname field from the CSV and removes the #space
$teachname = $entry.teachname -replace '\s+', ""
#This line splits the name up
$TeachSplit = $TeacherName.split(",")
#This line rearranges in correct order
$TeachFull = $TeachSplit[1] + $teachSplit[0]
#These next 2 lines Read the length and remove everything after the #6th character
$TeachFull.length
$TeacherID = $TeachFull.Remove(6)
}
Any help would be great.
Hi Russ,
DeleteThanks . can you try this .
$csv = Import-Csv D:\D.csv -Delimiter ","
foreach ( $name in $csv ) {
$myName = $name.ccoursenam + $name.teachname
$myName
}
Hi Aman i stuck with a problem let's say i have a file with almost 50 lines in it every lines have colon inside a phrase i need to delete or remove all the lines after the colon, ie
ReplyDeleteabcd : aajjnnnaa
ab: qwsxx
mmmjkkkoo : lopppkk has to be
abcd
ab
mmjkkkoo
Hi,
ReplyDeleteI need to add a word in a text file. I have bunch of lines in that text file and somewhere in the middle, i have a line like this and i need one more server name at end of the line.
$new - "Server10"
$Watchers = @( "Server1","Server2","Server3")
Please suggest me the script.
Hi Aman
ReplyDeleteI have a column with firstnames and lastnames from a csv file
the firstname column has multiple firstnames.
How would i construct the Remove function so that it removes all other words after the first space.
E.g. my firsname is Amy May Zan. and the last name is Hakim.
How do i just make it Amy.Hakim@email.com?
Thanks, great post. Helped me out :)
ReplyDeleteHi Aman, I would like to do following.
ReplyDelete20141001-2568-C2568-TRX (L)-169082523u.xml rename this file to ABN-2568-C2568-TRX.xml
I've 7 files under one folder with similar naming convention.
let me know best way to do this.
Thank you.
hi Aman can u help me with the syntax to replace double quote with /"
ReplyDeleteHello
ReplyDeleteHow can I get the first 7 letters of a string? The strings can vary in size from 2 to 15 characters long.
Hi Guys
ReplyDeleteNice article it helpful;