Thursday, January 7, 2010

Wednesday, December 2, 2009

Cut command

To get any n characters of each line from a file, use cut.

Code to get first 3 characters of file

cut -c1-3 filename
217
207
284
238
216
219

Other examples
1) Get just specific character
echo "your text here" | cut -c2
> o

2) Get everything from a character
echo "your text here" | cut -c2-
> our text here

3) argument -f can be used to get specific fields from the file. Default delimiter is tab. It can be overridden by -d.
e.g.
cat rhyme
>
jack , and jill, went
up the,hill
to fetch,a pail
of water.

cut -f1 -d, rhyme
>
jack
up the
to fetch
of water.

4) argument -s along with -f is used to suppress lines that do not contain the field delimiter.

cut -f1 -d, -s rhyme
>
jack
up the
to fetch

5) Get a range of fields
cut -f2-3 -d, -s rhyme
>
and jill, went
hill
a pail

Cut option -b can be used to obtain same results as -c.

Tuesday, December 1, 2009

Function that returns value in VBA

To code a function that returns value to the calling sub, use FunctionName as the VariableName in the function.

e.g.
function findrownbr()
findrownbr=10
end function

sub readCellData ()
cell_nbr = findrownbr()
msgbox Range("A" &cell_nbr)
end sub

Newline Character in VBA

Chr(13) inserts a new line character in vb.

Output
there is
newline between this and the line above.


Code
"there is a" & Chr(13) & "newline between this and the line above."

Note
Chr(13) should not be in quotes.

Wednesday, November 25, 2009

find a file based on date timestamp

find . -type f -newer guidedog | xargs -i mv {} going2dogs

With the above command, we are
1) looking for files (type -f) with date and timestamp newer than file guidedog.
2) then moving those files to directory going2dogs

guidedog is a dummy file which has the reference time we need. It can be created as -
touch -t 200911251340 guidedog

So all the files that were created after 1:40 PM on 11/25/2009 will move to directory doing2dogs.

Also, between two timestamps will work as

find . -type f -newer guidedog -a ! -newer anotherguidedog | xargs -i mv {} going2dogs

Update : Above command will move anotherguidedog also. To avoid that additional condition should be added before passing output to xargs:
-a ! -name
anotherguidedog

changing case within vi

1)
If you have to change one or two letters, use ~ over the letters while in command mode. vi toggles the cases with ~

2) Between a range, say marks a and b

Change to lower case
'b,'a,s/[aA-zZ]/\L&/g

Change to upper case
'b,'a,s/[aA-zZ]/\U&/g

3) Change to upper case thru out the file
%s/[aA-zZ]/\U&/g

Running shell commands from within vi

Without leaving vi, we can run shell commands from within vi.

e.g. we need to insert contents of a file within our current file opened in vi.

1) Insert will begin immediately after the cursor. So we place cursor where we need the insert.
2) enter vi command mode
3) :r! cat filename

:r! can be used to insert output of a unix command also.
e.g. :r! ls
the above command inserts list of the files in the current directory (pwd)

:sh can be used to enter shell from within vi.
to return to vi, use exit.