Bash question... is there a way to detect a possible presence of one character
I have a line like this:
Code:
sed -n /[hH][tT][tT][pP]:[/][/] Code:
sed -n /[hH][tT][tT][pP][sS]:[/][/] I'm just a person with illegally little knowledge of bash :redface::banghead: |
Quote:
Code:
sed 's/http\(.*\)/REPLACEMENT/gI' |
Quote:
I want to be able to catch http:// AND https:// but in a nicer way. I know I can do this by writing both of these lines but I was thinking there must be a nicer way for this. as the [sS] can catch both lower and uppercase 's', Id like something like this, if you get it: [sS[]] thus if it's an 's' OR 'S' OR not even present, the 'if-fi' it's inside will work. Did I get my thoughts thru? I'm more thinking of it as it's not a 'sed' thing, more of a general expression thing |
Quote:
|
Quote:
I can't test your "code" as I'm not sure how you're using "sed". When I echo something into what you posted I get a "missing command" error. (Perhaps my Sed Fu is weak.) |
Quote:
The line I want to find is http://yada OR https://yada in a nicer better looking way then just to the the "same" 'if-fi' twice. Quote:
I did try your '?' and '{0,1}' but nope. First one didn't work with "http", as I've changed the code now to use "[hH][tT][tT][pP][sS]", the second gave me errors: Code:
sed: can't read /[hH][tT][tT][pP][sS]1:[/][/].*[.][iI][mM][dD][bB][.].*.[0-9]/p: No such file or directory I just wanted to be 'c00l' and use some nicer approach. |
Quote:
The sed statement I gave you will replace (or delete, with a very slight modification), anything that is http or https, case-insensitive. That's all I could give you based on the lack of information. |
Quote:
Code:
http./i As has been stated, please tell us exactly what you're trying to accomplish, because finding a line and changing it are two different problems. |
I googled, sorry searched, for answers to a problem I have and found a script that I kinda "ripped" and did a very small change to, as...:
Code:
IUs="$(grep -a [Ii][Mm][Dd][Bb] $FILENAME | tr ' \|' '\n' | sed -n /[hH][tT][tT][pP]:[/][/].*[.][iI][mM][dD][bB][.].*.[0-9]/p | head -n 1 | tr -c -d '[:alnum:]\:./?')" If this file has multiple lines with https:// this script won't work, thus the IUs-variable will be null As I've said, I can fix it with an 'ugly' solution like copy of the 'if-fi' and it works but I want a fancier solution to it all, preferably a one-liner, thus the problem with the 'regex'. This is, for me, the perfect job to learn more of 'regex' as I know ~.6% of it. If this is NOT a regex solution, than I've learned that too. btw, this thread is closed so... you don't have to post, just wanted to let you know I'll go for another solution, but... you sure got my interest :p |
regular expressions are a subject in themselves and come in a few different flavors. perl regexps work a little differently than posix regexp.
Searches for 'regex' or 'regular expression' with and without the word 'syntax' will yield a plethora of reading material. Note there are also several 'checkers' on line which will help with experimentation and learning. Have fun! |
{0,1} and ? are ERE i.e. work in awk and php and perl and python, or in GNU sed with -r option.
Code:
sed -rn /[hH][tT][tT][pP][sS]?:[/][/] Code:
sed -n /[hH][tT][tT][pP][sS]\{0,1\}:[/][/] All of them mean: the preceding character(here [sS]) may exist 0 or 1 time. Compare with * that means: the preceding character may exist 0 or 1 or several times. |
All times are GMT -5. The time now is 02:05 AM. |