<p>I have a string which starts with <code>//#...</code> goes upto the newline characater. I have figured out the regex for the which is this <code>..#([^\n]*)</code>.</p>

<p>My question is how do you remove this line from a file if the following condition matches</p>

[{'answer_id': 80444, 'author': 'EricSchaefer', 'author_id': 8976, 'author_profile': 'https://Stackoverflow.com/users/8976', 'pm_score': 0, 'selected': False, 'text': "<p>Read the file line by line and only write those lines to a new file that don't match the regex.\nYou cannot just remove a line.</p>\n"}, {'answer_id': 80445, 'author': 'baudtack', 'author_id': 10738, 'author_profile': 'https://Stackoverflow.com/users/10738', 'pm_score': 0, 'selected': False, 'text': "<p>Does it start at the begining of a line or can it appear anywhere? If the former s/old/new is what you want. If the latter, I'll have to figure that out. I suspect that back referances could be used somehow.</p>\n"}, {'answer_id': 80498, 'author': 'bmb', 'author_id': 5298, 'author_profile': 'https://Stackoverflow.com/users/5298', 'pm_score': 0, 'selected': False, 'text': "<p>I don't think your regex is correct.</p>\n\n<p>First you need to start with ^ or else it will match this pattern anywhere on the line.</p>\n\n<p>Second, the <code>..</code> should be <code>\\/\\/</code> or else it will match any two characters.</p>\n\n<p><code>^\\/\\/#[^\\n]*</code> is probably what you want.</p>\n\n<p>Then do what EricSchaefer says and read the file line by line only writing lines that don't match.</p>\n\n<p>--<br>\nbmb</p>\n"}, {'answer_id': 80703, 'author': 'Pat', 'author_id': 238, 'author_profile': 'https://Stackoverflow.com/users/238', 'pm_score': 0, 'selected': False, 'text': "<p>Try the following:</p>\n\n<pre><code>perl -ne 'print unless m{^//#}' input.txt &gt; output.txt\n</code></pre>\n\n<p>If you are using windows you need double quotes instead of single quotes.</p>\n\n<p>You can do the same with grep</p>\n\n<pre><code>grep -v -e '^//#' input.txt &gt; output.txt\n</code></pre>\n"}, {'answer_id': 80711, 'author': 'David Precious', 'author_id': 4040, 'author_profile': 'https://Stackoverflow.com/users/4040', 'pm_score': 0, 'selected': False, 'text': '<p>Iterate over each line in the file, and skip the line if it matches the pattern:</p>\n\n<pre>\nmy $fh = new FileHandle \'filename\'\n    or die "Failed to open file - $!";\n\nwhile (my $line = $fh->getline) {\n    next if $line =~ m{^//#};\n    print $line;\n}\nclose $fh;\n</pre>\n\n<p>This will print all lines from the file, except the line that starts with \'//#\'.</p>\n'}, {'answer_id': 80791, 'author': 'Aeon', 'author_id': 13289, 'author_profile': 'https://Stackoverflow.com/users/13289', 'pm_score': 1, 'selected': False, 'text': "<p>You really don't need perl for this.</p>\n\n<pre><code>sed '/^\\/\\/#/d' inputfile &gt; outputfile\n</code></pre>\n\n<p>I &lt;3 sed.</p>\n"}, {'answer_id': 80853, 'author': 'kixx', 'author_id': 11260, 'author_profile': 'https://Stackoverflow.com/users/11260', 'pm_score': 3, 'selected': False, 'text': '<p>To filter out all the lines in a file that match a certain regex:</p>\n\n<pre><code>perl -n -i.orig -e \'print unless /^#/\' file1 file2 file3\n</code></pre>\n\n<p>The \'.orig\' after the -i switch creates a backup of the file with the given extension (.orig). You can skip it if you don\'t need a backup (just use -i).</p>\n\n<p>The -n switch causes perl to execute your instructions (-e \' ... \') for each line in the file. The line is stored in $_ (which is also the default argument for many instructions, in this case: print and regex matching).</p>\n\n<p>Finally, the argument to  the -e switch says "print the line unless it matches a # character at the start of the line.</p>\n\n<p>PS. There is also a -p switch which behaves like -n, except the lines are always printed (good for searching and replacing)</p>\n'}, {'answer_id': 80948, 'author': 'Aristotle Pagaltzis', 'author_id': 9410, 'author_profile': 'https://Stackoverflow.com/users/9410', 'pm_score': 6, 'selected': True, 'text': '<p>Your regex is badly chosen on several points:</p>\n\n<ol>\n<li><p>Instead of matching two slashes specifically, you use <code>..</code> to match two characters that can be anything at all, presumably because you don’t know how to match slashes when you’re also using them as delimiters. (Actually, dots match <em>almost</em> anything, as we’ll see in #3.)</p>\n\n<p>Within a slash-delimited regex literal, <code>//</code>, you can match slashes simply by protecting them with backslashes, eg. <code>/\\/\\//</code>. The nicer variant, however, is to use the longer form of regex literal, <code>m//</code>, where you can choose the delimiter, eg. <code>m!!</code>. Since you use something other than slashes for delimitation, you can then write them without escaping them: <code>m!//!</code>. See <a href="http://p3rl.org/op#Quote-and-Quote-like-Operators" rel="noreferrer">perldoc perlop</a>.</p></li>\n<li><p>It’s not anchored to the start of the string so it will match anywhere. Use the <code>^</code> start-of-string assertion in front.</p></li>\n<li><p>You wrote <code>[^\\n]</code> to match “any character except newline” when there is a much simpler way to write that, which is just the <code>.</code> wildcard. It does exactly that – match any character except newline.</p></li>\n<li><p>You are using parentheses to group a part of the match, but the group is neither quantified (you are not specifying that it can match any other number of times than exactly once) nor are you interested in keeping it. So the parentheses are superfluous.</p></li>\n</ol>\n\n<p>Altogether, that makes it <code>m!^//#.*!</code>. But putting an uncaptured <code>.*</code> (or anything with a <code>*</code> quantifier) at the end of a regex is meaningless, since it never changes whether a string will match or not: the <code>*</code> is happy to match nothing at all.</p>\n\n<p>So that leaves you with <code>m!^//#!</code>.</p>\n\n<p>As for removing the line from the file, as everyone else explained, read it in line by line and print all the lines you want to keep back to another file. If you are not doing this within a larger program, use perl’s command line switches to do it easily:</p>\n\n<pre><code>perl -ni.bak -e\'print unless m!^//#!\' somefile.txt\n</code></pre>\n\n<p>Here, the <code>-n</code> switch makes perl put a loop around the code you provide which will read all the files you pass on the command line in sequence. The <code>-i</code> switch (for “in-place”) says to collect the output from your script and overwrite the original contents of each file with it. The <code>.bak</code> parameter to the <code>-i</code> option tells perl to keep a backup of the original file in a file named after the original file name with <code>.bak</code> appended. For all of these bits, see <a href="http://p3rl.org/run" rel="noreferrer">perldoc perlrun</a>.</p>\n\n<p>If you want to do this within the context of a larger program, the easiest way to do it safely is to open the file twice, once for reading, and separately, with <a href="http://p3rl.org/IO::AtomicFile" rel="noreferrer">IO::AtomicFile</a>, another time for writing. IO::AtomicFile will replace the original file only if it’s successfully closed.</p>\n'}, {'answer_id': 81383, 'author': 'arclight', 'author_id': 13366, 'author_profile': 'https://Stackoverflow.com/users/13366', 'pm_score': 2, 'selected': False, 'text': '<p>As others have pointed out, if the end goal is only to remove lines starting with <code>//#</code>, for performance reasons you are probably better off using <code>grep</code> or <code>sed</code>:</p>\n\n<pre><code>grep -v \'^\\/\\/#\' filename.txt &gt; filename.stripped.txt\n\nsed \'/^\\/\\/#/d\' filename.txt &gt; filename.stripped.txt\n</code></pre>\n\n<p>or</p>\n\n<pre><code>sed -i \'/^\\/\\/#/d\' filename.txt\n</code></pre>\n\n<p>if you prefer in-place editing.</p>\n\n<p>Note that in perl your regex would be</p>\n\n<pre><code>m{^//#}\n</code></pre>\n\n<p>which matches two slashes followed by a # at the start of the string.</p>\n\n<p>Note that you avoid "backslashitis" by using the match operator <code>m{pattern}</code> instead of the more familiar <code>/pattern/</code>. Train yourself on this syntax early since it\'s a simple way to avoid excessive escaping. You could write <code>m{^//#}</code> just as effectively as <code>m%^//#%</code> or <code>m#^//\\##</code>, depending on what you want to match. Strive for clarity - regular expressions are hard enough to decipher without a prickly forest of avoidable backslashes killing readability. Seriously, <code>m/^\\/\\/#/</code> looks like an alligator with a chipped tooth and a filling or a tiny ASCII painting of the Alps.</p>\n\n<p>One problem that might come up in your script is if the entire file is slurped up into a string, newlines and all. To defend against that case, use the /m (multiline) modifier on the regex:</p>\n\n<pre><code>m{^//#}m\n</code></pre>\n\n<p>This allows ^ to match at the beginning of the string <em>and</em> after a newline. You would think there was a way to strip or match the lines matching <code>m{^//#.*$}</code> using the regex modifiers <code>/g</code>, <code>/m</code>, and <code>/s</code> in the case where you\'ve slurped the file into a string but you don\'t want to make a copy of it (begging the question of why it was slurped into a string in the first place.) It <em>should</em> be possible, but it\'s late and I\'m not seeing the answer. However, one \'simple\' way of doing it is:</p>\n\n<pre><code>my $cooked = join qq{\\n}, (grep { ! m{^//} } (split m{\\n}, $raw));\n</code></pre>\n\n<p>even though that creates a copy instead of an in-place edit on the original string <code>$raw</code>.</p>\n'}]
['https://Stackoverflow.com/questions/80415', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13046/']