Tuesday, May 10, 2016

Share the Terminal Output in Unix


if you want to share your terminal output and you do not have access to or do not want to use tools such as WebEx or Teamviewer  you can redirect your terminal output to another terminal.


whatever is written on the source terminal, after pressing enter it will appear in the destination terminal.

For the above to happen, get the number of terminal of the destination terminal, by typing: tty <enter>
The output should be something like: /dev/pts/7

Then on the source terminal type: script /dev/pts/7 <enter>

Now everything that is written on the source terminal will appear on the destination terminal, just note that only appear in the destination terminal after you press enter. Also the same Unix user needs to be used on both terminal.


Thursday, May 5, 2016

Move your Virtual Box machine and files location


If you need to move your vbox , vdi, and vmdk files from one location to another you may encounter that in your vbox file the location of your disks is hard coded.

After you move the files, if you try to run your VM you may get the errors:

Failed to open the hard disk <FilePath> or Cannot register the hard disk <FilePath> {GUID} because a hard disk <DifferentFilePath> with UUID {GUID} already exists.

To solve that do the following:

a) Execute vboxmanage command specifying the full path name of each one of your disks, for example:

"C:\Program Files\Oracle\VirtualBox\vboxmanage.exe" internalcommands sethduuid E:\VM\SE-disk1.vmdk

b) Edit the vbox file and update the lines where your hard disk is specified using the output of the previous command, for example:


 <HardDisks>
        <HardDisk uuid="{d2c62e46-e10e-4784-9b8e-61ca357f28b5}" location="SE-disk1.vmdk" format="VMDK" type="Normal"/>
        <HardDisk uuid="{0f563dcb-5f2c-491f-9a02-1015514778ae}" location="SE-disk2.vmdk" format="VMDK" type="Normal"/>
</HardDisks>

<AttachedDevice type="HardDisk" port="0" device="0">
          <Image uuid="{d2c62e46-e10e-4784-9b8e-61ca357f28b5}"/>
</AttachedDevice>
<AttachedDevice type="HardDisk" port="0" device="1">
          <Image uuid="{0f563dcb-5f2c-491f-9a02-1015514778ae}"/>
</AttachedDevice>


c) Remove your VM from the Manager and add it again.

That's it, you are done!!!.



Friday, April 29, 2016

Filter Log Records between Two Dates

If you have a log file with a date time per line and you need to filter the lines that fall between certain dates you can use this Unix script for that.

For example, if you log file looks like:


Apr 15 02:29:58 GCAdaptiveSizePolicy::compute_survivor_space_size_and_thresh: 


 cat GC.log | sed 's/\(.*\) \([[:digit:]]*\) \([[:digit:]][[:digit:]]\):\([[:digit:]][[:digit:]]\):\([[:digit:]][[:digit:]]\) \(.*$\)/\1 \2\3\4\5 \6/' | awk -v from="$from" -v to="$to" '$2 > from && $2 < to {print $0}' | sed 's/\(.*\) \([[:digit:]]*\)\([[:digit:]][[:digit:]]\)\([[:digit:]][[:digit:]]\)\([[:digit:]][[:digit:]]\) \(.*$\)/\1 \2 \3:\4:\5 \6/'   


cat GC.log -- reads files sequentially, writing them to standard output.

Matches the date:

sed  's/\(.*\) \([[:digit:]]*\) \([[:digit:]][[:digit:]]\):\([[:digit:]][[:digit:]]\):\([[:digit:]][[:digit:]]\) \(.*$\)/\1 \2\3\4\5 \6/'

Each regular expression is enclosed between parenthesis, which are escaped, in our example:

\(.*\)  -- matches "Apr"
\([[:digit:]]*\)  -- Matches the day of the month, "15"
\([[:digit:]][[:digit:]]\): -- Match the hour, "02:"
\([[:digit:]][[:digit:]]\): -- Match the minute "29:"
\([[:digit:]][[:digit:]]\)  -- Match the seconds "58 "
\(.*$\) -- Match the rest of the line

then the second part (between //) is the output

\1 \2\3\4\5 \6

which prints the month, then the day of the month together with the time in order to form a long number and then the rest of the line. The output of the sed command is:

Apr 15022958  GCAdaptiveSizePolicy::compute_survivor_space_size_and_thresh:

The next part of the command is the awk script

awk -v from="15010000" -v to="15035959" '$2 > from && $2 < to {print $0}'

The about script will do the filtering, printing only the lines that are between the from and to values. The next part return the line to its original format, so it takes the output from the awk where the day of the month is toghether with the time and split it again to its original format.

sed  's/\(.*\) \([[:digit:]]*\)\([[:digit:]][[:digit:]]\)\([[:digit:]][[:digit:]]\)\([[:digit:]][[:digit:]]\) \(.*$\)/\1 \2 \3:\4:\5 \6/'


Hope that this is useful for you.