Showing posts with label command-line. Show all posts
Showing posts with label command-line. Show all posts

Thursday, 16 August 2012

Randomize lines in two files, keeping relative order


I recently wanted to randomize the lines in two files, but to keep the relative order of the lines between the files. So I can remember how to do this next time I will post it here.
for i in `cat file1.txt`;do echo $RANDOM;done >randomOrder.txt
paste randomOrder.txt  file1.txt file2.txt  |sort -k1n >sorted.txt
cut -f 2 sorted.txt  >file1.txt
cut -f 3 sorted.txt  >file2.txt
rm -f sorted.txt
rm -f randomOrder.txt

Thursday, 9 September 2010

Quick Tip: PUT - scp files from remote machine to local machine

It is often desirable to have a quick look at a file on a remote machine, such as an HPC cluster, on your desktop machine. I use MacFUSE and SSHFS for this but sometimes I like to move a file without finding my current location in a finder windows. I have written a quick wrapper around scp with a line to determine the IP address of my local machine, stored as an environment variable.

Add the following lines to your .bashrc on the remote machine.


export DESKTOP=$(last -100 |grep $(whoami) |head -n 1 |perl -ane 'print $F[2]')
put(){
if [ -z "$1" ]; then
    echo "put - Sends specified files to Desktop of local machine";
    echo "usage: put filesToSend";
else
find "$*" |xargs -I % scp % $DESKTOP:~/Desktop
fi
}

Then reload the file with source .bashrc. You should now be able to type put fileName, and the file will appear on your desktop. As long as you have your rsa keys set up, otherwise it will ask for a password too. It works for multiple files too, such as *.png. 

I find it useful for quickly viewing pdfs and images. 

Wednesday, 8 September 2010

My very quick guide to Screen.

Intro:
Due to lots of power outages today I thought I should resurrect by usage of screen, which I have neglected recently.

It is difficult to explain what screen is, much easier to say what it does, or at least what I use it for. Screen allows you to resume an interactive terminal session, from any other machine. For example I ssh from ym desktop machine into our cluster then run R interactively, but if my local machine dies or I accidentally close the terminal window that session ends and I loose everything. However, if I use screen I can resume it. Better still I can go home VPN into work and resume the same session on my laptop. It is ideally for running long interactive jobs, without having to leave my desktop machine on overnight. I could also run screen on my desktop machine, log into the cluster, then ssh into my desktop from another machine and I can resume the session, getting straight into the cluster.
There are lots of features which are explained elsewhere, but I will detail how I use it and give my config files in case they are useful to anyone else.

Basic usage:
It really couldn't be simpler to get started, just type screen. Nothing much will change, you are still at the terminal, and can do everything as normal, but now you can detach this session and resume at on another machine if you want to, or shut down your desktop and resume when you come back. To resume you just need to do screen -r. 


Another great feature is having multiple screens, which is a bit like having a tabbed terminal window. So you can edit a file in one window, then quickly switch to the other one to run it. The Ctrl-z Ctrl-z command does this very quickly, see below. You can also split the screen, so you can edit at the top, run at the bottom, or maybe monitor something on the top while working on the bottom. Very nice. 


Config:
I use emacs a lot, so the default screen key of Ctrl-a is a bit of a pain as that means move to the start of the line in emacs, something I do a lot. So I have re-mapped my screen key to Ctrl-z, though you can do anything. I have also made a few other changes to my .screenrc


I also added the following lines to my .bashrc to stop screen Woofing at me. Yes, Woofing. 



### http://benmabey.com/2007/12/13/wuff-wuff.html
alias screen='TERM=screen screen'



Basic Keys:
Ctrl-z Ctrl-c : Create a new window
Ctrl-z " : Show a list of windows
Ctrl-z 0
.. : Switch to window x
Ctrl-z 9
Ctrl-z Ctrl-z : Toggle to previous window
Ctrl-z A : Name the current Window

Ctrl-z S : Split the window horizontally
Ctrl-z TAB : Move focus to other section of split screen
Ctrl-z Q : Remove all but current section

You can view all the default command by doing a Ctrl-z ? within screen, or using Google.

There is a lot more to screen than that, and maybe one day I will go into some of the most advanced features.

Monday, 21 June 2010

googleCL - Command Line Access to the cloud

I have just installed googleCL and am blogging this from the command line. There are some really nice features that will be really useful to integrate into other scripts. For example adding calendar appointments or getting contacts e-mail addresses all at the command line. I have not explored all the features yet, but it looks like it will be really useful. I can't wait for access to Tasks which is the feature I really want.