Archive

Posts Tagged ‘Tutorial’

Print next few lines after pattern in AWK

May 6, 2010 1 comment

Input data.txt is a collection report for XYZ corp group by different collection zones.

$ cat data.txt
Total Collection = $10291 {Fri May  8}
zone7   4500
zone8   3545
zone1   1200
zone0   900
zone3   70
zone5   67
zone11  9
Total Collection = $11847 {Sat May  9}
zone1   2800
zone3   2800
zone6   2567
zone8   2300
zone9   1200
zone12  90
zone11  90

Required: We need to find out the top 4 collection zones for each day from the above file. i.e. to print next 4 lines where the pattern “Total Collection =” is found (as the items are sorted on collection amount).

This is how we can achieve this using awk:

$ awk '/^Total Collection =/{c=4;next}c-->0' data.txt
zone7   4500
zone8   3545
zone1   1200
zone0   900
zone1   2800
zone3   2800
zone6   2567
zone8   2300

Now if we need to print the header line also, something like:

$ awk '/^Total Collection =/{c=4;{print}next}c-->0' data.txt
Total Collection = $10291 {Fri May  8}
zone7   4500
zone8   3545
zone1   1200
zone0   900
Total Collection = $11847 {Sat May  9}
zone1   2800
zone3   2800
zone6   2567
zone8   2300

And if you want to just print the date part as the header with top 4 collection zones.

$ awk -F "[{,}]" '/^Total Collection =/{c=4;{print $2}next}c-->0' data.txt
Fri May  8
zone7   4500
zone8   3545
zone1   1200
zone0   900
Sat May  9
zone1   2800
zone3   2800
zone6   2567
zone8   2300
Categories: Linux / Unix, Tutorial Tags: , , ,

Multiple FS in AWK

May 6, 2010 1 comment

Sample file:

$ cat summary.txt
A|Jan|clerk|02:45
B|Jan|Salesman|02:12
C|Jan|Accountant|03:12
A|Feb|clerk|01:10
B|Feb|Salesman|11:10
B|March|Salesman|3:10
C|Feb|Accountant|3:34

Output Required:

(First field)|(last field converted to minutes)
i.e.
A|165
B|132
C|192
A|70
B|670
B|190
C|214

