Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
11-25-2010, 03:20 PM
|
#16
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,022
|
Code:
$ man cat
...
-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB
In other words cat -v translates the escape sequences into a plain text representation.
Code:
% echo -en '\r' | cat | xxd
0000000: 0d .
% echo -en '\r' | cat -v | xxd
0000000: 5e4d ^M
% echo -n '^M' | cat -v | xxd ~
0000000: 5e4d ^M
|
|
|
1 members found this post helpful.
|
|
Click here to see the post LQ members have rated as the most helpful post in this thread.
|
11-25-2010, 03:55 PM
|
#17
|
|
LQ 5k Club
Registered: Sep 2009
Distribution: Arch x86_64
Posts: 6,443
|
This works for me:
Code:
ls --color=always > file
cat file
and it will show in color (but only in a terminal, because it understands the color codes, while GUI editors will show funny characters).
This link, even though it's mainly to explain how to color your prompt, has a lot of info about the escape codes:
https://wiki.archlinux.org/index.php/Color_Bash_Prompt
Last edited by MTK358; 11-25-2010 at 04:00 PM.
|
|
|
1 members found this post helpful.
|
11-26-2010, 12:33 AM
|
#18
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,589
|
Quote:
Originally Posted by ntubski
In other words cat -v translates the escape sequences into a plain text representation.
|
We can confirm that. While the printed output may look the same, the ^[ from ls --color is an actual ascii escape character (hex code 1B), while filtering it through cat -v changes it to a literal carat and bracket.
Code:
$ mkdir dir
$ ls -d --color dir >testfile.txt
$ uniname testfile.txt
character byte UTF-32 encoded as glyph name
0 0 00001B 1B ESCAPE
1 1 00005B 5B [ LEFT SQUARE BRACKET
2 2 000030 30 0 DIGIT ZERO
3 3 00006D 6D m LATIN SMALL LETTER M
4 4 00001B 1B ESCAPE
5 5 00005B 5B [ LEFT SQUARE BRACKET
6 6 000030 30 0 DIGIT ZERO
7 7 000031 31 1 DIGIT ONE
8 8 00003B 3B ; SEMICOLON
9 9 000033 33 3 DIGIT THREE
10 10 000034 34 4 DIGIT FOUR
11 11 00006D 6D m LATIN SMALL LETTER M
12 12 000064 64 d LATIN SMALL LETTER D
13 13 000069 69 i LATIN SMALL LETTER I
14 14 000072 72 r LATIN SMALL LETTER R
15 15 00001B 1B ESCAPE
16 16 00005B 5B [ LEFT SQUARE BRACKET
17 17 000030 30 0 DIGIT ZERO
18 18 00006D 6D m LATIN SMALL LETTER M
19 19 00000A 0A LINE FEED (LF)
Code:
$ ls -d --color dir |cat -v >testfile.txt
$ uniname testfile.txt
character byte UTF-32 encoded as glyph name
0 0 00005E 5E ^ CIRCUMFLEX ACCENT
1 1 00005B 5B [ LEFT SQUARE BRACKET
2 2 00005B 5B [ LEFT SQUARE BRACKET
3 3 000030 30 0 DIGIT ZERO
4 4 00006D 6D m LATIN SMALL LETTER M
5 5 00005E 5E ^ CIRCUMFLEX ACCENT
6 6 00005B 5B [ LEFT SQUARE BRACKET
7 7 00005B 5B [ LEFT SQUARE BRACKET
8 8 000030 30 0 DIGIT ZERO
9 9 000031 31 1 DIGIT ONE
10 10 00003B 3B ; SEMICOLON
11 11 000033 33 3 DIGIT THREE
12 12 000034 34 4 DIGIT FOUR
13 13 00006D 6D m LATIN SMALL LETTER M
14 14 000064 64 d LATIN SMALL LETTER D
15 15 000069 69 i LATIN SMALL LETTER I
16 16 000072 72 r LATIN SMALL LETTER R
17 17 00005E 5E ^ CIRCUMFLEX ACCENT
18 18 00005B 5B [ LEFT SQUARE BRACKET
19 19 00005B 5B [ LEFT SQUARE BRACKET
20 20 000030 30 0 DIGIT ZERO
21 21 00006D 6D m LATIN SMALL LETTER M
character byte UTF-32 encoded as glyph name
22 22 00000A 0A LINE FEED (LF)
|
|
|
1 members found this post helpful.
|
11-26-2010, 02:45 AM
|
#19
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
I didn't know about the uniname command from the uniutils package. Thank you David for revealing it. Regarding the weird behaviors, I think they have been clarified. Thanks to all.
|
|
|
|
11-26-2010, 02:53 AM
|
#20
|
|
LQ Newbie
Registered: May 2008
Posts: 5
Original Poster
Rep:
|
Very interesting stuff, but not the answer I was looking for. I want to know if the colors can be saved into a file so that the file can be printed showing different file types in different colors. Anyway thanks for the answers.
|
|
|
|
11-26-2010, 04:43 AM
|
#21
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
Quote:
Originally Posted by smurthygr
Very interesting stuff, but not the answer I was looking for. I want to know if the colors can be saved into a file so that the file can be printed showing different file types in different colors. Anyway thanks for the answers.
|
Given the discussion above, it depends on the application by which you open the file. If it can interpret ansi escape sequences (like a terminal) the answer is yes.
The correct way to write the file can be either one of the following (as shown before):
Code:
\ls --color > file
/bin/ls --color > file
ls --color=always > file
|
|
|
|
11-26-2010, 05:59 AM
|
#22
|
|
LQ 5k Club
Registered: Jan 2006
Location: Oldham, Lancs, England
Distribution: Laptop: Slackware 14.0 // Desktop: Slackware64 14.0 // Netbook: Slackware 14.0
Posts: 6,183
|
smurthygr wants it printed out on paper as it's seen in the terminal.
|
|
|
|
11-26-2010, 07:10 AM
|
#23
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
Quote:
Originally Posted by brianL
smurthygr wants it printed out on paper as it's seen in the terminal.
|
Oops... I didn't notice that... the requirement was not specified in the original post. Thanks brianL.
Unfortunately, I'm not aware of any tool to interpret ansi color escape sequences and convert them to Postscript colors (it looks like enscript and a2ps, just to mention some CLI printing tools, cannot do that).
|
|
|
|
11-26-2010, 10:25 AM
|
#24
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,270
|
I was going to post a nice example of how the $TERM variable can be used to modify the escape codes used by ls to generate the color output, so the OP could potentially control how the listing gets created, and then also potentially create a filter that can translate the output to something his printer can handle. However, when I set $TERM to any value that is found as a terminal type entry in /etc/termcap, ' ls --color=always' doesn't change the escape sequences that it uses to generate color output.
So, does anyone know how to control what ls uses to generate its escape codes? My testing is done on a KDE Konsole ($TERM=xterm).
Code:
#! /bin/sh
NATIVE_TERM=$TERM
ls -lash --color=always > /tmp/ls.${NATIVE_TERM}
od -t c /tmp/ls.${NATIVE_TERM} > /tmp/ls_od.native_term
for termtype in $( perl -e 'while(<>){ if($_ =~ m/^[^\s#]/){@types=split /\|/,$_; print $types[0],"\n";}}' /etc/termcap ); do
TERM=$termtype
# echo $TERM
ls -lash --color=always > /tmp/ls.${TERM}
od -t c /tmp/ls.${TERM} > /tmp/ls_od.${TERM}
diff /tmp/ls_od.native_term /tmp/ls_od.${TERM}
done
TERM=$NATIVE_TERM
This compares the output of ls for every known terminal type on my system against the native terminal type and finds no differences whatsoever. I can see that other applications ( vi, man,...) respect the $TERM variable; just not ls. Google seems silent on the subject.
Sorry if this is a hijacking of the thread. It started out as an on-topic contribution...
--- rod.
|
|
|
|
11-26-2010, 09:29 PM
|
#25
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,897
|
Quote:
Originally Posted by theNbomr
I was going to post a nice example of how the $TERM variable can be used to modify the escape codes used by ls to generate the color output, so the OP could potentially control how the listing gets created, and then also potentially create a filter that can translate the output to something his printer can handle.
|
The idea is correct: the problem is to create a filter to generate the proper PostScript code based on the given color escape sequences, but they cannot be changed by the ls command (basically because they are a standard). Moreover looking at the ls source code, there is no link between the terminal type and the generated colors, since the problem is if the output is connected to a terminal or not, hence if the sequences must be generated or not.
On the other hand, ls is influenced by the LS_COLORS environment variable, but only to retrieve the color definitions associated to each object and change colors accordingly.
|
|
|
|
11-27-2010, 05:25 PM
|
#26
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,589
|
Quote:
Originally Posted by theNbomr
So, does anyone know how to control what ls uses to generate its escape codes?
|
They're controlled by the LS_COLORS environment variable. You can use the dircolors command to generate a properly-formatted variable entry.
Here's a detailed how to:
http://ubuntuforums.org/archive/index.php/t-41538.html
also check out man dircolors and info dircolors.
|
|
|
|
11-28-2010, 02:11 AM
|
#27
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,270
|
Yes, I knew that. That simply defines the relationship between file types and colors. How the colors actually get displayed on a terminal is a different matter, and is quite device-dependent. That is the part that I was trying to establish. colucix seems to have figured that part out. Now that I know where there must exist a concise list of the escape codes used, I think I will have a go at producing a basic filter that can translate the escape codes to something palatable to a printer. I don't know that Postscript is the easiest to generate, so perhaps some intermediate format, letting an existing tool do some of the heavy lifting. While researching the subject, I came across some PHP code that does the translation to some kind of HTML. Perl should do at least as well.
--- rod.
|
|
|
|
11-30-2010, 08:06 AM
|
#28
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
@ ntubski,
I'm curious: on your system, what package brings `xxd` ?
|
|
|
|
11-30-2010, 09:13 AM
|
#29
|
|
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,022
|
To my surprise it turns out to be vim-common: Debian Package Search for xxd.
|
|
|
|
11-30-2010, 09:21 AM
|
#30
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
Ehh, no wonder I couldn't find it - I haven't got vim installed.
Thanks!
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:34 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|