Reputation: 775
I have two regex values that I have been trying to combine into a single statement however it is not working properly. I need to be able to display lines that have comments in them or start with a blank space and then have the comments.
ie.
/* comment
/* comment
sed -n "/\(^[/*]\).*\([*/]$\)/ p" testfile
sed -n "/^\s/ p" testfile
I have been trying using the | statement however it is not working.
Is it possible to merge them into one or do I need to run them separately.
Thanks
Upvotes: 0
Views: 117
Reputation: 20002
You can use grep:
grep "^ */\*" testfile
^=start of line First * is repeating the space 0 or more times Second star is escaped, so that is a real one.
Upvotes: 1
Reputation: 3363
As the other answers illustrate, it is often possible to find a regular expression general enough to match everything your original expressions matched. In fact, this is always possible, since you can always combine two expressions like this:
(regex_1|regex_2|...|regex_n)
(Depending on the dialect you use, you might have to use backslashes.) However, a sed
program may consist of several statements, so your example could be simply rewritten as:
sed -n "/\(^[/*]\).*\([*/]$\)/p /^\s/p" testfile
Upvotes: 1
Reputation: 98901
You can try :
/^[ ]*(\/\*.*)$/
The regex above will match any lines with comments, that can start with spaces, and capture the line to a group. Check the Regex Demo
Regex Explanation
/^[ ]*(\/\*.*)$/
^ assert position at start of a line
[ ]* match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
the literal character
1st Capturing group (\/\*.*)
\/ matches the character / literally
\* matches the character * literally
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
$ assert position at end of a line
Upvotes: 1