This is how we can specify two field separators (| and 🙂 with FS in awk:

$ awk 'BEGIN{FS="[|,:]"; OFS="|"} {print $1,$(NF-1)*60+$NF}' summary.txt
Categories: Linux / Unix, Tutorial Tags: , ,

Ubuntu auto shutdown due to high CPU temperature

February 23, 2009 40 comments

Ubuntu

I use a laptop with Ubuntu 8.10 installed at work. While running the some high process, the CPU temperature get really high and its automatically shutdown the system. This thing happen every 2 or 3 days and its really annoying. The syslog showed the following error message:

ACPI: Critical trip point
Critical temperature reached (100 C), shutting down.

Read more…

How to reset Ubuntu root password

September 9, 2008 42 comments

If your memory or mistyping leaves you without the right password to get into an account on a Linux computer, there’s no need to reformat. You’ll just need to reboot into single user mode to reset it. Here’s how to do it on a typical Ubuntu machine with the GRUB bootloader:

  1. Reboot the machine.
  2. Press the ESC key while GRUB is loading to enter the menu.
  3. If there is a ‘recovery mode’ option, select it and press ‘b’ to boot into single user mode.
  4. Otherwise, the default boot configuration should be selected. Press ‘e’ to edit it.
  5. Highlight the line that begins with ‘kernel’. Press ‘e’ again to edit this line.
  6. At the end of the line, add an additional parameter: ‘single’. Hit return to make the change and press ‘b’ to boot.
Change the admin password

The system should load into single user mode and you’ll be left at the command line automatically logged in as root. Type ‘passwd’ to change the root password or ‘passwd someuser’ to change the password for your “someuser” admin account.

Reboot

Once your done, give the three finger salute, or enter ‘reboot’ to restart into your machine’s normal configuration.

That’s all there is to it. Now just make sure to write your password down on a post-it and shove it somewhere safe like under your keyboard. 🙂

[ Source: Hackzine.com ]

Fix date and time in Ubuntu

June 20, 2008 30 comments

Dual-booting with Ubuntu and Windows sometimes giving a problem with date and time in Ubuntu. This is a well-established problem when dual-booting, since Linux assumes the hardware clock represents UTC, whereas Windows assumes the hardware clock represents local time. Luckily Linux provides ways to change this to fix it.

If you go into your clock settings, I think you can select between setting the hardware as either UTC or local time. Try switching that, and then adjust the time and see if it “sticks.”

If not, you can do this via the commandline in Linux :-

  1. In Linux, set the date and time to what it currently is. For example if it’s 10:20am local time:
    user@server:~$ sudo date -s 10:20
  2. Then update the hardware clock accordingly, and force this to be considered “localtime”:
    user@server:~$ sudo /sbin/hwclock --systohc --localtime
  3. Check to make sure it looks right:
    user@server:~$ sudo /sbin/hwclock --localtime
  4. Sync between hardware clock and system clock:
    user@server:~$ sudo /sbin/hwclock --hctosys --localtime

Now Linux should consider the clock to be “localtime”, which should be identical to what Windows is doing. So after rebooting into Windows, the time should look right.

Hope that helps.

Reverse SSH Tunnelling

May 13, 2008 3 comments

It is possible to create a “reverse” SSH Tunnel. The reverse tunnel will allow you to create an SSH Tunnel from your work computer to your home computer, for example, and then login to your work machine from your home machine even if your work firewall does not permit ssh traffic initiated from your home machine!

For this to work, an SSH Server must be installed on your work and home computer, and ssh (TCP port 22) must be allowed outbound from your work computer to your home computer.

Syntax: ssh -R remote_port:localhost:22 your_home_computer

At home, you would then run ssh -p 2048 localhost to log into your work computer via ssh.

Here is a script that you can run through the cron facility on your work system to make sure the reverse SSH Tunnel to your home system is up and running. It is useful in case the system is rebooted.

#!/bin/sh

# $REMOTE_HOST is the name of the remote system
REMOTE_HOST=remote.system.ip

# $REMOTE_PORT is the remote port number that will be used to tunnel
# back to this system
REMOTE_PORT=5000

# $COMMAND is the command used to create the reverse ssh tunnel
COMMAND=”ssh -q -N -R $REMOTE_PORT:localhost:22 $REMOTE_HOST”

# Is the tunnel up? Perform two tests:

# 1. Check for relevant process ($COMMAND)
pgrep -f -x “$COMMAND” > /dev/null 2>&1 || $COMMAND

# 2. Test tunnel by looking at “netstat” output on $REMOTE_HOST
ssh $REMOTE_HOST netstat -an | egrep “tcp.*:$REMOTE_PORT.*LISTEN” \
> /dev/null 2>&1
if [ $? -ne 0 ] ; then
pkill -f -x “$COMMAND”
$COMMAND
fi

SSH: Convert OpenSSH to SSH2 and vise versa

December 14, 2007 31 comments

Connecting two server running different type of SSH can be nightmare if you does not know how to convert the key. In this tutorial, I will try to explain on how to convert the public key from OpenSSH to SSH2 and SSH2 to OpenSSH. To convert the key, it must be done in OpenSSH server.

Convert OpenSSH key to SSH2 key

  • Run the OpenSSH version of ssh-keygen on your OpenSSH public key to convert it into the format needed by SSH2 on the remote machine. This must be done on the system running OpenSSH.
    #ssh-keygen -e -f ~/.ssh/id_dsa.pub > ~/.ssh/id_dsa_ssh2.pub

Convert SSH2 key to OpenSSH key

  • Run the OpenSSH version of ssh-keygen on your ssh2 public key to convert it into the format needed by OpenSSH. This needs to be done on the system running OpenSSH.
    #ssh-keygen -i -f ~/.ssh/id_dsa_1024_a.pub > ~/.ssh/id_dsa_1024_a_openssh.pub

Read more…

%d bloggers like this: