LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sub-string to be converted into Capital (https://www.linuxquestions.org/questions/linux-newbie-8/sub-string-to-be-converted-into-capital-4175604954/)

sysmicuser 04-30-2017 08:12 AM

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.

JeremyBoden 04-30-2017 08:53 AM

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

e.g.
Code:

sed 's/-ping/-PING/' <filename

grail 04-30-2017 11:08 AM

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

sysmicuser 04-30-2017 05:41 PM

Quote:

Originally Posted by JeremyBoden (Post 5704227)
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

sysmicuser 04-30-2017 05:59 PM

Quote:

Originally Posted by grail (Post 5704257)
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.

syg00 04-30-2017 06:42 PM

Code:

sed -r "s/-[[:alpha:]]+$/\U&/"
Adjust the character class as needed.

sysmicuser 04-30-2017 07:22 PM

Quote:

Originally Posted by syg00 (Post 5704384)
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 ?

AwesomeMachine 04-30-2017 07:51 PM

After the last slash put a "g". That means sed should not stop at the first instance.

sysmicuser 04-30-2017 08:34 PM

Quote:

Originally Posted by AwesomeMachine (Post 5704396)
After the last slash put a "g". That means sed should not stop at the first instance.

Unfortunately it is not working :(

syg00 04-30-2017 08:51 PM

Quote:

Originally Posted by sysmicuser (Post 5704223)
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.

!!! 04-30-2017 08:56 PM

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

Turbocapitalist 05-01-2017 12:27 AM

Quote:

Originally Posted by sysmicuser (Post 5704392)
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

ondoho 05-01-2017 01:51 AM

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.

grail 05-01-2017 06:02 AM

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)

sundialsvcs 05-01-2017 09:42 AM

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.


All times are GMT -5. The time now is 09:45 AM.