Remove ANSI codes from string with sed [duplicate] - bash

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

Related

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

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

sed -i ‘’ -e '/javaagent.jar/ s/$/ proxyPort=8080/' this command should append string only if the line is uncommented [duplicate]

This question already has answers here:
sed - Commenting a line matching a specific string AND that is not already commented out
(10 answers)
sed in-place flag that works both on Mac (BSD) and Linux
(15 answers)
Ignore comments (#) using sed, but keep the lines untouched
(5 answers)
Closed 2 years ago.
Im using below to append proxyport=8080 to the end of the line where javaagent.jar string found and below is working fine, but this should ignore if there is a # (commented line) in the text file
sed -i '' -e '/javaagent.jar/ s/$/ proxyPort=8080/'
Example input:
#/opt/ver/inmind/javaagent.jar
/opt/ver/inmind/javaagent.jar
Example output should be:
#/opt/ver/inmind/javaagent.jar
/opt/ver/inmind/javaagent.jar proxyPort=8080
Assuming you have a FreeBSD sed version (as you have -i '' in your command), you can use
sed -i '' -e '/^#/!s/javaagent\.jar.*/& proxyPort=8080/' file
See the online demo.
The /^#/! part stops processing lines starting with # (add [[:space:]]* after ^ to account for indentation, any leading whitespace), and if there is no # at the start, s/javaagent\.jar.*/& proxyPort=8080/ finds javaagent.jar + the rest of the line and replaces this match with itself (with &) and adds a space and proxyPort=8080.

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 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
.

Using grep to filter real time output of a process? If so, how to get the line after a match? [duplicate]

This question already has answers here:
How to show only next line after the matched one?
(14 answers)
grep: show lines surrounding each match
(14 answers)
Read from a endless pipe bash [duplicate]
(1 answer)
Closed 4 years ago.
Should I use grep to filter a real time output? I'm not sure if this is what I should use for a real time output.
Example: command -option | grep --color 'string1\|string2'
If so, how to get also the lines after string1 and string2?
As #shellter mentioned, from man grep:
-A num, --after-context=num
Print num lines of trailing context after each match. See also the -B and -C options.
so you would use command -option | grep -A 1 --color 'string1\|string2' to print matched lines and the line right after them.
There are plenty of other options in the manual for grep, and most other command-line programs, so I suggest getting used to running man cmd as a quick first check.

Resources