Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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.
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.
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)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.