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.