LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-30-2017, 08:12 AM   #1
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Rep: Reputation: 0
sub-string to be converted into Capital


Hello Guys,

Is it possible to turn a sub string/partial string into capital locks?

Code:
echo "AB-System-Check-ping"
AB-System-Check-ping

echo "AB-System-Check-http"
AB-System-Check-http
Result should be
AB-System-Check-PING
AB-System-Check-HTTP

Not sure if its going to each or hard? maybe grep and then use tr ?
The problem is I have several like these in one file so need something smarter. Further if I have multiple files with such conditions I should be able to use the same technique.
 
Old 04-30-2017, 08:53 AM   #2
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
You could use sed with your file as input to output the appropriate translated characters.

e.g.
Code:
sed 's/-ping/-PING/' <filename
 
Old 04-30-2017, 11:08 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
You could do it in bash with a little bash substitution:
Code:
str="AB-System-Check-http"
end=${str##*-}
str="${str%-*}-${end^^}"

echo "$str"
Easy enough to then make that process into a little function
 
1 members found this post helpful.
Old 04-30-2017, 05:41 PM   #4
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by JeremyBoden View Post
You could use sed with your file as input to output the appropriate translated characters.

e.g.
Code:
sed 's/-ping/-PING/' <filename
It could be anything say ping, ssh, sftp, Total-processes, Swap-Usage,Cpu-Usage
 
Old 04-30-2017, 05:59 PM   #5
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
You could do it in bash with a little bash substitution:
Code:
str="AB-System-Check-http"
end=${str##*-}
str="${str%-*}-${end^^}"

echo "$str"
Easy enough to then make that process into a little function
Liked your idea but how can I do within file or basically as a small script? Eng goal is file should be updated with capital letters. Please guide me.
 
Old 04-30-2017, 06:42 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,131

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Code:
sed -r "s/-[[:alpha:]]+$/\U&/"
Adjust the character class as needed.
 
Old 04-30-2017, 07:22 PM   #7
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by syg00 View Post
Code:
sed -r "s/-[[:alpha:]]+$/\U&/"
Adjust the character class as needed.
Please assist me. It works in the following situation

AB-System-Check-ping

It does not work for the below situations

AB-System-Check-Swap-Check-01
AB-System-Check-CPU-Usage


What should I do so that I can make it work-able in all situations?


Most important when I do :
Code:
sed -ir "s/-[[:alpha:]]+$/\U&/" data.txt
My file is still having old contents meaning it is not have an "inline" updates ?
 
Old 04-30-2017, 07:51 PM   #8
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,524

Rep: Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015Reputation: 1015
After the last slash put a "g". That means sed should not stop at the first instance.
 
Old 04-30-2017, 08:34 PM   #9
sysmicuser
Member
 
Registered: Mar 2010
Posts: 458

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by AwesomeMachine View Post
After the last slash put a "g". That means sed should not stop at the first instance.
Unfortunately it is not working
 
Old 04-30-2017, 08:51 PM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,131

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Quote:
Originally Posted by sysmicuser View Post
Is it possible to turn a sub string/partial string into capital locks?
My answer was to show that yes you can. It was specific to the initial data shown.
Read the documentation to make it more generally applicable. If you don't understand what it is doing you certainly shouldn't be attempting updates in-place.
 
Old 04-30-2017, 08:56 PM   #11
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 997

Rep: Reputation: 382Reputation: 382Reputation: 382Reputation: 382
Probably more complex "rules" are needed to distinguish between
-Swap-Check-...
-CPU-Usage
etc. Maybe a table of words to capitalize all vs. first letter.
Or a list of desired outputs, grep -i 'ed by like: cpu-use

Last edited by !!!; 04-30-2017 at 10:52 PM.
 
Old 05-01-2017, 12:27 AM   #12
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Quote:
Originally Posted by sysmicuser View Post
Please assist me. It works in the following situation

AB-System-Check-ping

It does not work for the below situations

AB-System-Check-Swap-Check-01
AB-System-Check-CPU-Usage


What should I do so that I can make it work-able in all situations?
You'll need to show enough samples to properly define the type of strings you will encounter. One string by itself was not enough. The original question was answered for the one string you asked about.

That said, with the slightly larger sample set, there are still several ways to consider. If the first part of the string is always the same, that can be used to identify the part to be changed. The following will convert everything after "AB-System-Check" to uppercase.

Code:
sed -r -e 's/^(AB-System-Check)-(.+)$/\1-\U\2/;'
Please see

Code:
man 7 regex
 
Old 05-01-2017, 01:51 AM   #13
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
sysmicuser, you should try a little harder.
after your initial post, you haven't shown any effort, in fact you just keep dismissing the advice offered.
show us what you got and how you're working on it and where you're failing.
you can't expect people to just throw possible solutions at you until one happens to fit.
you want to learn how to do it yourself in the end, don't you? this one isn't too hard, but YOU have to get the logic right.
 
1 members found this post helpful.
Old 05-01-2017, 06:02 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
So I am with the others that you need to start showing some of what you have tried, however, there are some little things I can help with and questions I have:

1. sed -ir ... :- You will find that this did work but that you also have a backup copy of the original file called - data.txtr, because you left no space after the -i option it has assumed that the 'r' is to designate the back up naming and because of this the '-r' option is now missing so any extended items, like +, are seen as exactly what they are and not used for the regular expressions as a meta-character. Hence, no changes made even though you will have a new file

2. You mentioned the sed did not work for AB-System-Check-CPU-Usage, I cannot understand how when it follows the exact same format as previous examples. It works just fine at the command line

3. Again you say the sed does not work for AB-System-Check-Swap-Check-01, my question would be, what was it supposed to do, there are not characters in the position mentioned to be made upper case as numbers do not have a case. Please advise what should have happened??

As mentioned by others, give well thought out examples (especially if there are a few curly cases), then provide the desired output for those cases. Lastly, try and show your attempts to solve the cases
mentioned so we know where we are going?

As for using the substitution I showed, you could feed your input into a while loop and make the necessary changes to a temp file and then move then new file to the old name once done (although sed is more the correct tool at this point)
 
1 members found this post helpful.
Old 05-01-2017, 09:42 AM   #15
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,668
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
May I ("puh-leeuze!") make an alternative suggestion?

- - -

Don't imagine that "Bash scripting" is your only alternative – that you have to string-together sed-gyrations to accomplish simple tasks.

A far better alternative is to use #!shebang.

For example ... "are you comfortable with php?" Very well, then start this way:
Code:
#!/usr/bin/env php
<?php
  ... write your script in PHP
Prefer another language? Go for it.

The primary reason for my suggestion is – not only that you can easily do it ... that you don't have to be a "Bash-script monkey" – but that the resulting script is infinitely easier for a mortal to understand, and to further revise as the requirement continues to grow and to evolve.

I've been doing this crazy programming thing for decades now, and I defy you to figure out "at a glance" what such a script is actually doing – even if you wrote the thing just a month or so ago. Instead of "monkey-poo business," you can write an honest program.

Last edited by sundialsvcs; 05-01-2017 at 10:01 AM.
 
  


Reply



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
[SOLVED] "Catchable fatal error: Object of class input could not be converted to string" - php Skyer Programming 2 03-12-2012 11:04 PM
PHP "Object of class mysqli could not be converted to string " Randall Slack Programming 1 12-02-2011 10:02 AM
chmod - need help with options capital D and capital F.. cookiedough Linux - Newbie 4 04-04-2011 08:07 PM
assign converted string to variable bmaheni Linux - Newbie 2 12-28-2007 08:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:06 PM.

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