How to? Delete a line from a file

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#1
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
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.
 

rconn

Administrator
Staff member
May 14, 2008
10,100
85
#3
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?
Easily done -- see the TPIPE /selection filter.
 
#5
Easily done -- see the TPIPE /selection filter.
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