hi everyone! hope you're all staying safe. idk whe...
# random
a
hi everyone! hope you're all staying safe. idk whether this is the best place to ask, but i've been trying to figure out a sed command in bash for the past hour or so, and could do with some guidance! I was wondering if anyone could explain what
sed 's/\\?/n/g'
means please? I understand the basic principles of
sed
but can't quite figure out what the
\\?
means exactly. thanks in advance!
a
As
?
is used to define a quantifier, e.g. the expression before that is found zero or one times, you have to escape it to find it literally in your string you want to replace πŸ™‚
a
thank you so much! this was a really helpful explanation πŸ™‚
a
(sed syntax is a bit different on various systems, sometimes a single
\?
is enough .. πŸ€” )
No problem πŸ‘
p
I think the first
\
is escaping the second
\
isin't it?
So this isn't searching for a literal
?
, but rather zero or more
\
characters
πŸ‘ 2
a
It could mean then a literal \ ?
m
yes, I think it is replacing
\
with
n
πŸ‘ 1
a
(been looking things up on the sed man page)
p
https://regex101.com/ I think that sed usually follows PCRE?
πŸ‘ 1
a
It does, just that
sed
on OSX behaves a bit differently than on other Unix systems πŸ˜…
Ok double checked it
Copy code
echo "bla\\?" | sed 's/\\?/n/g' 
bla\n
πŸ€” 1
m
I get
blan
both on osx and centos
a
Same here, maybe that was just a thing in the past and should be forgotten: https://unix.stackexchange.com/questions/13711/differences-between-sed-on-mac-osx-and-other-standard-sed
m
I was just confused about the remaining
\
in your output
a
Copy code
echo "bla\?" | sed 's/\\?/n/g'
bla\n
m
(and that it removed the
?
also for me)
πŸ™ˆ 1
a
Bildschirmfoto 2020-03-31 um 15.45.34.png
m
Screenshot 2020-03-31 15.46.23.png
a
lol
OSX?
m
centos
a
(OSX latest here)
m
but also on OSX mojave
a
Catalina here
a
Does it also depend on what quotes you use, i.e. single or double?
a
Yes
πŸ‘ 1
m
okay, I get alex’s results if I use double quotes…
😌 1
a
need to be careful with sed, my personal take home message here πŸ˜†
πŸ˜† 1
p
This thread is hilarious πŸ˜†
πŸ˜‚ 2
a
Also very useful! Really grateful for such a swift answer πŸ™‚