LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-07-2020, 12:30 PM   #1
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: CentOS, Arch, Mint, Zorin, Slax
Posts: 351

Rep: Reputation: 21
Question 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
I'm just a person with illegally little knowledge of bash
 
Old 02-07-2020, 12:47 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 22,444

Rep: Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100
Quote:
Originally Posted by Basher52 View Post
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
I'm just a person with illegally little knowledge of bash
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.
 
Old 02-07-2020, 01:07 PM   #3
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: CentOS, Arch, Mint, Zorin, Slax
Posts: 351

Original Poster
Rep: Reputation: 21
Quote:
Originally Posted by TB0ne View Post
... 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

Last edited by Basher52; 02-07-2020 at 01:09 PM.
 
Old 02-07-2020, 01:19 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 22,444

Rep: Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100
Quote:
Originally Posted by Basher52 View Post
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.
 
Old 02-07-2020, 01:22 PM   #5
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: Currently: openSUSE, Raspbian, Slackware. Formerly: CentOS, MacOS, Red Hat. Other: Solaris, Tru64
Posts: 1,779

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by Basher52 View Post
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
I'm just a person with illegally little knowledge of bash
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.)
 
Old 02-07-2020, 01:58 PM   #6
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: CentOS, Arch, Mint, Zorin, Slax
Posts: 351

Original Poster
Rep: Reputation: 21
Quote:
Originally Posted by TB0ne View Post
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 View Post
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.
 
Old 02-07-2020, 03:16 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 22,444

Rep: Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100Reputation: 6100
Quote:
Originally Posted by Basher52 View Post
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.
 
Old 02-07-2020, 03:25 PM   #8
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.7.1908
Posts: 4,545

Rep: Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628
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.
 
1 members found this post helpful.
Old 02-07-2020, 04:35 PM   #9
Basher52
Member
 
Registered: Mar 2004
Location: .SE
Distribution: CentOS, Arch, Mint, Zorin, Slax
Posts: 351

Original Poster
Rep: Reputation: 21
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
 
Old 02-07-2020, 04:59 PM   #10
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.7.1908
Posts: 4,545

Rep: Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628Reputation: 1628
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!
 
Old 02-08-2020, 04:09 PM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,329

Rep: Reputation: 607Reputation: 607Reputation: 607Reputation: 607Reputation: 607Reputation: 607
{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.

Last edited by MadeInGermany; 02-08-2020 at 04:17 PM.
 
  


Reply

Tags
bash, regular expression, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any reason why a usb wireless dongle would be affected by the presence of a usb disk? hazel Linux - Networking 9 12-26-2016 07:01 AM
Writing a file character by character with a bash builtin command (script). stf92 Linux - Newbie 4 06-30-2012 08:41 PM
Bash scripting: parsing a text file character-by-character Completely Clueless Programming 13 08-12-2009 09:07 AM
Detect presence of a file s8utt Linux - Newbie 6 05-20-2009 05:41 AM
how to detect floppy presence Shirley Linux - General 1 05-15-2002 07:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration