Print Terminal Output Exactly how it Appears in the Terminal to a New Text File

Hello All,

I have a text file containing output from a command that contains lots of escape/control characters that when viewed using vi or view, looks like jibberish. But when viewed using the cat command the output is formatted properly.

Is there any way to take the output from the cat command, exactly how it is displays in the terminal and print it to another text file...?

So basically it would be like the new text file contains the interpreted output of the first file. Almost as though I just copy and pasted the terminal output to a text file, so the new text file wouldn't show the control characters... Does that make sense?

For example:
One line from file1.txt Contains:

server           37290556^[[29G4.5    user

Then `cat` prints:

server           37290556   4.5    user

I've tried a few different things like redirection of cat, the tee command, and 'echo -e' like this:

echo -e "$(cat file.txt)" > new_file.txt

But nothing is getting me the results I'm looking for.
Each new file that gets written to from the attempts I mentioned above, contains the EXACT same data as the original file.

If anyone has ANY thoughts or suggestions, it would be greatly appreciated.!

Thanks in Advance,
Matt

See if something like this might do the trick:

sed "s/$(printf '\033')"'\[[0-9;]*[[:alpha:]]/ /' file1.txt > new_file.txt

It will not preserve the exact position of characters...

That is because cat hides non-printing characters and to see them do cat -vet file and write a small script to remove those unwanted characters...

Hey guys, thanks for the replies. Much appreciated!

The sed command gave some strange results, and with the character locations not being preserved, I won't be able to find what I'm looking for in the file.

Thanks for the info on cat, but that just shows me what non-printing characters are in there and by removing the control chars it won't preserve how the text looks when printed using just 'cat filename.txt'....

I guess maybe this isn't possible..? I thought maybe it would be since the escape sequences are just re-arranging the text (*and inserting whitespace) and I can physically see how the formatted data SHOULD look when printed in the terminal. So I guess I kind of hoped I could just capture what is actually seen in the terminal and NOT what is unseen, like escape sequences/control-characters...

Thanks Again,
Matt

---------- Post updated at 06:52 PM ---------- Previous update was at 05:41 PM ----------

To explain a little better of what I'm trying to do see below, *maybe there is a simpler way....

So what the goal is, I'm trying get the output of the AIX command "topas" into a text file so I can grab one specific value... This command is very similar to the top command where it is an interactive command which just continuously updates the terminal window with new values every 2 seconds... Since it does that, that is why the control characters show up in the text file. They use the control characters to place the values correctly on the screen.

If you redirect topas to a text file and kill it right away, and then do a vi / view of the file the values are all over the place. It seems like it shows some of the Value's labels in the correct spot but their actual values could show up 20 lines below where it would be if you just did a 'cat' on the file.

I was praying topas had a command line switch similar to top's "-b" option for batch mode, but it does not...

There is a way to record the topas command's values with the topasrec command, but the output from that is harder to understand then just using topas. And I believe you need to use something like Excel to read that file....

I guess if there isn't another way I can just do what I'm already doing and see if that Label's value shows up on the exact same line everytime... If it does, I can just hard-code it in to capture that specific line and pull out the value... But, I guess we'll see.

Thanks Again,
Matt

You may be able to eliminate the escape sequences by changing the terminal type to 'dumb' so that the program uses only spaces to generate the output.

1 Like

On linux and a virtual console, screendump will eliminate console_codes . Not sure how positioning will be influenced.

1 Like

Hey guys, thanks for the replies.

Ok cool, I'll give that a shot and comment back.

Thanks Again,
Matt

Sorry for the late response, but I had to put this off for a bit...

But, I found a better solution then the topas command. Instead I am using the vmstat command. This command will print out the specific value that I need and it doesn't manipulate the terminal with escape sequences like topas does...

So if I want the Runqueue data, running the following command will print me out a 2 second average 30 times. So basically I'm getting the Avg Runqueue value every 2 seconds for one minute.

Like this:

# vmstat 2 30

System Configuration: lcpu=12 mem=59392MB

kthr    memory              page              faults        cpu    
----- ----------- ------------------------ ------------ -----------
 r  b   avm   fre  re  pi  po  fr   sr  cy  in   sy  cs us sy id wa
12  0 7141885 601248   0   0   0   0    0   0 1028 769445 74759 44 33 23  0
13  0 7137635 605413   0   0   0   0    0   0 907 688147 153046 46 47  7  0
17  1 7150048 592918   0   0   0   0    0   0 1156 613005 148124 49 44  6  0
17  1 7138231 604545   0   0   0   0    0   0 886 433753 228084 42 50  7  0
18  1 7161200 581382   0   0   0   0    0   0 1101 610800 128955 49 43  7  0
11  0 7150526 592039   0   0   0   0    0   0 983 642756 129719 56 40  3  0
16  1 7149470 593014   0   0   0   0    0   0 1020 588923 99835 54 43  3  0
10  0 7151992 590498   0   0   0   0    0   0 1088 567663 80723 38 36 25  1
11  0 7153280 588958   0   0   0   0    0   0 1305 519558 88317 50 43  7  0
14  1 7139093 603321   0   0   0   0    0   0 1672 467037 160236 42 49  8  1
...
.....
....etc...
.....
...

And from the output Column 1, with the column Heading of "r", that is the Runqueue data.

Thanks to all who commented on the Post. Very much appreciated!

Thanks,
Matt

1 Like