Delete certain lines in a txt file via a batch file - string

I have a generated txt file. This file has certain lines that are superfluous, and need to be removed. Each line that requires removal has one of two string in the line; "ERROR" or "REFERENCE". These tokens may appear anywhere in the line. I would like to delete these lines, while retaining all other lines.
So, if the txt file looks like this:
Good Line of data
bad line of C:\Directory\ERROR\myFile.dll
Another good line of data
bad line: REFERENCE
Good line
I would like the file to end up like this:
Good Line of data
Another good line of data
Good line
TIA.

Use the following:
type file.txt | findstr /v ERROR | findstr /v REFERENCE
This has the advantage of using standard tools in the Windows OS, rather than having to find and install sed/awk/perl and such.
See the following transcript for it in operation:
C:\>type file.txt
Good Line of data
bad line of C:\Directory\ERROR\myFile.dll
Another good line of data
bad line: REFERENCE
Good line
C:\>type file.txt | findstr /v ERROR | findstr /v REFERENCE
Good Line of data
Another good line of data
Good line

You can accomplish the same solution as #paxdiablo's using just findstr by itself. There's no need to pipe multiple commands together:
findstr /V "ERROR REFERENCE" infile.txt > outfile.txt
Details of how this works:
/v finds lines that don't match the search string (same switch #paxdiablo uses)
if the search string is in quotes, it performs an OR search, using each word (separator is a space)
findstr can take an input file, you don't need to feed it the text using the "type" command
"> outfile.txt" will send the results to the file outfile.txt instead printing them to your console. (Note that it will overwrite the file if it exists. Use ">> outfile.txt" instead if you want to append.)
You might also consider adding the /i switch to do a case-insensitive match.

If you have sed:
sed -e '/REFERENCE/d' -e '/ERROR/d' [FILENAME]
Where FILENAME is the name of the text file with the good & bad lines

If you have perl installed, then perl -i -n -e"print unless m{(ERROR|REFERENCE)}" should do the trick.

It seems that using the FIND instead of the FINDSTR can support also unicode characters.
e.g. type file.txt | find /v "Ω"

Related

SED or other editor - remove strings from file on Windows

