Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Batch: How remove all blank (or empty) lines

I am trying to remove all blank lines from a text file using a Windows batch program.

I know the simplest way do achieving this is bash is via regular expressions and the sed command:

sed -i "/^$/d" test.txt

Question: Does Windows batch have an similar simple method for removing all lines from a text file? Otherwise, what is the simplest method to achieving this?

Note: I'm running this batch script to setup new Windows computers for customers to use, and so preferably no additional programs need to be installed (and then unistalled) to achieve this - ideally, I'll just be using the "standard" batch library.

like image 652
andyandy Avatar asked Jan 21 '16 11:01

andyandy


People also ask

How do I delete all blank lines?

Click Search and then Replace. In the Replace window, in the Find what section, type ^\n (caret, backslash 'n') and leave the Replace with section blank, unless you want to replace a blank line with other text. Check the Regular Expression box. Click the Replace All button to replace all blank lines.

Which command will delete all the blank lines in the field old text?

The d command in sed can be used to delete the empty lines in a file.

What does 0 mean in batch?

%0 is the name of the currently executing batch file. A batch file that contains just this line: %0|%0. Is going to recursively execute itself forever, quickly creating many processes and slowing the system down.


1 Answers

For /f does not process empty lines:

for /f "usebackq tokens=* delims=" %%a in ("test.txt") do (echo(%%a)>>~.txt
move /y  ~.txt "test.txt"
like image 123
npocmaka Avatar answered Oct 07 '22 22:10

npocmaka