Contents
How to remove comment lines and empty lines in SED?
(3) Lastly, on OSX at least, the @paxdiablo solution in which the first clause turns comment lines into blank lines, and the second clause strips blank lines (including what were originally comments) doesn’t work. It seems to be more portable to make both clauses /d delete actions as I’ve done.
How to ignore comment lines when reading in a file?
If you are using a modern Python that has the withstatement, it is considered very good form to use with open(“filename”) as f:and then refer to the file object by f(or any other variable name you might choose). withwill make sure the file is closed, no matter what, even in the face of an exception.– stevehaNov 10 ’09 at 8:02
Is there a command to remove an empty line?
You can see that both commands are executed on each line. The line with a comment first has the comment removed then all is removed because it’s empty. In addition, the original empty line is also removed. @paxdiablo has a good answer but it can be improved. (1) The ‘/^$/d’ clause only matches 100% blank lines.
Is there such thing as blank lines in Python?
The above criteria may not be universal (e.g. within a HEREDOC block, or in a Python multi-line string the different approaches could be significant), but in many cases the conventional definition of “blank” lines include whitespace-only, and “comment” lines include whitespace-then- #.
How to comment a string in regex SED?
If the pattern you’re searching for does not include a “#” it will add it to the beginning of the line, and it will also add a trailing space. Another solution with the & special character which references the whole matched portion of the pattern space.
How to delete comments from the beginning of a line?
If you want to change it in place, add the -i switch: This will delete from any # to the end of the line, ignoring any context. If you use # anywhere where it’s not a comment (like in a string), it will delete that too. If comments can only start at the beginning of a line, do something like this:
Can a sed script save a file of line numbers?
Humorously (but perhaps slightly confusingly) you can do this with sed. This accepts a file of line numbers, one per line, and produces, on standard output, the same line numbers with d appended after each. This is a valid sed script, which we can save to a file, or (on some platforms) pipe to another sed instance:
Can a sed command be used as a semicolon?
But based on your comment that you want to do in-place editing, you can still do that with distinct commands ( sed commands rather than invocations of sed itself). You can either use multiple -e arguments or separate commands with a semicolon, something like (just one of these, not both):
How to delete lines that start with specified character?
Delete lines that begin with specified character ^ is to specify the starting of the line. Above sed command removes all the lines that start with character ‘u’. 11. Delete lines that end with specified character $ is to indicate the end of the line. The above command deletes all the lines that end with character ‘x’. 12.
Why does SED not work with POSIX regex?
It didn’t work with sed because it will by default use POSIX’s basic regular expressions, where the characters for grouping are \\ ( and \\), while ( and ) will match literal characters. There were no such brackets in your input text, so it didn’t match.
Is there a way to collapse multiple blank lines?
If you aren’t firing vim or sed for some other use, cat actually has an easy builtin way to collapse multiple blank lines, just use cat -s.
How to replace multiple blank lines with one new line?
Use to indicate a newline in the search pattern. Use Ctrl + M in the replacement text, or a backreference. See :help pattern and :help sub-replace-special (linked from :help :s ). The -s flag for cat squeezes multiple blank lines into one.