I am trying to get matched non-numerical strings on new line with sed
So, if I have string abc def 123 (ghi)
, I want output to be:
(abc)
(def)
(ghi)
This is what I have tried:
echo "abc def 123 (ghi)" | sed -r 's/([a-z]+)/(\1)\n/g'
But this outputs following:
(abc)
(def)
123 ((ghi)
)
I am quite confused here. Have many doubts: Why there is leading space on line 2 and 3? Why double bracket ghi
? Why 123
is not eliminated? Why, enclosing bracker came individually on last line?
Update
Actually, I wanted to extract URLs from specific domain. So using suggestions in comments and answer, I tried below:
in="https://www.example.com/user1 ddsf none http://www.example.com/user2 kbu7f7yy"
echo $in | sed 's/http[s]*:\/\/www.example.com\/[^ ]*/&\n/g'
This printed following:
https://www.example.com/user1
ddsf none http://www.example.com/user2
kbu7f7yy
So, I tried this (as suggested in one )
echo $in | sed 's/.*\(http[s]*:\/\/www.example.com\/[^ ]*\).*/\1\n/g'
But I ended up getting:
http://www.example.com/user2