Issue
Here is a file that contains:
+
-
+ <>cow apple</>
- apple
+ ball
+ +
- -
+ -
- +
+ !
-
-
+
+ $
+ **
+ *
+ =
+ #
- ?
- ◊
+ ◊◊
-
-
+ <>cow apple</>
- apple
+ ball
+ +
- -
+ -
- +
+ !
+ $
+ **
+ *
+ =
+ #
- ?
- ◊
+ ◊◊
How to remove line that starts with + or - followed by empty space only?
Here is code which gives expected result but better solution would be very helpful. Since I am running this cmd on large file and has to be accurate.
sed ‘/^[^[:alnum:]]* $/d’
Solution
You may use this grep
with -v
(inverse) option:
grep -v '^[-+][[:blank:]]*$' file
+ <>cow apple</>
- apple
+ ball
+ +
- -
+ -
- +
+ !
+ $
+ **
+ *
+ =
+ #
- ?
- ◊
+ ◊◊
Here:
^[-+][[:blank:]]*$
: Matches a line starting with-
or+
followed by 0 or more whitespaces till end.
Following awk
or sed
solutions would also work:
sed '/^[-+][[:blank:]]*$/d' file
awk '!/^[-+][[:blank:]]*$/' file
Answered By - anubhava Answer Checked By - Timothy Miller (WPSolving Admin)