Jay Taylor's notes
back to listing indexRemove color codes (special characters) with sed | commandlinefu.com
[web search]What's this?
commandlinefu.com is the place to record those command-line gems that you return to again and again.
Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.
If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/
Get involved!
You can sign-in using OpenID credentials, or register a traditional username and password.
First-time OpenID users will be automatically assigned a username which can be changed after signing in.
Stay in the loop…
- Follow the Tweets.
-
Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.
» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10 - Subscribe to the feeds.
-
Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):
Subscribe to the feed for:
- » all commands
- » commands with 3 up-votes (commandlinefu3)
- » commands with 10 up-votes (commandlinefu10)
News
- May 19, 2015 - A Look At The New Commandlinefu
- I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
- March 2, 2015 - New Management
- I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Functions
- access
- acpi
- admin
- alias
- amixer
- aplay
- apropos
- apt
- ar
- arch
- arecord
- arp
- as
- at
- atq
- atrm
- aumix
- awk
- badblocks
- banner
- basename
- bash
- batch
- bc
- bg
- bison
- break
- bzgrep
- bzip2
- c++
- cal
- cat
- cc
- cd
- cdparanoia
- cdrdao
- cdrecord
- cflow
- chage
- chattr
- chgrp
- chkconfig
- chmod
- chown
- chsh
- chvt
- clear
- cmp
- col
- column
- comm
- command
- compress
- continue
- cp
- cpio
- cpp
- crontab
- csplit
- ctags
- cut
- cvs
- date
- dd
- debugfs
- delta
- df
- diff
- diff3
- dig
- dir
- dirname
- disable
- dmesg
- dot
- du
- dump
- dumpe2fs
- e2fsck
- e2label
- echo
- ed
- egrep
- eject
- emacs
- enable
- env
- etags
- eval
- ex
- exec
- exit
- expand
- export
- expr
- factor
- false
- fc
- fc-cache
- fc-list
- fdisk
- fetchmail
- fg
- fgrep
- file
- find
- finger
- fmt
- fold
- formail
- free
- fsck
- ftp
- fuser
- g++
- gawk
- gcc
- gdb
- get
- getconf
- getent
- gpasswd
- gpg
- grep
- groff
- groups
- gs
- gunzip
- gzexe
- gzip
- halt
- hash
- hdparm
- head
- hexdump
- host
- hostname
- hwclock
- iconv
- id
- ifconfig
- info
- init
- install
- ipcrm
- ipcs
- iptables
- iptables-restore
- iptables-save
- isoinfo
- ispell
- jobs
- join
- kill
- killall
- kudzu
- last
- lastb
- ldconfig
- ldd
- less
- lftp
- link
- ln
- loadkeys
- locale
- locate
- lockfile
- logger
- login
- logname
- look
- losetup
- lp
- lpadmin
- lpr
- lpstat
- ls
- lsattr
- lsmod
- lspci
- lsusb
- m4
- mailq
- mailx
- make
- man
- manpath
- mcd
- md5sum
- merge
- mformat
- mkdir
- mkdosfs
- mke2fs
- mkfifo
- mkfs
- mkfs.ext3
- mkisofs
- mknod
- mkswap
- mktemp
- modinfo
- modprobe
- more
- mount
- mpg123
- mpg321
- mt
- mv
- named
- namei
- nameif
- netstat
- newgrp
- nice
- nl
- nm
- nohup
- nslookup
- objdump
- od
- passwd
- paste
- patch
- pax
- perl
- pidof
- ping
- pinky
- pmap
- pr
- printenv
- printf
- ps
- pwd
- python
- qdel
- qstat
- quota
- rdate
- read
- readelf
- readlink
- readonly
- reboot
- rename
- renice
- reset
- resize2fs
- restore
- return
- rev
- rm
- rmdir
- rmmod
- rndc
- route
- rpm
- rsync
- scanimage
- scp
- screen
- script
- sdiff
- sed
- sendmail
- sensors
- seq
- set
- setleds
- setterm
- sftp
- sh
- sha1sum
- shift
- showkey
- shred
- shutdown
- size
- skill
- sleep
- slocate
- sort
- split
- ssh
- ssh-add
- ssh-keygen
- sshd
- stat
- strace
- strings
- strip
- stty
- su
- sudo
- sum
- swapoff
- swapon
- sync
- sysctl
- tabs
- tac
- tail
- tailf
- tar
- taskset
- tcpdump
- tee
- telnet
- test
- time
- times
- tload
- top
- touch
- tput
- tr
- traceroute
- trap
- true
- tty
- tune2fs
- type
- ulimit
- umask
- umount
- unalias
- uname
- uniq
- unlink
- unset
- uptime
- useradd
- userdel
- usermod
- users
- usleep
- uudecode
- uuencode
- val
- vi
- vim
- vmstat
- wait
- wall
- watch
- wc
- wget
- what
- whatis
- whereis
- which
- who
- whoami
- whois
- write
- xargs
- yes
- zcat
- zdump
- zgrep
- zless
Remove color codes (special characters) with sed
Alternatives
There is 1 alternative - vote for the best!
Removes ANSI color and end of line codes to the [{attr1};...;{attrn}m format.
Remove ( color / special / escape / ANSI ) codes, from text, with sed
Credit to the original folks who I've copied this command from.
The diff here is:
Theirs: [m|K]
Theirs is supposed to remove \E[NUMBERS;NUMBERS[m OR K]
This statement is incorrect in 2 ways.
1. The letters m and K are two of more than 20+ possible letters that can end these sequences.
2. Inside []'s , OR is already assumed, so they are also looking for sequences ending with | which is not correct.
This : [a-zA-Z]
This resolves the "OR" issue noted above, and takes care of all sequences, as they all end with a lower or upper cased letter.
This ensures 100% of any escape code 'mess' is removed.
Remove ANSI colors from stream or file
Handles the color codes intended for 256-color terminals (such as xterm-(256)color and urxvt-unicode-256color), in addition to the standard 16-color ANSI forms. Overkill for strict ANSI output, see other options for something simpler.
Know a better way?
If you can do better, submit your command here.
What others think
See also http://www.pixelbeat.org/scripts/ansi2html.sh for other possible codes to strip
Just what I needed for cleaning the capture of the 'script' command. Thanks!
Does this work when you have 2 colors on a line?
badass. nice seddage :)
i wonder if there is a simpler way to do it with simple redirection of stdout.. hmm not sure yet. anyway awesome, i'm using this for now xD
FWIW, the `-r` does not work with `sed` on Mac OS-X.
Here's the OS X equivalent:
sed -E "s/"$'\E'"\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g
Note:
-E is the OS X equivalent of the -r option.
$'\E' splices a literal Escape character into the regex, because OS X sed doesn't recognize the \xnn escapes.
Also:
One of the ()? constructs is redundant, because it is covered by the enclosed ()*.
The '|' is probably misplaced, as the enclosing construct is the set operator, not an alternative, so '[mK]' is sufficient.
The terminal 'K' relates to the 'Erase in Line' command, and not a color or style, and thus may not be needed.
You only need *3* digits for *non-standard* sequences relating to aixterm, according to http://en.wikipedia.org/wiki/ANSI_escape_sequences#CSI_codes; if you only care about standard behavior, 2 digits will do.
With the above in mind, we get the following:
Linux:
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})*)?m//g
OS X:
sed -E "s/"$'\E'"\[([0-9]{1,2}(;[0-9]{1,2})*)?m//g
Is there any way to undo the effect of sed command?
Your point of view
You must be signed in to comment.