Monday, February 6, 2012

Part-4: Text Manipulation in PowerShell using .Replace(), and .Remove() methods.


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
06-02-2012 12-48-52
Replace                       
Using replace we can replace the text,symbols of our strings.  The syntax is simple Replace(“Old Character”,”new character”)
First lets replace “Aman” with some other name , for example with “SAM”,"
$text.Replace("Aman","Sam")
You can see in below screenshot that  text “aman.dhally@xyz.com” is replaced with “Sam.Dhally@xyz.com

06-02-2012 12-52-28 
Secondly lets replace “.” with a blank spaces
$text.Replace("."," ")
In the screenshot you can se that all “.” are replaced with the “ “ a black space.
06-02-2012 12-55-42
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
06-02-2012 13-01-11
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

06-02-2012 13-21-38 
$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.
06-02-2012 13-22-55
$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 .
06-02-2012 13-25-15
there is another example of Remove() method. 
$text.Remove(4,7)
In this example. first you can see our original text “Aman.Dhally@xyz.com”, in second we are keeping the first 4 characters of the text and removing everything else, so the output is “Aman”, in third we are keeping first 4 characters of the text "which is “AMAN”, and then removing next 7 characters of the text which is “.Dhally”  and then the output is “Aman@xyz.com

06-02-2012 13-34-04

Video:
















Thanks for reading
Aman Dhally

18 comments:

  1. Hello how can i remove the last 2 digits of my string:

    [string]$text = "Archive_Testebene1_20120228_124556"

    $text.Length
    $text.Remove(4)


    With remove it only remove the first 4

    ReplyDelete
    Replies
    1. Hi Jan
      To remove the last 2 digits try
      $text.Remove(32)

      Delete
    2. It is not working. Please tell how to remove some starting characters

      Delete
    3. $text.substring(2) will remove the first 2 characters

      Delete
  2. Hi...I have one problem..Maybe you could help me :)
    I 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

    ReplyDelete
    Replies
    1. Hi Alex,

      I think that can be done {not sure} , can you give me few sample file names.

      thanks
      aman

      Delete
    2. [string[]]$pdfFileNames = @"
      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
      "@ -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

      Delete
  3. Hi Aman,

    Thanks 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

    ReplyDelete
  4. Hi Aman,
    Great 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.

    ReplyDelete
    Replies
    1. Hi Russ,

      Thanks . can you try this .


      $csv = Import-Csv D:\D.csv -Delimiter ","



      foreach ( $name in $csv ) {


      $myName = $name.ccoursenam + $name.teachname

      $myName



      }

      Delete
  5. 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
    abcd : aajjnnnaa
    ab: qwsxx
    mmmjkkkoo : lopppkk has to be

    abcd
    ab
    mmjkkkoo

    ReplyDelete
  6. Hi,

    I 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.

    ReplyDelete
  7. Hi Aman
    I 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?

    ReplyDelete
  8. Thanks, great post. Helped me out :)

    ReplyDelete
  9. Hi Aman, I would like to do following.

    20141001-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.

    ReplyDelete
  10. hi Aman can u help me with the syntax to replace double quote with /"

    ReplyDelete
  11. Hello

    How can I get the first 7 letters of a string? The strings can vary in size from 2 to 15 characters long.

    ReplyDelete
  12. Hi Guys

    Nice article it helpful;

    ReplyDelete

Note: Only a member of this blog may post a comment.