[sed] Replace a tab character using sed

Tags

, , , , , , , , ,

Here’s how to replace all instances of TAB in a file input_file by, say comma (,), using sed

$ sed 's/<TAB>/,/g' input_file

But what is <TAB> above? On Linux systems you may just type \t (which is the regular expression for TAB) in place of <TAB>. However,  on some other systems (e.g., OSX with FreeBSD) it does not work. In cases where it doesn’t work, invoke <TAB> by hitting Control+v followed by the TAB key. This may alternatively be achieved by hitting Control+v followd by Control+i, as well.

References: ATOzTOA, Stack Overflow.

[python] Fit a curve through a given set of data points

Tags

, ,

#!/usr/bin/env python

# import the necessary modules
import numpy as np
from scipy.optimize import curve_fit

# Define the fitting function
def func(x, a, b, c):
  return a*np.exp(-b*x) + c

# Generate x and y values which the curve will be fitted to
# (In practical cases, these should be read in)
x = np.linspace(0,4,50)
y = func(x, 2.5, 1.3, 0.5)
yn = y + 0.2*np.random.normal(size=len(x))

# The actual fitting part
# popt = the fitted parameters as a tuple, namely (a,b,c)
# pconv = The estimated covariance of popt.
#        The diagonals provide the variance of the parameter estimate.
popt, pcov = curve_fit(func, x, yn)

Source: Scipy

2011 in review

Tags

,

The WordPress.com stats helper monkeys prepared a 2011 annual report for this blog.

Here’s an excerpt:

Madison Square Garden can seat 20,000 people for a concert. This blog was viewed about 62,000 times in 2011. If it were a concert at Madison Square Garden, it would take about 3 sold-out performances for that many people to see it.

Click here to see the complete report.

[vim] Automatically jump to the last visited line when a file is reopened

Tags

, , ,

Put the following in your .vimrc file to automatically jump to the last visited line when a file is reopened using vim:

" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
au BufReadPost * if line("’\"") > 0 && line("’\"") <= line("$")
\| exe "normal! g’\"" | endif
endif

Alternatively, you could just reopen the file normally and then hit '0 (that is, single quote followed by the number zero) to get the same result. Source: Stack Overflow

[security] Encrypt a PDF file in the command line

Tags

, , , , ,

There are a few command line tools to encrypt/ password-protect a file, for example OpenSSL, GnuPG. However, if you want to send someone an encrypted PDF who does not have access to these CLI tools, then that’s not cool for the recipient at all! Pdftk is another open-source command line tool that can encrypt a PDF file which can be decrypted using either Pdftk in the commandline or entering the password in a GUI pop-up box when the recipient tries to open the PDF file using a PDF viewer (for example, Adobe Reader).

Examples (taken from PDF Labs):

1. Encrypt a PDF using 128-Bit Strength (the Default) and Withhold All Permissions (the Default)

$ pdftk mydoc.pdf output mydoc.128.pdf owner_pw foopass

2. Same as Above, Except a Password is Required to Open the PDF

$ pdftk mydoc.pdf output mydoc.128.pdf owner_pw foo user_pw baz

3. Same as Above, Except Printing is Allowed (after the PDF is Open)

$ pdftk mydoc.pdf output mydoc.128.pdf owner_pw foo user_pw baz allow printing

4. Decrypt a PDF

$ pdftk secured.pdf input_pw foopass output unsecured.pdf


NOTE:
Setting an owner password prevents the document from being modified unless the password is provided. In the same way, setting a user password prevents the user to view the file.

Design a site like this with WordPress.com
Get started