How to calculate the length of a xterm control sequence [duplicate] - linux

This question already has answers here:
How can I remove the ANSI escape sequences from a string in python
(8 answers)
Closed 3 years ago.
I am looking to format text before passing it to stdout/console. To do this properly I need to know how long a given section of text is after being printed to the console, which requires knowing how much characters will not be printed due to them being escape sequences.
On the web I have found multiple documentations for these sequence commands, but there seems to be no quick and easy way to find out where a given escape sequence command ends unless I was to parse them. Is there a trick/solution to getting the length of any escape sequence command without parsing?
How many characters is this \x1b[38;2;20;60;122m string \x1b[0m in console?

You can use ansifilter:
$ printf '\x1b[38;2;20;60;122mabc\x1b[0m' | ./ansifilter --no-trailing-nl | wc -c
3

Related

How come the pound sign '£' in Haskell is shown as '\163' instead of outputting '£'? [duplicate]

This question already has an answer here:
Haskell writes '\n' instead of a newline
(1 answer)
Closed 2 years ago.
I'm trying to write a program that writes the pound sign '£' in Haskell, but it outputs '\163' whenever I try to use it. I'm guessing that this is some alphanumeric code, but how do I get it to display what I want it to? I'm writing to the console, when calling a function that returns '£'.
Thank you.
This was solved by using putStrLn, because print and show do not allow for non-ASCII characters to be shown.

Cut characters from position from string in bash [duplicate]

This question already has answers here:
Remove the middle n characters from lines in Bash
(6 answers)
Closed 4 years ago.
I have the following string, which it will always be 35 characters long:
S202SCTRXBAVCWPJAC001181204120000.N
I would like to cut 3 characters (position 17-19), JAC in this case, to remain only
S202SCTRXBAVCWP001181204120000.N
Is there a way to achieve this in bash?
strIn=S202SCTRXBAVCWPJAC001181204120000.N
strOut=${strIn:0:15}${strIn:18}
echo "$strOut"
...uses only bash-built-in functionality to emit:
S202SCTRXBAVCWP001181204120000.N
...as it emits the first 15 characters starting at position 0, then everything after position 18.
Agree with the answer by Charles Duffy. If you know you want specifically "JAC" instead of what indices you want removed, you could do:
str="S202SCTRXBAVCWPJAC001181204120000.N"
echo "${str/JAC/}"

Remove colour code from linux files [duplicate]

This question already has answers here:
Can I programmatically "burn in" ANSI control codes to a file using unix utils?
(2 answers)
Closed 6 years ago.
I have an output file from a testing script (which I cannot alter), the output looks great in the terminal thanks to the encoding, which displays the output in nice colours.
However when I vim the file, I get the following:
^[[1m0024^[[0m, ^[[36munknown.10^[[0m --> ^[[32mUNKNOWN^[[0m
I would rather the file contained:
0024, unknown.10 --> UNKNOWN
There are a couple of similar questions on stackover flow, but so far I have not found a solution that works for me.
Any help would be greatly appreciated!
Many thanks!
Additional info:
I don't want to conceal the colour characters, I would like to remove them from the file.
The output goes into an evidence file, and then that file is pushed up to a GIT for the team to review. It is difficult to the GIT UI with those colour codes :(
To remove color control character, you may use the following sed command:
sed 's/\x1b\[[^\x1b]*m//g' file
As indicated in here, the a color code is composed of <Esc>[FormatCodem.
The escape character is \x1b in hexadecimal (sometimes noted as \e or \033).
The command looks for the sequence escape followed by square bracket \x1b\[ until the character m, if found it deletes it.
Everything in between these 2 characters is allowed except the escape character itself [^\x1b]*. This allows to have the shortest regex.
If you can't remove them from the tool producing the output, you could still remove them afterwards with the following sed command :
sed -r 's/\^\[\[[0-9]{1,2}m//g'
Example :
$ echo """^[[1m0024^[[0m, ^[[36munknown.10^[[0m --> ^[[32mUNKNOWN^[[0m""" | sed -r 's/\^\[\[[0-9]{1,2}m//g'
0024, unknown.10 --> UNKNOWN

How to Format grep Output When Saving to a Variable [duplicate]

This question already has answers here:
How to preserve line breaks when storing command output to a variable?
(2 answers)
Closed 7 years ago.
If I grep our syslogs for a specific term, I get a nice output of those logs matching my term and each entry on a separate line.
If I save that to a variable so I can use it in a script as such:
results=$( grep "term" logs )
echo $results
then all the logs run together and are not human readable.
How can I make it look cleaner so when I do echo $results, I can actually read the output?
Thanks,
Quote it:
echo "$results"
This preserves all the whitespace, instead of using it for word splitting.
In general, you should almost always quote variables, unless you have a specific reason not to.

python, \t works different when passed as argument (in Eclipse) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python: Split string with multiple delimiters
Convert django Charfield “\t” to tab
I have written a python code in Eclipse which takes delimiters as an argument. When I do
print "Hello",delimiter, "All".
This generates --> Hello \t All, whereas if I overwrite the delimiter with delimiter = '\t' within the code, I get the right output Hello All. I wonder what is the difference? I hope this not just the eclipse thing.
The problem is that what is being passed in from the command line is actually a string of length two "\\t" and not a tab character. You can do the following to your delimiter
delimiter.decode("string_escape"))
that should convert the string '\\t' into '\t'. The answer comes from a duplicate questions here

Resources