1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to? Delete a line from a file

Discussion in 'Support' started by Joe Caverly, May 1, 2013.

  1. Joe Caverly

    Joined:
    Aug 28, 2009
    Messages:
    680
    Likes Received:
    8
    Hi,
    The Unix utility SED provides the ability to delete a specific line from a file. For example, if I want to delete line 44 from my hosts file, saving the new file with a .bak extension, I can do so with the following SED command;

    Code:
    sed -i".bak" '44d' hosts
    Is there a simple way to do this in TCC? At present, I am doing this in TCC as follows;

    Code:
    @setlocal
    @echo off
    head /n43 hosts > hosts.bak
    head /n+44 /n%@lines[hosts] hosts >> hosts.bak
    endlocal
    
    and it achieves the same result as SED, but I was hoping for a one-liner.

    Yes, I could create a SED.btm, and parse command line arguments to do the same thing, but before I do, thought I would ask.

    Thanks from Joe
     
  2. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    Sorry, no such feature. However, your procedure can be simplified - use

    tail /n+44 hosts >> hosts.bak

    to get the tail end of the source file. You could create an alias [UNTESTED!!!]:

    alias drop_line=`(head /n%@DEC[%1] %2 %+ tail /n+%1 %2) > %2.bak`

    Invoke it as:

    drop_line line_#_to_be_dropped source_file
    It creates source_file.bak

    A more sophisticated version would allow specifying a set of consecutive lines to be dropped, and a target file.
     
  3. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,860
    Likes Received:
    83
    Easily done -- see the TPIPE /selection filter.
     
  4. Joe Caverly

    Joined:
    Aug 28, 2009
    Messages:
    680
    Likes Received:
    8
    Thanks Rex! Wow, that was easy, just like you said. To delete line 652 from a file of 291,071 lines, using

    Code:
    tpipe /input=hosts /output=hosts.bak /selection=6,0,652,652,0,0,"",0
    took only 0:00:01.01

    Joe
     
  5. Joe Caverly

    Joined:
    Aug 28, 2009
    Messages:
    680
    Likes Received:
    8
    I was using FFIND to search for the text on the line that I wanted to delete. Using the /L option of FFIND returned the line number that I wanted to delete. I then used that line number with the TPIPE command to quickly remove that line from the hosts file.

    After doing some more reading on TPIPE, I see that I can remove the line just by passing in the text that I want to delete. So now, my batch file is thus;

    Code:
    @setlocal
    @echo off
    do Text2Find in @deadhosts.txt
      echo Removing %Text2Find
      tpipe /input=hosts /output=hosts.bak /grep=5,0,0,0,0,0,0,0,"%Text2Find"
      if exist hosts del hosts > nul
      ren hosts.bak hosts > nul
    enddo
    endlocal
    
    Much faster, less code.

    Joe
     

Share This Page