LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Bash question... is there a way to detect a possible presence of one character (https://www.linuxquestions.org/questions/linux-software-2/bash-question-is-there-a-way-to-detect-a-possible-presence-of-one-character-4175669172/)

Basher52 02-07-2020 12:30 PM

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]:[/][/]
and it work perfect but I also want to be able to grab this:
Code:

sed -n /[hH][tT][tT][pP][sS]:[/][/]
I can do both lines but I guess there's a better way, it's Linux for cryin' out loud :p
I'm just a person with illegally little knowledge of bash :redface::banghead:

TB0ne 02-07-2020 12:47 PM

Quote:

Originally Posted by Basher52 (Post 6087326)
I have a line like this:
Code:

sed -n /[hH][tT][tT][pP]:[/][/]
and it work perfect but I also want to be able to grab this:
Code:

sed -n /[hH][tT][tT][pP][sS]:[/][/]
I can do both lines but I guess there's a better way, it's Linux for cryin' out loud :p
I'm just a person with illegally little knowledge of bash :redface::banghead:

This isn't so much a bash thing but a sed thing. You don't post the rest of your script/sed statement, but this:
Code:

sed 's/http\(.*\)/REPLACEMENT/gI'
will do a case-insensitive replace for http or https. Again, not sure what you're after because of incomplete information here.

Basher52 02-07-2020 01:07 PM

Quote:

Originally Posted by TB0ne (Post 6087336)
... Again, not sure what you're after because of incomplete information here.

If there is a chance to make both of the two lines in just one.

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

TB0ne 02-07-2020 01:19 PM

Quote:

Originally Posted by Basher52 (Post 6087343)
If there is a chance to make both of the two lines in just one.

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

Yes...we understand the question, but you haven't posted the rest of your script or even a complete sed statement. And I gave you a way to do this in my first reply.

rnturn 02-07-2020 01:22 PM

Quote:

Originally Posted by Basher52 (Post 6087326)
I have a line like this:
Code:

sed -n /[hH][tT][tT][pP]:[/][/]
and it work perfect but I also want to be able to grab this:
Code:

sed -n /[hH][tT][tT][pP][sS]:[/][/]
I can do both lines but I guess there's a better way, it's Linux for cryin' out loud :p
I'm just a person with illegally little knowledge of bash :redface::banghead:

A "{0,1}" -- or "?" -- qualifier on the "[sS]" pattern should match "0" or "1" occurrences of those characters? As in "[sS]{0,1}" or "[sS]?". Does that do the trick?

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.)

Basher52 02-07-2020 01:58 PM

Quote:

Originally Posted by TB0ne (Post 6087348)
Yes...we understand the question, but you haven't posted the rest of your script or even a complete sed statement. And I gave you a way to do this in my first reply.

Sorry if I'm being abrupt but as this must be an expression thing, I can't see the relevance of it as you won't be able to see the contents of the file that's being read.
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:

Originally Posted by rnturn (Post 6087349)
A "{0,1}" -- or "?" -- qualifier on the "[sS]" pattern should match "0" or "1" occurrences of those characters? As in "[sS]{0,1}" or "[sS]?". Does that do the trick?

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.)

I know, there's much more to it, it reads a file that has plenty of lines that holds either 'http' or 'https' and this is just a part of that line of code, so I know the 'code paste' gives an error.

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
Think I'm gonna go for the fast thing and just add two 'if-fi'.
I just wanted to be 'c00l' and use some nicer approach.

TB0ne 02-07-2020 03:16 PM

Quote:

Originally Posted by Basher52 (Post 6087367)
Sorry if I'm being abrupt but as this must be an expression thing, I can't see the relevance of it as you won't be able to see the contents of the file that's being read.
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.



I know, there's much more to it, it reads a file that has plenty of lines that holds either 'http' or 'https' and this is just a part of that line of code, so I know the 'code paste' gives an error.

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
Think I'm gonna go for the fast thing and just add two 'if-fi'.
I just wanted to be 'c00l' and use some nicer approach.

Your code, and two seds will work just fine...but again, we have NO IDEA about the rest of your bash script, or what you've written around it, or what you want to DO with the http(s). All we know is you posted incomplete sed statements, and were asked to explain more.

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.

scasey 02-07-2020 03:25 PM

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.
To find a line containing either http OR https, the regex is simply
Code:

http./i
where the lower-case i modifier makes the match case insensitive.

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.

Basher52 02-07-2020 04:35 PM

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:]\:./?')"
This line will in the file that's, attached/opened/read produce an output into the above named variable; 'IUs' ONLY!!! if it is 'http://', not 'https://'
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

scasey 02-07-2020 04:59 PM

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!

MadeInGermany 02-08-2020 04:09 PM

{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]?:[/][/]
\{0,1\} is BRE i.e. works with a standard sed.
Code:

sed -n /[hH][tT][tT][pP][sS]\{0,1\}:[/][/]
\? works in GNU sed's BRE mode as well.
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.