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. |
|
 |
|
07-31-2009, 04:40 AM
|
#1
|
|
LQ Newbie
Registered: Jul 2009
Posts: 5
Rep:
|
difference in cat and vi output
Hi,
I'm copying a output to a file. This output is from a terminal.
When I'm opening this file using cat and vi I'm getting diferent output.
e.g.
output of cat is like:
This is your output
while in vi editor output is like:
^M^[[3;0H
^M This is your output
^M^[[4;0H
There are a lot of unwanted character in the file like this
can anybody tell me why it is happening, and how to filter these unwanted character?
I've tried sed its unable to read vi output.
Thanks in advance.
Kesh
|
|
|
|
07-31-2009, 04:44 AM
|
#2
|
|
Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,796
|
Hello,
Type at console
to give you more information about the file itself. I have had these characters in files in the past also, but only when I copied a file from Windows to Linux.
Executing
Code:
dos2unix yourfilename
could solve the problem.
Kind regards,
Eric
|
|
|
|
07-31-2009, 04:53 AM
|
#3
|
|
Member
Registered: Jul 2009
Location: Illinois, US
Distribution: Fedora 11
Posts: 374
Rep:
|
It might be worth telling us what's producing that output. The only thing I can think of (and only because I was reading another thread about this earlier today) is if you're running ls and that's aliased to `ls --color=always` somewhere, vim won't know what to do with the escape sequences that cause text to be colored, but cat will.
Also, dos2unix probably won't help. This is a Linux program producing the output, no? And besides, I wasn't aware dos2unix did anything besides convert CR/LF line terminators to LF.
|
|
|
|
08-01-2009, 06:12 AM
|
#4
|
|
LQ Newbie
Registered: Jul 2009
Posts: 5
Original Poster
Rep:
|
Hi,
Thanks for your feedback...
The thing is this is the outputs of a linux terminal rather then window, so dos2unix, not helping...
When i checked the file type, the coming filetype is:
: ASCII English text, with very long lines, with CRLF, CR line terminators, with escape sequences
Any idea how to remove these unwanted character,
Thanks in advance..
K.
|
|
|
|
08-01-2009, 01:19 PM
|
#5
|
|
Member
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 325
Rep:
|
This happens when you use the script command, the output file (default typescript) is exactly like what you're describing. I don't have any strange settings at all, that's just how it is.
Quote:
file typescript
typescript: ASCII text, with CRLF, LF line terminators, with escape sequences
|
I don't know which programs would do that ...
Last edited by joeBuffer; 08-01-2009 at 01:21 PM.
|
|
|
|
08-02-2009, 12:06 PM
|
#6
|
|
LQ Newbie
Registered: Jul 2009
Posts: 5
Original Poster
Rep:
|
Hi,
Any idea how to remove CRLF, CR line terminators, from a file in linux.
K.
|
|
|
|
08-02-2009, 12:12 PM
|
#7
|
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,843
Rep: 
|
See:
man dos2unix
Or, if that doesn't work:
man fromdos
|
|
|
|
08-02-2009, 01:31 PM
|
#8
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Hello keshavk
Your problem is impossible to solve completely. It has been asked many times before in connection with the script command and terminal spying programs. What you have is the sequence of bytes that went to the terminal; what you want to get is a text version of what appeared on the terminal. That is fundamentally impossible; the terminal is a dynamic device while your text file is static.
As long as nothing complex happened in the terminal session (switch to full screen mode followed by user interaction, command recall, command editing, title setting etc) then a "good enough" solution is possible -- filtering out the bytes that controlled colour, cursor position, rang bells etc -- which may render the text sufficiently readable for your purposes.
An alternative approach -- which has the advantage of not being fundamentally impossible -- would be to replay the captured bytes by feeding them to a terminal thus reproducing the session.
Best
Charles
Last edited by catkin; 08-02-2009 at 01:33 PM.
Reason: Typo
|
|
|
|
08-02-2009, 01:32 PM
|
#9
|
|
Member
Registered: Apr 2006
Location: India
Posts: 470
Rep:
|
If cat is working for you, just type cat filename > out.txt. You'll get your results in the text file. Hope this helps.
|
|
|
|
08-02-2009, 01:43 PM
|
#10
|
|
Member
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 325
Rep:
|
Yeah, when I read catkin's post and thought about what you were saying just now, I was going to suggest cat, also.
It would work, with redirection. It works for me, when using the typescript files.
Last edited by joeBuffer; 08-03-2009 at 06:09 AM.
|
|
|
|
08-03-2009, 12:34 AM
|
#11
|
|
LQ Newbie
Registered: Jul 2008
Location: JHB, South Africa
Distribution: Suse
Posts: 4
Rep:
|
This might help with removing "^M" from a file with vi/vim
Type in the following in vi
:%s/<ctrl>v<ctrl>m//g<ENTER>
Stan
|
|
|
|
08-03-2009, 01:05 AM
|
#12
|
|
LQ Newbie
Registered: Jul 2009
Posts: 5
Original Poster
Rep:
|
Hi Stan,
Thanks for your help.....it helped me in removing many junk...
rest i'll try to solve.
K.
|
|
|
|
08-03-2009, 01:14 PM
|
#13
|
|
LQ Newbie
Registered: May 2009
Distribution: Ubuntu, CentOS, Redhat, Maemo
Posts: 10
Rep:
|
Filtering escape sequences from a file
Why are you using vi? Are you trying to edit the file? Or are you trying to view the file?
If you are only trying to view the file, then you can use "less -fr" and the color sequences will be put to use.
To remove the escape sequences, you can use this:
Code:
$ cat dobit.sh
#!/bin/bash
#
# Copyright SwaJime's Cove, 2009; all rights reserved
#
# Based on data obtained from http://www.gnu.org/software/teseq/manual/html_node/Escape-Sequence-Recognition.html
#
# A control sequence starts with
CSI='\x1b\[' # the two-character csi escape sequence ‘Esc [’, followed by
Rp='[0-9:;<=>?]' # an optional sequence of parameter bytes in the range x30–x3F,
Ri='[- !\"#$%&'\''()*+,./]' # an optional sequence of intermediate bytes in the range x20–x2F,
Rf='[]@A-Z[\\^_`a-z{|}~]' # and a final byte in the range x40–x7e.
# The set of standard control sequence functions are defined in Ecma-48 / ISO/IEC 6429.
ls --color=always > myFile.txt
cat myFile.txt | sed -e 's/'"$CSI$Rp"'*'"$Ri"'*'"$Rf"'//g'
|
|
|
|
08-03-2009, 01:21 PM
|
#14
|
|
Member
Registered: Jul 2009
Location: Illinois, US
Distribution: Fedora 11
Posts: 374
Rep:
|
Quote:
Originally Posted by SwaJime
Code:
# Copyright SwaJime's Cove, 2009; all rights reserved
|
Good of you to provide code, but that's not really in the spirit of this place, is it? 
|
|
|
|
08-04-2009, 09:24 AM
|
#15
|
|
LQ Newbie
Registered: Mar 2009
Posts: 4
Rep:
|
transliterate
In Unix, the character transliteration tool 'tr' exists
almost from the beginning.
To remove CR=13(dec)=15(oct):
tr -d '\015' < infile.txt > outfile.txt
To remove LF=NL=10(dec)=12(oct)
tr -d '\012' < infile.txt > outfile.txt
To remove both:
tr -d '\015\012' < infile.txt > outfile.txt
... all just from the command line, no programming. You can also transliterate
other characters, of course, for instance a->d, b->e, c->f by
tr 'a-c' 'd-f' < infile.txt > outfile.txt
In Linux, there will be mnemonics for meaningful character
subsets like '[:cntrl:]' and '[: punct:]'
Last edited by kunstkopf; 08-04-2009 at 09:38 AM.
Reason: put in some more general info on tr and stressed it is a command line tool
|
|
|
|
| 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 03:51 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
|
|