Hi,
I have text file which has lot of character entries one line after another.
I want to find all lines which start with ::
and delete all those lines.
What is the regular expression to do this?
-AD
Hi,
I have text file which has lot of character entries one line after another.
I want to find all lines which start with ::
and delete all those lines.
What is the regular expression to do this?
-AD
^::.*[\r\n]*
If you're reading the file line-by-line you won't need the [\r\n]*
part.
Regular expressions don't "do" anything. They only match text.
What you want is some tools that uses regular expressions to identify a line and then apply some command to those tools.
One such tools is sed
(there's also awk
and many others). You'd use it like this:
sed -e "/^::/d" < input.txt > output.txt
The part "/^::/
" tells sed
to apply the following command to all lines that start with "::" and "d
" simply means "delete that line".
Or the simplest solution (which my brain didn't produce for some strange reason):
grep -v "^::" input.txt > output.txt
If you don't have sed or grep, find this and replace with empty string:
^::.*[\r\n]
Thanks for the pointers:
Following thing worked for me. After "::" any character was possiblly present in the text file so i gave:
^::[a-zA-Z0-9 I put all punctuation symbols here]*$
-AD
Here's my contribution in C#:
Text stream:
string stream = :: This is a comment line
Syntax:
Regex commentsExp = new Regex("^::.*", RegexOptions.Singleline);
Usage:
Console.WriteLine(commentsExp.Replace(stream, string.Empty));
Alternatively, if I wanted to simply take a text file that included comments and produce an exact duplicate without the comment lines I could use a simple but effective combination of the type and findstr commandline tools:
type commented.txt | findstr /v /R "^::" > uncommented.txt