Why can't I get to grips with scripting ? I just want to delete certain text

Discussion in 'Scripting' started by tnx, Aug 31, 2015.

  1. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,618
    232
    60
    Hi.

    dumby tn here wanting to learn summet which I thought would be easy but I just keep getting it wrong.

    I have a .txt file with lots of entries in and I want to delete all instances of a certain text.
    I can not even get the basics down.

    For example I have a .txt file with

    RED
    CAR
    BLUE
    CAR
    GREEN
    CAR

    I don't want the word CAR in my list, but say CAR appears 20 times or more and I am too lazy to manually delete it.
    I have tried commands, powershell, even next doors dog, but he's just barking mad.:eek:

    I just can not get the hang of it..

    Please help. It must be easy, right ?:confused:
     
  2. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    5,766
    13,247
    180
    ... but he's just barking mad LOL

    Alright for this chores I make use of notepad++
     
  3. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,618
    232
    60

    Why Mr.X you suggestion works well.

    I have just downloaded notepad++.
    Was not sure on how to do but I made a simple text file to test on, opened it up, searched for the text I did not want. Chose "replace" but left the replace tab empty.
    text went away.

    how cool.

    I think I will have to test this out on my file I want to amend..

    :D
     
  4. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    5,766
    13,247
    180
    I know notepad++ is powerful... :D
     
  5. hearywarlot

    hearywarlot MDL Member

    Jul 31, 2015
    112
    109
    10
    #5 hearywarlot, Sep 9, 2015
    Last edited by a moderator: Apr 20, 2017
    If you also want the newlines to be gone after replacing (as not to create empty lines), do the following in Notepad++ or your favorite editor with regex support:
    • Open the replace text window
    • In the section 'Search Mode' left below Select the option 'Extended'
    • In 'Find what' write your to remove text, but end it with '\r\n'
    Code:
    i.stack.imgur.com/r22P5.png
    A '\r' is a control character for 'carriage return' and '\n' for 'line break', combining them for newline or so called 'CRLF'.

    After done, i recommend to set 'Search Mode' back to 'Normal' to not confuse later if you need to replace a literal '\r\n'.

    Have a nice day ^^.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,618
    232
    60
    Thanks for that info. :cool:

    Once I had removed the text I did not want I just used the notepad++ to remove all empty lines.
    A little bit of manual work but by god it works a treat.
     
  7. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    872
    457
    30
    #7 KNARZ, Sep 11, 2015
    Last edited by a moderator: Apr 20, 2017
    1)
    Code:
    type yourtext.txt | find /i /v "Line you want to exclude" > cleared_yourtext.txt
    2)
    Code:
    find /i /v "Line you want to exclude" "X:\Path\yourtext.txt" > cleared_yourtext.txt
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. Kamrul08

    Kamrul08 MDL Member

    Dec 28, 2013
    101
    12
    10
    #8 Kamrul08, Oct 12, 2015
    Last edited by a moderator: Apr 20, 2017
    Good code KNARZ. I think findstr command is better than find. I used findstr command like bellow.

    @echo off
    ren "About Me.txt" About_Me.txt
    findstr /v /i /c:"Car" /c:"Red" About_Me.txt >"About Me.txt"
    del About_Me.txt

    The command above will recreate the file deleting the line with Car and Red.
     
  9. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    872
    457
    30
    just for the record you can also just use "Car Red" space means sperator. ;)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...