Sunday, October 11, 2015

Removing special/unwanted characters from linux "script" command output file

What is "script":
        Linux is having an utility to log the output displayed on the screen console. This utility is invoked using the "script" command.

How to invoke script:
       script <file-name>
       The above command will create a text file in the "file-name" and then will start to log all the outputs displayed on the screen into this file.

        Note: If  the file with name "file-name" already present, then it will be overwritten.

Formatting the "script" output file:
        The output file generated using the script command, will have some special/unwanted characters. These can be cleaned up using the "vim" editor's regular expression matching.
        In most cases, you will find the special characters like ^M, ^H, etc. in the output of script command. This can be cleaned up using the below "vim" commands.

StepCommand
Open the file using vim editorvim "file-name"
Go to the command modePress ESC key
Give the command.
This command will remove all the occurrences of the character "^M". The "&lt;ctrl-v><ctrl-m>" combination will be encoded to "^M"
:%s/<ctrl-v><ctrl-m>//g 
  •   Note: Similarly we can give remove any special characters using the combination of "<ctrl-v><ctrl-X>", where X = H,G, or any special character present in the file.
  •  If the character "^M" present at the end of the line, then use the command "":%s/<ctrl-v><ctrl-m>\n/\r/g" to replace it with the line break.

Sample Output:

Before Formatting:




Find & Replace Characters:




After Formatting:



No comments:

Post a Comment