Delete lines with a script

This topic contains 10 replies, has 2 voices, and was last updated by  Emilio Marí 3 years, 6 months ago.

  • Author
    Posts
  • #13733

    Emilio Marí
    Participant

    I need to delete several lines in one operation based on several criteria. I know I can do it with only one crieria and I need three sentences but not with several and only one piece of code. For example I want to delete all lines that may content (name,street, city) any one of these names. One line may contain only the name, the other line may content only the city.
    regards

  • #13735

    Robert Westerlund
    Participant

    And how is that going for you?

    Please add the code you have now, explain what you have tried and describe the errors you observe.

  • #13736

    Emilio Marí
    Participant

    I only know how to code this
    #elimina las líneas que contienen localhost
    New-Item -ItemType file "file6.log" -force
    Select-String 'name' .\file4.log -notmatch | % {$_.Line} | set-content file6.log

    #elimina las líneas que contienen local
    New-Item -ItemType file "file7.log" -force
    Select-String 'city' .\file6.log -notmatch | % {$_.Line} | set-content file7.log

    but I do like something like Select-String 'name' or 'city'

  • #13738

    Robert Westerlund
    Participant

    If you read the Get-Help section on Select-String you can see that the Pattern parameter is an array of strings.

    Select-String [-Pattern]  ...

    This means that you can provide an array of strings to search for. In the examples help section for Select-String (the one you get if you run Get-Help Select-String -Examples) there is even an example of searching for any of two different strings:

    -------------------------- EXAMPLE 7 --------------------------
    
    PS C:\>select-string -path process.txt -pattern idle, svchost -notmatch
    
    
    This command finds lines of text in the Process.txt file that do not include the words "idle" or "svchost".
    
  • #13739

    Emilio Marí
    Participant

    Many thanks Robert. I will try. I used to code in visual basic years ago. No, retired, I am starting with Powershell to automate some code with a defined task. I should read the get-help first.Regards

  • #13740

    Robert Westerlund
    Participant

    The Get-Help in PowerShell is really wonderful. Both the Get-Help sections on each cmdlet which contain both documentation on the cmdlet and the cmdlet parameters as well as examples of usage. On top of that there are loads of Get-Help about_* sections (122 on my Win8.1 machine) which can be used for learning about powershell (e.g. about_WMI, about_scopes, about_Functions, about_Functions_Advanced). It's a really good resource!

  • #13741

    Emilio Marí
    Participant

    Thanks again. What I tried and got it with better or worse code is to automate the update of the HOSTS file based in two addresss. I just did it manually and with a task.And ¡It Works! The task is what matters.
    here inclosed the text file with my project. It can help somebody else. I promise I will have a look to your references.Kind Regards Emilio

  • #13743

    Emilio Marí
    Participant

    I also attach cabeza.txt

  • #13745

    Emilio Marí
    Participant

    attached cabeza.txt

  • #13747

    Robert Westerlund
    Participant

    I can honestly say that I don't understand your message. Is it working? Is it not working? If it isn't, what is not working and what errors do you observe?

    This also seems unrelated to the original reason for this thread. Could you please start a new discussion thread with a description of your new problem?

  • #13752

    Emilio Marí
    Participant

    Yes Robert. The code you send me works very well and with it also the whole program. I just wanted to share the whole code for you to see. Kind regards. No other help needed for the moment.

You must be logged in to reply to this topic.