replace file extensions
# change .htm files to .html
for file in *.htm ; do mv $file `echo $file | sed 's/\(.*\.\)htm/\1html/'` ; done
# change .html files to .htm
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1htm/'` ; done
#change .html files to .shtml
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1shtml/'` ; done
#change .html files to php
for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1php/'` ; done replace string in text
:%s/str1/str2/gc 整个文档
:1,$s/str1/str2/gc 整个文档
:.,$s/str1/str2/gc 从当前到结尾 #extract a range of lines from a text
sed -n 16224,16482p filename > newfile
sed -n -e 12345p filename > newfile
sed '5!d' filename > newfile # compare substring of a field
awk '{if ( substr($1,0,7)>6435201 && substr($1,0,7)<6521605) print $0}' # find a file
find ./ -name 'my*'
# find & cp
find /PATH/TO/YOUR/FILES -name NAME.EXT -exec cp -rfp {} /DST_DIR \; # add numbers in a bash script
num=$((num1 + num2))
num=$(($num1 + $num2)) # also works
num=$((num1 + 2 + 3)) # ...
num=$[num1+num2] # old, deprecated arithmetic expression syntax # Assign Output of Shell Command To Variable
var=$(command-name-here)
var=$(command-name-here arg1)
var=$(/path/to/command)
var=$(/path/to/command arg1 arg2)
var=`command-name-here`
var=`command-name-here arg1`
var=`/path/to/command`
var=`/path/to/command arg1 arg2` # fg/bg/nohup
find /etc/httpd/ -name "httpd.conf" -print >find.dt 2>&1 & awk '{if ($11=="9002" && $6==0) {revenue+=$3;count++}}END{print revenue;print count}' new_ck_table
awk '{if ($3=="+1") count++;}END{print count;}' file.txt
awk 'FNR==NR{a[$1]=1; next}; {if($1 in a) print $0} ' rank02.dat 0201-all.tmp3 > rank02.tmp3 # output third column to end of each row
cut -d ":" -f 3-
# filter empty lines
grep -e '^$' -v # comment /uncomment
ctrl+v -> x
ctrl+v -> shift+i # esc # sort by one column
sort -k 1 0316.new -o 0316.new # remove leading space
sed -e 's/^[ \t]*//' # mount NFS
mount -t nfs 10.134.12.60:/data/online/public localDir ##--------------- high frequency python command ------------------------ # regex text = "<H1>title</H1>"
re.mathc('<.*>', text) # match <H1>title</H1>
re.match('<.*?>',text) # match <H1> # sum of elements in a list
sum = reduce(lambda x,y: x+y, mylist) # compare all elements in two lists
if all(x==y for x, y in zip(X, Y))
do something # get sorted dictionary by key
sorted(dd.keys())
[(key, dd[key]) for key in sorted(dd.keys())]
print ' '.join([str(key)+':'+str(dd[key]) for key in sorted(dd.keys())]) # get sorted dictionary by value
[key for key in sorted(dd, key=dd.get, reverse=True)]
[(key, dd[key]) for key in sorted(dd, key=dd.get, reverse=True)]
print ' '.join([str(key)+':'+str(dd[key]) for key in sorted(dd, key=dd.get, reverse=True)])
sorted(myDict.items(), key=lambda e: e[1][2]) # value is a list # get key intersection of two dictionaries
intersect = set(dict_A.keys()) & set(dict_B.keys()) # sort a list of tuple
sorted_list = sorted(tuples, key=lambda x:x[0]) # map list onto dictionary
mydict = {x.split(':')[0]:x.split(':')[1] for x in mylist} from os.path import basename
from os.path import splitext
fname = splitext(basename(fullname))[0] # sort list and return index of list
sorted_index = sorted(range(len(list)), key=lambda k: list[k]) # intersection of two lists
b1 = [...]
b2 = [...]
intersection = set(b1).intersection(b2) # string to date / date to string
import datetime
obj_date = datetime.datetime.strptime(str_date, "%Y%m%d%H%M").date()
str_date = obj_date.strftime('%Y%m%d%H%M')
obj_date = obj_date + datetime.timedelta(days=-1)
# date to timestamp
time.mktime(time.strptime('2015-03-15 00:00:00', '%Y-%m-%d %H:%M:%S')) # read first N line of a file
with open("datafile") as myfile:
head = [next(myfile) for x in xrange(N)]
print head
# remove leading whitespace in vim
%s/^\s*//g