Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to? Delete a line from a file

Aug
1,917
68
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
 
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.
 
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.
 
Easily done -- see the TPIPE /selection filter.

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
 
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
 

Similar threads

Back
Top