I need to find a string in a textfile, delete the line containing it, and save the file. The string is found (read from) another textfile, containing hundreds of different strings, one per row. The process is to go on from the first to the last string in the file.
Any (hopefully easy to use) text editors (on Windows OS) recommended ? To achive the task.
I am not into serious day-to-day editing. So I'd be ever so happy if the task could be accomplished with a easy-to-use but still reliable editor.
Thanks a bunch,
Frank
You can try notepad++ since it has a lot of plugins, also a great search algorithm. I did a similar task where I had to do a lot of search/replace stuff, and used a plugin I dug up from the internet, can't remember the name exactly (try google-ing I think it's replaacc for notepad++ or something similar).
On unix/linux/cygwin:
grep -v -f pattern_file unmodified_file > new_file
Remove all lines containing the patterns in pattern_file from unmodified_file, write to new_file.
grep -v outputs lines not matching any pattern. -f reads patterns from a file.
On windows this appears equivalent to running this at the command prompt:
FINDSTR /V /G:pattern_file unmodified_file > new_file
That's it. If you already have the two source files, it's a one-liner.
pattern_file is going to be whitespace and case sensitive unless you delve into other options, which are described with FINDSTR /?
Using sed:
sed -n '/PATTERN/n;p' FILE > FILE.new # then copy FILE.new to FILE
Tells sed to not output anything by default (-n), find the pattern (/PATTERN/) and skip this line if found (n), otherwise print the line (;p). If you have GNU sed you do can just call
sed -i -n '/PATTERN/n;p' FILE`
which automatically updates the file due to (-i /--in-place).

Display entire line containing certain characters in Linux

I am wondering if there is a way to display the entire line in a data file containing specific characters in linux? For example searching for "577789999" in a file.txt should display me the line such as below
577789999 adef YTM 777888
that's what grep is for
grep 577789999 file.txt
you might want to restrict the pattern to occur only in the beginning of the line:
grep ^577789999 file.txt
Generaly, you might use grep like that :
grep "the researched string" "filename"
It'll tells you at which line you're string is, and if you search it on "*" (All files in the current dir), it tells you in which file you're string is ;)

How to write a batch file to find a string in a text file and push it into a new text file

i want to write a batch file to find a particular string in a file. If the string is found then i want to redirect the whole line which contains the string to another file. for ex:
suppose a file myfile.txt contains the following text
abcwerthfdh
qwerewtretywr
weqreqwrtabcwerwe
wqerweqabcqwewq
when i launch the batch file giving myfile.txt and abc as command line arguments, then the output should be into a file called newfile.txt and it should contain only the lines which contain the text "abc". if i run this code again it should append to newfile.txt and not delete existing contents. in this case it should push the lines 1, 3 and 4 into newfile.txt
#echo off&setlocal
for /f "delims=" %%a in ('findstr "%~2" "%~1"') do (echo(%%a)>>newfile.txt
You could use FINDSTR:
FINDSTR abc myfile.txt >> newfile.txt
type myfile.txt | findstr /n "abc" >> newfile.txt
???

Batch file to find all files in a directory containing an HTML string then output list to a text file

I've made several attempts at this but get nothing but "can't open..." errors, so I'm asking here:
I want to find all instances of the string "SOME TEXT" within a directory full of HTML files. Then the search results should be output to a file in that same directory (D:\myfiles)
Here's a sample batch file that'll do the trick.
#echo off
setlocal
pushd D:\myfiles
rem case-insensitive search for the string "SOME TEXT" in all html files
rem in the current directory, piping the output to the results.txt file
rem in teh same directory
findstr /ip /c:"SOME TEXT" *.html > results.txt
popd
endlocal
Update: Some caveats to using findstr command.
If your string contains angle brackets, you have to escape them using the CMD escape character - ^. So, if you want to search for <TITLE>, you have to specify it as /c:"^<TITLE^>".
If you want only file names, change /ip to /im. Also, you can add /s to search subfolders. In general, you can play with the different findstr options as listed in findstr /?.
Findstr will find the text only in UTF-8 encoded files. If the HTML files are UTF-16 encoded (ie, each character takes two bytes), findstr will not find the text.
I would also suggest running the command without the piping to the results.txt first to get the right findstr options and make sure it outputs what you need.
for %%f in (*.html) do findstr /i /m /p /c:"SOME TEXT" "%%f" >> results.txt

Move lines from one .txt file to another

I am trying to move certain lines from one .txt file to another. These lines all follow a certain pattern. I have been looking at using the find command in a batch file, but this does not delete the line from the original file.
For example:
find \i pattern "d:\example1.txt" >> "d:\example2.txt"
Is there any way to achieve this?
Thanks in advance.
Using findstr you can print lines that don't match, too. So you can do it in several steps, psudocoded like this:
findstr pattern input > output
findstr /v pattern input > input-inverse
move /y input-inverse input
This should leave you with all lines matching pattern in output, and an input without those lines.
EDIT: Made the last step use move with an option to overwrite, so no need to remove the input before. I guess I (being mainly a Linux person) think of "rename" and "move" as the same thing, and took that overwrite for granted. So, thanks for the heads-up.
If you can use external programs, one way would be using awk or sed.
Awk example:
awk /pattern/ { print }
Sed example:
sed '/inverse_pattern/ d' //Deletes lines which do not match
How about creating two files, then replacing the original?
find \i pattern "d:\example1.txt" >> "d:\example2.txt"
find \i antipattern "d:\example1.txt" >> "d:\example3.txt"
del example1.txt
ren example3.txt example1.txt
Deleting lines from files is hard. Typically, even in a genuine programming environment, you'd be using an extra file here.
Here's a slightly different implementation:
ren example1.txt source.txt
find \i pattern "d:\source.txt" >> "d:\example2.txt"
find \i antipattern "d:\source.txt" >> "d:\example1.txt"
del source.txt

Resources