Reputation: 35
Hi I am having trouble to understand why this regex:
(candy)?(?(1)A| is false)
for this String: A candy is true is false
is not matching the A
, and instead is matching is false
What I'm really trying to do is, for example, if I have a Windows in this string:
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) is false.
I want to substitute all this string to just Windows
using sed command in Bash. If the string doesn't contain Windows
, it's supposed to just replace with -
.
The bash command I'm trying to use is:
sed -i 's/(?(?=Windows)("Mozilla.*)|(?!))/Windows/' accesslog.txt
Upvotes: 1
Views: 496
Reputation: 626728
First of all, sed
's POSIX regex flavor does not support conditional constructs. What you want to do has nothing to do with regex conditional constructs as all they do is match further text depending on the previous group match, or text immediately before or after the current location.
You want something like
#!/bin/bash
s='Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) is false.
some other line'
sed 's/.*\(Windows\).*/\1/; t; s/.*/-/' <<< "$s"
See the online demo. Output:
Windows
-
The 's/.*\(Windows\).*/\1/; t; s/.*/-/'
command means:
s/.*\(Windows\).*/\1/
- replace the whole line that contains Windows
(captured into Group 1 with a capturing group) with the Group 1 valuet
- jump to the end of the command upon successful substitutions/.*/-/
- else, replace the whole line with -
.Upvotes: 1