LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2021, 05:47 AM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 324

Rep: Reputation: Disabled
Deleting a word in variable not knowing if it will be capitalized or not


in this text :

Quote:
I Think John went to the JOHN house
i can use :
Code:
var1="I Think John went to the house"
var2=$(echo "$var1" | sed 's/\<JOHN\>//g' | sed 's/\<John\>//g')
echo "$var2"
On this next code will fail because the Capitalized john is not in variable .
Code:
var1="I Think John is fine"
var2=$(echo "$var1" | sed 's/\<JOHN\>//g' | sed 's/\<John\>//g')
echo "$var2"
However if in next text the 2nd john does not show up then how can i write the code for the 2 options no matter if one of them does no show up in variable?
 
Old 10-24-2021, 06:39 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,059

Rep: Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671
Depends on how specific the tests are - if only those 2, use alternation. If any variation of jOHn might arise, use a case-insensitive directive. See the doco from the GNU site or "info sed"; the manpage doesn't go this detail.
 
Old 10-24-2021, 08:51 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,037

Rep: Reputation: Disabled
There was a recent thread on case-insensitive matching in sed.
 
Old 10-25-2021, 06:28 PM   #4
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 324

Original Poster
Rep: Reputation: Disabled
Thanks
 
Old 10-26-2021, 02:21 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,434
Blog Entries: 4

Rep: Reputation: 3375Reputation: 3375Reputation: 3375Reputation: 3375Reputation: 3375Reputation: 3375Reputation: 3375Reputation: 3375Reputation: 3375Reputation: 3375Reputation: 3375
It's useful in general to read up on the topic of "regular expressions ('regexes')."
 
Old 10-27-2021, 08:42 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,852

Rep: Reputation: 3111Reputation: 3111Reputation: 3111Reputation: 3111Reputation: 3111Reputation: 3111Reputation: 3111Reputation: 3111Reputation: 3111Reputation: 3111Reputation: 3111
Of course on something so trivial, you don't even need something like sed:
Code:
x='I Think John went to the JOHN house'
echo "${x//[Jj][Oo][Hh][Nn]/}"
I Think  went to the  house
 
1 members found this post helpful.
Old 10-30-2021, 03:04 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,856

Rep: Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827Reputation: 827
And if you want to store it in a new variable, you can use a simple assignment
Code:
var1='I Think John went to the JOHN house'
var2=${var1//[Jj][Oo][Hh][Nn]/}
echo "$var2"
 
  


Reply

Tags
case-insensitive, 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
regex for phrase like'word-word-word' Zero4 Linux - General 9 07-06-2019 07:36 AM
Folder/Directory Names capitalized hstoellinger Linux - Newbie 6 08-17-2017 11:51 PM
Are you sure? [Y/n]. Why is the Y capitalized? superbbrr Linux - Newbie 2 05-21-2015 04:50 PM
[SOLVED] Keyboard input gets capitalized intermittently - why ? rtspitz Linux - General 7 06-26-2011 03:59 PM
1000+ Capitalized Directories Xerxes1121 Linux - General 1 06-17-2009 04:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:38 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
Open Source Consulting | Domain Registration