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 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. |
You could use sed with your file as input to output the appropriate translated characters.
e.g. Code:
sed 's/-ping/-PING/' <filename |
You could do it in bash with a little bash substitution:
Code:
str="AB-System-Check-http" |
Quote:
|
Quote:
|
Code:
sed -r "s/-[[:alpha:]]+$/\U&/" |
Quote:
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 |
After the last slash put a "g". That means sed should not stop at the first instance.
|
Quote:
|
Quote:
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 |
Quote:
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/;' Code:
man 7 regex |
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 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. |