How can I capitalize a single letter after a certain character? [duplicate] - bash

This question already has answers here:
Using Sed to capitalize the first letter of each word
(4 answers)
Closed 1 year ago.
I have some strings
some-string
some-other-string
yet-another-string-to-handle
I want to convert those strings into
someString
someOtherString
yetAnotherStringToHandle
I'm trying to do the following
echo yet-another-string-to-handle | sed -r 's/\-(.*)/\U\1\E/g'
But that results in
yetANOTHER-STRING-TO-HANDLE
Needless to say, I'm a bit lost. Any suggestions on how I can achieve my goal?

With GNU sed:
sed -E 's/-(.)/\u\1/g' file
\u: Turn the next character to uppercase (GNU 'sed' extension).
Output:
someString
someOtherString
yetAnotherStringToHandle
See: info sed

Related

Replace a string with special characters using sed [duplicate]

This question already has answers here:
Is it possible to escape regex metacharacters reliably with sed
(4 answers)
Closed 7 months ago.
I have the below string
sec.val.hos.patn=.*app\.com$|localhost$|127\.0\.0\.1$
I want replace .*app\.com$|localhost$|127\.0\.0\.1$ with * so that final string looks like below
sec.val.hos.patn=*
I am trying to solve this problem using below sed command on Mac OS
sed -i ' ' 's~\.\*app\\\.com\$\|localhost\$\|127\\\.0\\\.0\\\.1\$~\*~g' file.txt
but unable to get the desired replacement. Can someone please help me to get this working.
I don't know if the pattern is actually more complex than you sample, but seems this should do the trick:
sed 's/\(^.*=\).*$/\1*/' <<< "sec.val.hos.patn=.*app\.com$|localhost$|127\.0\.0\.1$"
Here we capture everything from the start to =, then we replace the whole thing with capture group 1 plus *.

How to add double quotation marks ("") around pipe-separated fields [duplicate]

This question already has answers here:
How can I add quotation marks to fields in a CSV file?
(4 answers)
Closed 2 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Let's say I have a file with this structure:
1|2|3|4|
5|6|7|8|
9|10|11|12|
However, I want my file to look like this (expected output):
"1"|"2"|"3"|"4"|
"5"|"6"|"7"|"8"|
"9"|"10"|"11"|"12"|
I am trying to used sed command in the following way:
sed 's/^/"/g'
Unfortunately, it only adds quotation marks at the beginning of each line:
"1|2|3|4|
"5|6|7|8|
"9|10|11|12|
^ means "the beginning of a line". Use [^|] instead which means "anything but |". If your implementation of sed supports +, you can use
sed -E 's/[^|]+/"&"/g'
otherwise, you need to be more verbose
sed 's/[^|][^|]*/"&"/g'
& represents the matched part.
You can use
sed -E 's/[^|]+/"&"/g' file > newfile
The -E option enables the POSIX ERE syntax and [^|]+ thus matches one or more chars other than |, and "&" replaces each with its copy enclosed with " on both sides.
See the online sed demo:
s='1|2|3|4|
5|6|7|8|
9|10|11|12|'
sed -E 's/[^|]+/"&"/g' <<< "$s"
Output:
"1"|"2"|"3"|"4"|
"5"|"6"|"7"|"8"|
"9"|"10"|"11"|"12"|
Here is a gnu awk way of doing the same:
awk -v RS="[|\n]+" '{ORS=RT; print "\"" $0 "\""}' file
"1"|"2"|"3"|"4"|
"5"|"6"|"7"|"8"|
"9"|"10"|"11"|"12"|

How do I remove a line that does not contain a specific character? [duplicate]

This question already has answers here:
In-place edits with sed on OS X
(8 answers)
Closed 2 years ago.
For example, I want do remove all lines in a textile that do not contain the character '#'
I have already tried to use sed like so
sed '/#/!d' data.txt
What am I missing? Shouldn't this work?
I prefer using ed over the non-standard sed -i, especially if it needs to be portable:
printf "%s\n" "v/#/d" w | ed -s filename
This deletes every line that doesn't contain a #, and saves the changed file back to disc.
sed -n '/#/p' [file]
-n suppress default printing
/#/ match on # anywhere on the line
p print if it matches
Add -i for in-place editing of the file (if supplied).

Remove ANSI codes from string with sed [duplicate]

This question already has answers here:
Removing colors from output
(19 answers)
Closed 3 years ago.
Given the following string:
"foo\e[38;5;1mbar\e[0mbaz"
How can I remove both \e[38;5;19m and \e[0m with sed without knowing the exact values of the numbers within those strings?
Expected output:
foobarbaz
I have the following sed line, which matches the first string:
# regex: \\e\[[^/]*m
sed -E 's/\\e\[[^/]*m//g'
This returns sed: 1: unbalanced brackets ([]).
How can I get rid of both strings where the numbers are not known?
You were very close. This should do the job:
$ echo "foo\e[38;5;1mbar\e[0mbaz" | sed -E 's/\\e[^\\]*m//g'
# foobarbaz

How can I use a regex pattern in `tr` to replace only the final character of the match? [duplicate]

This question already has answers here:
Replacing first occurence in every line
(6 answers)
unix tr find and replace
(4 answers)
Closed 3 years ago.
I'm trying to use tr in bash to replace only the final character of a match. The string will have a substring with 5 digits followed by a dash, but I want to replace that dash with a slash.
I want to use something like this:
echo "xyzvb12345-Ab-C5678-dEf" | tr "#####-" "#####/"
To get an output like this:
xyzvb12345/Ab-C5678-dEf
Is there a way to do this with tr? Or maybe sed?
EDIT:
This is not a duplicate of the many tickets out there that merely find and replace text. Please read carefully before marking as a duplicate.
echo "xyzvb12345-Ab-C5678-dEf" | sed 's/\([0-9]\{5\}\)-/\1\//g'
[0-9] matches numbers
\{5\} matches five of the previous group (numers)
\(...\) set the matching group so as to be referred in replacement (as \1)
g at the end tells sed to replace all matches in the input
echo "xyzvb12345-Ab-C5678-dEf" | sed '0,/-/s//\//'
Thanks to this other answer
.

Resources