none
How to search and add new string in the same line were search string is found

    Question

  • Hi ,

    Please can  you advise how to achieve below:

    I have text file in which already few lines are present and i have to add new text line string  on the text file if a string found and it should at same line :

    for example below are contents of my file:

    google.com

    tibco.com

    msn.com

    i have to search all the lines and for all values and upon the search if text file has above values present then at respective places it should add the new lines i.e on same line in which search string is found.

    Please could you advise.


    Umeed4u


    • Edited by Umeed4u Wednesday, April 19, 2017 11:13 AM
    Wednesday, April 19, 2017 11:12 AM

Answers

  • Please read up on compare operators and conditional scripts.

    $textfile = "C:\Temp\MyTextFile.txt"
    
    $contents = (get-content $textfile)
    
    foreach ($line in $contents){
        if (($line -match "google.com") -and ($line -notmatch "google.com abc")){
            $line -replace "google\.com","google.com abc"
        }
        elseif (($line -match "msn.com") -and ($line -notmatch "msn.com xyz")){
            $line -replace "msn\.com","msn.com xyz"
        }
        elseif (...){
            ...
        }
    }

    If you want to make it more concise, read up on regular expressions and use regex in the replace part.

    Thursday, April 20, 2017 9:43 AM

All replies

  • Hi,

    Wouldn't a simple -replace action suffice?

    Start with something as follows:

    $textfile = "C:\Temp\MyTextFile.txt"
    
    $contents = (get-content $textfile)
    
    foreach ($line in $contents){
    
        $line -replace "google\.com","google.com is a search engine"
        $line -replace "thomas", "thomas is a tank engine"
    
    }
    
    $contents | out-file $textfile -force

    Wednesday, April 19, 2017 11:20 AM
  • Hi Pawet,

    I don't want to replace but to add or prepend new string  in the same line where my search string is found.


    Umeed4u

    Wednesday, April 19, 2017 11:36 AM
  • Use $_ to include entire input string in the new string:

    $textfile = "C:\Temp\MyTextFile.txt"
    
    $contents = (get-content $textfile)
    
    foreach ($line in $contents){
    
        $line -replace "google\.com","$_ is a search engine"
        $line -replace "thomas", "$_ is a tank engine"
    
    }
    
    $contents | out-file $textfile -force

    If you have more specific patterns that you'd like to replace, prepend or append, regular expressions are definitely the way to go.

    Wednesday, April 19, 2017 11:40 AM
  • Hi Pawet,

    Can you please let me know how to achieve it in regular expression?

    Prepend and append any thing will work


    Umeed4u

    Wednesday, April 19, 2017 11:51 AM
  • Please try the suggestions above on a small batch of data; simply replace a string with that string (marked by $_) and whatever you like before and/or after it.
    Wednesday, April 19, 2017 11:54 AM

  • can you share any sample example,Pls.


    Umeed4u

    Wednesday, April 19, 2017 12:00 PM
  • Hi ,

    Please can  you advise how to achieve below:

    I have text file in which already few lines are present and i have to add new text line string  on the text file if a string found and it should at same line :

    for example below are contents of my file:

    google.com

    tibco.com

    msn.com

    i have to search all the lines and for all values and upon the search if text file has above values present then at respective places it should add the new lines i.e on same line in which search string is found.

    Please could you advise.


    Umeed4u

    • Merged by jrv Wednesday, April 19, 2017 2:55 PM Duplicate
    Wednesday, April 19, 2017 12:31 PM
  • I am trying below method but not working. Pls advise.

    $RESULT=GET-CONTENT C:\Temp\MyTextFile.txt

    $prependVar = "some text    "

    FOREACH ($LINE in $RESULT) 
              { 

              if ($line.tostring() –eq "google" ) 
                   { 
                   Write-Host $LINE
                   $newlines = $line + "$prependVar$_"
                   write-host ( $newlines ) 
                   $newlines | Out-File $file
                   } 
              }

             

    Umeed4u


    • Edited by Umeed4u Wednesday, April 19, 2017 1:17 PM
    Wednesday, April 19, 2017 1:15 PM
  • I am trying below method but not working. Pls advise.

    tried belwo method but no luck.any advise pls

    $RESULT=GET-CONTENT C:\Temp\MyTextFile.txt

    $prependVar = "some text    "

    FOREACH ($LINE in $RESULT) 
              { 

              if ($line.tostring() –eq "google" ) 
                   { 
                   Write-Host $LINE
                   $newlines = $line + "$prependVar$_"
                   write-host ( $newlines ) 
                   $newlines | Out-File $file
                   } 
              }


    Umeed4u

    Wednesday, April 19, 2017 1:16 PM
  • # file to read
    $sourceFile = "C:\temp\myTextFile.txt"
    # destination file to write with updated text
    $destFile = "C:\temp\myTextFileUpdated.txt"
    # array of text that need to be searched
    $SearchText = @('google.com','tibco.com','msn.com')
    # text to prepend during search
    $prependText = 'some text '
    # read source text file in a variable for updation
    $text = Get-Content $sourceFile -Raw
    
    # search and prepend specified text and write updated text to dest file
    $searchText | foreach { $text = $text -replace $_,"$prependText $_" }
    $text | out-file -FilePath $destFile


    cheers..uc

    Wednesday, April 19, 2017 2:31 PM

  • it looks good but one more thing is that if i have to add different string for each search value

    for example

    if google.com is found then add  "abc"

    if msn.com found then add   "xyz"

    etc like that for other search values  and if already abc or xyz is added then don't add it again.

    on each run it puts a space at the start of search text.How to avoid that?


    Umeed4u

    Thursday, April 20, 2017 6:43 AM
  • one way to achieve what you're trying to do is to use another array which will store text that need to be prepended for each search text (apart from various other ways).

    I would suggest getting good grasp of arrays and how string search/replace works in powershell.. here are some links and do google search to find more ways to learn them

    https://mcpmag.com/articles/2017/04/06/working-with-arrays-in-powershell.aspx

    https://blogs.technet.microsoft.com/heyscriptingguy/2011/03/21/use-powershell-to-replace-text-in-strings/


    cheers..uc

    Thursday, April 20, 2017 8:25 AM
  • Please read up on compare operators and conditional scripts.

    $textfile = "C:\Temp\MyTextFile.txt"
    
    $contents = (get-content $textfile)
    
    foreach ($line in $contents){
        if (($line -match "google.com") -and ($line -notmatch "google.com abc")){
            $line -replace "google\.com","google.com abc"
        }
        elseif (($line -match "msn.com") -and ($line -notmatch "msn.com xyz")){
            $line -replace "msn\.com","msn.com xyz"
        }
        elseif (...){
            ...
        }
    }

    If you want to make it more concise, read up on regular expressions and use regex in the replace part.

    Thursday, April 20, 2017 9:43 AM
  • Please can  you advise how to achieve below:

    I have text file in which already few lines are present and i have to add new text line string  on the text file if a string found and it should at same line :

    for example below are contents of my file:

    google.com

    tibco.com

    msn.com

    i have to search all the lines and for all values and upon the search if text file has above values present then at respective places it should add the new lines i.e on same line in which search string is found.

    if google.com is found then add  "abc"

    if msn.com found then add   "xyz"

    etc like that for other search values  and if already abc or xyz is added then don't add it again.

    i tried belwo method but no luck.any advise pls

    $RESULT=GET-CONTENT C:\Temp\MyTextFile.txt

    $prependVar = "some text    "

    FOREACH ($LINE in $RESULT) 
              { 

              if ($line.tostring() –eq "google" ) 
                   { 
                   Write-Host $LINE
                   $newlines = $line + "$prependVar$_"
                   write-host ( $newlines ) 
                   $newlines | Out-File $file
                   } 
              }

    Please could you advise.


    Umeed4u

    • Merged by jrv Friday, April 21, 2017 9:59 AM Duplicate
    Friday, April 21, 2017 9:50 AM