1 replace file extensions
  2 # change .htm files to .html  
  3 for file in *.htm ; do mv $file `echo $file | sed 's/\(.*\.\)htm/\1html/'` ; done
  4 # change .html files to .htm  
  5 for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1htm/'` ; done
  6 #change .html files to .shtml 
  7 for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1shtml/'` ; done
  8 #change .html files to php    
  9 for file in *.html ; do mv $file `echo $file | sed 's/\(.*\.\)html/\1php/'` ; done
 10 
 11 replace string in text
 12 :%s/str1/str2/gc  整个文档  
 13 :1,$s/str1/str2/gc  整个文档  
 14 :.,$s/str1/str2/gc  从当前到结尾  
 15 
 16 #extract a range of lines from a text
 17 sed -n 16224,16482p filename > newfile
 18 sed -n -e 12345p filename > newfile
 19 sed '5!d' filename > newfile
 20 
 21 # compare substring of a field
 22 awk '{if ( substr($1,0,7)>6435201 && substr($1,0,7)<6521605) print $0}'
 23 
 24 # find a file
 25 find ./ -name 'my*'
 26 # find & cp
 27 find /PATH/TO/YOUR/FILES -name NAME.EXT -exec cp -rfp {} /DST_DIR \;
 28 
 29 
 30 # add numbers in a bash script
 31 num=$((num1 + num2))
 32 num=$(($num1 + $num2))       # also works
 33 num=$((num1 + 2 + 3))        # ...
 34 num=$[num1+num2]             # old, deprecated arithmetic expression syntax
 35 
 36 
 37 # Assign Output of Shell Command To Variable
 38 var=$(command-name-here)
 39 var=$(command-name-here arg1)
 40 var=$(/path/to/command)
 41 var=$(/path/to/command arg1 arg2)
 42 var=`command-name-here`
 43 var=`command-name-here arg1`
 44 var=`/path/to/command`
 45 var=`/path/to/command arg1 arg2`
 46 
 47 # fg/bg/nohup
 48 find /etc/httpd/ -name "httpd.conf" -print >find.dt 2>&1 & 
 49 
 50 awk '{if ($11=="9002" && $6==0) {revenue+=$3;count++}}END{print revenue;print count}' new_ck_table 
 51 awk '{if ($3=="+1") count++;}END{print count;}' file.txt 
 52 awk 'FNR==NR{a[$1]=1; next}; {if($1 in a) print $0} ' rank02.dat 0201-all.tmp3   >  rank02.tmp3
 53 
 54 # output third column to end of each row
 55 cut -d ":" -f 3-
 56 # filter empty lines
 57 grep -e '^$' -v 
 58 
 59 # comment /uncomment
 60 ctrl+v -> x
 61 ctrl+v -> shift+i # esc
 62 
 63 # sort by one column
 64 sort -k 1 0316.new -o 0316.new 
 65 
 66 # remove leading space
 67 sed -e 's/^[ \t]*//'
 68 
 69 # mount NFS
 70 mount -t nfs 10.134.12.60:/data/online/public localDir
 71 
 72 
 73 ##--------------- high frequency python command ------------------------ 
 74 
 75 # regex
 76 
 77 text = "<H1>title</H1>"
 78 re.mathc('<.*>', text) # match <H1>title</H1>
 79 re.match('<.*?>',text)  # match <H1>
 80 
 81 
 82 
 83 # sum of elements in a list
 84 sum = reduce(lambda x,y: x+y, mylist)
 85 
 86 # compare all elements in two lists
 87 if all(x==y for x, y in zip(X, Y))
 88     do something
 89 
 90     
 91 # get sorted dictionary by key
 92 sorted(dd.keys())
 93 [(key, dd[key]) for key in sorted(dd.keys())]
 94 print ' '.join([str(key)+':'+str(dd[key]) for key in sorted(dd.keys())])
 95 
 96     
 97 # get sorted dictionary by value
 98 [key for key in sorted(dd, key=dd.get, reverse=True)]
 99 [(key, dd[key]) for key in sorted(dd, key=dd.get, reverse=True)]
100 print ' '.join([str(key)+':'+str(dd[key]) for key in sorted(dd, key=dd.get, reverse=True)])
101 sorted(myDict.items(), key=lambda e: e[1][2]) # value is a list
102 
103 # get key intersection of two dictionaries
104 intersect = set(dict_A.keys()) & set(dict_B.keys()) 
105 
106 # sort a list of tuple
107 sorted_list = sorted(tuples, key=lambda x:x[0])
108 
109 # map list onto dictionary
110 mydict = {x.split(':')[0]:x.split(':')[1] for x in mylist} 
111 
112 from os.path import basename
113 from os.path import splitext
114 fname = splitext(basename(fullname))[0]
115 
116 # sort list and return index of list
117 sorted_index = sorted(range(len(list)), key=lambda k: list[k])
118 
119 # intersection of two lists
120 b1 = [...]
121 b2 = [...]
122 intersection = set(b1).intersection(b2)
123 
124 # string to date / date to string
125 import  datetime
126 obj_date = datetime.datetime.strptime(str_date, "%Y%m%d%H%M").date()
127 str_date = obj_date.strftime('%Y%m%d%H%M')
128 obj_date =  obj_date + datetime.timedelta(days=-1)
129 # date to timestamp
130 time.mktime(time.strptime('2015-03-15 00:00:00', '%Y-%m-%d %H:%M:%S'))
131 
132 # read first N line of a file 
133 with open("datafile") as myfile:
134     head = [next(myfile) for x in xrange(N)]
135 print head

 

# remove leading whitespace in vim
%s/^\s*//g