How to? Delete a line from a 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? 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
 
May 20, 2008
3,515
4
Elkridge, MD, USA
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
12,367
150
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.

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
Thread starter Title Forum Replies Date
A Installers delete EVerything service if already installed Support 0
Chen Touboul When i try to delete an empty folder i got "the dirctoy name is invalid" Support 3
I zip /M fails to delete file after adding to zip file Support 2
D SYNC /W copies back instead of propagating a delete Support 2
M Delete multiple Alternate Datastreams Support 5
D safe delete files command Support 10
P How to? Delete subfolders but not the folder itself Support 11
P Can't do something to a file and then delete it? Support 6
nickles WAD del unable to delete files ending in "." Support 18
gschizas Fixed Cannot use extended path to delete invalid file Support 3
M Can't delete files... Support 8
aedthuio REG ADD/DELETE Support 1
vefatica Cut/delete Support 3
D TCMD16 Delete Toolbar Button bug Support 7
G FOLDERMONITOR created also firing for delete (and maybe other events) Support 7
T How to? Select and Delete text Support 6
CWBillow Delete based on dates Support 5
JohnQSmith How to? Delete descriptions Support 2
vefatica How do I delete "spaces.txt "? Support 21
A How to? Instruct COPY to delete file if only partially copied (via FTP) Support 8
M Why am I unable to delete files? Support 8
R Delete command sets directory attributes to hidden & system Support 46
S 4NT - delete from command prompt and recycle bin Support 12
Phileosophos COPY /W does not delete files in the target Support 4
T Possible to delete the new standard tab? Support 1
P Sync /w won't delete in ftp folder Support 1
T VIEW only works from command line Support 14
vefatica `Back quotes` - command line vs. batch file Support 5
Phileosophos Is there any way to open the Take Command options dialog from the command line? Support 8
Phileosophos Command-line Editing Shortkeys That Fail Support 6
D How to use multi-line DO to loop through first level directory names Support 5
D Open Windows 10 Photos app from command line Support 11
Peter Murschall Single-line Do-CMD is a bit uncooperative. Support 6
Jesse Heines Strange Line Wrapping Behavior Support 14
fpefpe How to? command echo in title line/bar Support 2
vefatica Command line DO with no closing parenthesis? Support 9
R Long commands retrieved from command history have blank line when wrapped Support 20
M No line wraps if last line of the screen buffer is reached Support 5
Joe Caverly No blank line in TEXT...ENDTEXT in a LIBRARY function Support 8
R CMDebug, dark theme, current line Support 7
vefatica IDE - when breakpoint is on a blank line? Support 3
D Double Quote in TC Tool Bar Button Command line Support 3
Joe Caverly Expand all aliases on the command line Support 2
vefatica How do I do this with a multiple-line DO? Support 7
Jay Sage Cannot Postion Cursor in Command Line Using Mouse Support 7
vefatica Windows 10 and line wrapping selection? Support 6
vefatica IDE.EXE's command line? Support 19
x13 TPIPE /REPLACE - Remove line (including EOL) Support 3
D btm file command line augments comparing for string or math. Support 12
D Grabbing html text with @line Support 3

Similar threads