<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>小生这厢有礼了(BioFaceBook Personal Blog) &#187; 脚本语言</title>
	<atom:link href="https://www.biofacebook.com/?cat=8&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>https://www.biofacebook.com</link>
	<description>记录生物信息学点滴足迹（NGS,Genome,Meta,Linux)</description>
	<lastBuildDate>Sun, 23 Aug 2020 03:28:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.41</generator>
	<item>
		<title>dictionary to csv</title>
		<link>https://www.biofacebook.com/?p=1416</link>
		<comments>https://www.biofacebook.com/?p=1416#comments</comments>
		<pubDate>Thu, 07 Nov 2019 08:58:47 +0000</pubDate>
		<dc:creator><![CDATA[szypanther]]></dc:creator>
				<category><![CDATA[兴趣杂项]]></category>
		<category><![CDATA[脚本语言]]></category>

		<guid isPermaLink="false">http://www.biofacebook.com/?p=1416</guid>
		<description><![CDATA[<p>#!/usr/bin/env python import os,re,sys,string,commands,getopt,subprocess,glob,csv import prettytable as pt from os import path #SL335752_kneaddata_paired_1_kneaddata_paired_1.fastq.gz_rep.mpa.txt_bracken.txt #SL311013_1_kneaddata_paired_1_kneaddata_paired_1.fastq.gz_rep_mpa.txt_bracken.txt def main(): dic = {} unique_speciesname = [] speciesname = [] samplenames = [] for d in os.listdir(&#8216;.&#8217;): print(d) a = [] a = re.split(&#8220;_kneaddata_paired_1_&#8221;,d) if len(a) &#62;=2: samplename = a[0] dic[a[0]] = {} #os.rename(d, newname) fh = open(d, &#8216;r&#8217;) fhlines [...]]]></description>
				<content:encoded><![CDATA[<p>#!/usr/bin/env python<br />
import os,re,sys,string,commands,getopt,subprocess,glob,csv<br />
import prettytable as pt<br />
from os import path<br />
#SL335752_kneaddata_paired_1_kneaddata_paired_1.fastq.gz_rep.mpa.txt_bracken.txt<br />
#SL311013_1_kneaddata_paired_1_kneaddata_paired_1.fastq.gz_rep_mpa.txt_bracken.txt<br />
def main():<br />
dic = {}<br />
unique_speciesname = []<br />
speciesname = []<br />
samplenames = []<br />
for d in os.listdir(&#8216;.&#8217;):<br />
print(d)<br />
a = []<br />
a = re.split(&#8220;_kneaddata_paired_1_&#8221;,d)<br />
if len(a) &gt;=2:<br />
samplename = a[0]<br />
dic[a[0]] = {}<br />
#os.rename(d, newname)<br />
fh = open(d, &#8216;r&#8217;)<br />
fhlines = fh.readlines()<br />
fh.close()</p>
<p>for line in fhlines:<br />
line = line.strip()<br />
if re.search(&#8220;name&#8221;, line):<br />
continue<br />
else:<br />
b = []<br />
b = re.split(&#8220;\t&#8221;,line)<br />
speciesname.append(b[0])<br />
length = len(b)<br />
dic[a[0]][b[0]] = b[length-1]<br />
unique = [unique_speciesname.append(x) for x in speciesname if x not in unique_speciesname]<br />
for sample_name in dic.keys():<br />
samplenames.append(sample_name)<br />
for name in unique_speciesname:<br />
#print name<br />
if dic[sample_name].has_key(name):<br />
print_line = name + &#8220;\t&#8221; + dic[sample_name][name]<br />
else:<br />
dic[sample_name][name] = &#8220;0&#8221;<br />
print_line = name + &#8220;\t&#8221; + dic[sample_name][name]</p>
<p>#print dic<br />
csv_columns = [&#8216;Species&#8217;]<br />
for ele in samplenames:<br />
csv_columns.append(ele)<br />
print csv_columns<br />
#csv_columns = [&#8216;No&#8217;,&#8217;Name&#8217;,&#8217;Country&#8217;]<br />
csv_file = &#8220;sample_species.csv&#8221;<br />
#{&#8216;SL311014&#8242;: {&#8216;Acinetobacter sp. WCHA55&#8242;: &#8216;0.00029&#8217;, &#8216;Streptococcus sp. oral taxon 431&#8242;: &#8216;0.00007&#8217;, &#8216;Bacillus velezensis': &#8216;0.00001&#8217;, &#8216;Ahniella affigens': &#8216;0.00003&#8217;, &#8216;Arsenicicoccus sp. oral taxon 190&#8242;: &#8216;0.00077&#8217;, &#8216;Aureimonas sp. AU20&#8242;: &#8216;0&#8217;, &#8216;Mycobacterium sp. MS1601&#8242;: &#8216;0&#8217;, &#8216;Acinetobacter sp. WCHAc010052&#8242;: &#8216;0.00003&#8217;, &#8216;Candidatus Micrarchaeota archaeon Mia14&#8242;: &#8216;0.00000&#8217;, &#8216;Pseudomonas sp. MT-1&#8242;: &#8216;0.00005&#8217;, &#8216;Halothece sp. PCC 7418&#8242;: &#8216;0.00001&#8217;}, &#8216;SL311013&#8242;: {&#8216;Acinetobacter sp. WCHA55&#8242;: &#8216;0&#8217;, &#8216;Streptococcus sp. oral taxon 431&#8242;: &#8216;0.00009&#8217;, &#8216;Bacillus velezensis': &#8216;0.00004&#8217;, &#8216;Candidatus Micrarchaeota archaeon Mia14&#8242;: &#8216;0&#8217;, &#8216;Ahniella affigens': &#8216;0.00002&#8217;, &#8216;Arsenicicoccus sp. oral taxon 190&#8242;: &#8216;0.00059&#8217;, &#8216;Mycobacterium sp. MS1601&#8242;: &#8216;0.00029&#8217;, &#8216;Acinetobacter sp. WCHAc010052&#8242;: &#8216;0.00001&#8217;, &#8216;Aureimonas sp. AU20&#8242;: &#8216;0.00026&#8217;, &#8216;Pseudomonas sp. MT-1&#8242;: &#8216;0.00003&#8217;, &#8216;Halothece sp. PCC 7418&#8242;: &#8216;0.00001&#8217;}, &#8216;SL311012&#8242;: {&#8216;Acinetobacter sp. WCHA55&#8242;: &#8216;0&#8217;, &#8216;Streptococcus sp. oral taxon 431&#8242;: &#8216;0.00008&#8217;, &#8216;Bacillus velezensis': &#8216;0.00001&#8217;, &#8216;Candidatus Micrarchaeota archaeon Mia14&#8242;: &#8216;0&#8217;, &#8216;Ahniella affigens': &#8216;0.00001&#8217;, &#8216;Arsenicicoccus sp. oral taxon 190&#8242;: &#8216;0.00054&#8217;, &#8216;Mycobacterium sp. MS1601&#8242;: &#8216;0.00026&#8217;, &#8216;Acinetobacter sp. WCHAc010052&#8242;: &#8216;0.00002&#8217;, &#8216;Aureimonas sp. AU20&#8242;: &#8216;0.00024&#8217;, &#8216;Pseudomonas sp. MT-1&#8242;: &#8216;0.00003&#8217;, &#8216;Halothece sp. PCC 7418&#8242;: &#8216;0.00000&#8217;}}<br />
dict_data_mine = []<br />
#dic_ele = {}<br />
for name in unique_speciesname:<br />
dic_ele = {}<br />
dic_ele[&#8220;Species&#8221;]=name<br />
for key in dic.keys():<br />
#dic_ele[&#8220;speciesname&#8221;]=&gt;name<br />
dic_ele[key]=dic[key][name]<br />
dict_data_mine.append(dic_ele)<br />
dict_data = [<br />
{&#8216;No': 1, &#8216;Name': &#8216;Alex&#8217;, &#8216;Country': &#8216;India&#8217;},<br />
{&#8216;No': 2, &#8216;Name': &#8216;Ben&#8217;, &#8216;Country': &#8216;USA&#8217;},<br />
{&#8216;No': 3, &#8216;Name': &#8216;Shri Ram&#8217;, &#8216;Country': &#8216;India&#8217;},<br />
{&#8216;No': 4, &#8216;Name': &#8216;Smith&#8217;, &#8216;Country': &#8216;USA&#8217;},<br />
{&#8216;No': 5, &#8216;Name': &#8216;Yuva Raj&#8217;, &#8216;Country': &#8216;India&#8217;},<br />
]</p>
<p>#print dict_data_mine<br />
try:<br />
with open(csv_file, &#8216;w&#8217;) as csvfile:<br />
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)<br />
writer.writeheader()<br />
for data in dict_data_mine:<br />
writer.writerow(data)<br />
except IOError:<br />
print(&#8220;I/O error&#8221;)<br />
if __name__== &#8216;__main__':<br />
main()</p>
]]></content:encoded>
			<wfw:commentRss>https://www.biofacebook.com/?feed=rss2&#038;p=1416</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>batch script in python</title>
		<link>https://www.biofacebook.com/?p=1413</link>
		<comments>https://www.biofacebook.com/?p=1413#comments</comments>
		<pubDate>Mon, 14 Oct 2019 08:11:51 +0000</pubDate>
		<dc:creator><![CDATA[szypanther]]></dc:creator>
				<category><![CDATA[脚本语言]]></category>

		<guid isPermaLink="false">http://www.biofacebook.com/?p=1413</guid>
		<description><![CDATA[<p>#!/usr/bin/env python import os,re,sys,string,commands,getopt,subprocess,glob from os import path #/disk/rdisk08/mhyleung/loreal_shotgun2/NovaSeq_new/decontam_output_genome/paired_completed/szy_test #LOR294C_S130_R1_001_kneaddata_paired_1_decontaminated.fastq.paired.fq.gz</p> <p>#LOR294C_S130_R1_001_kneaddata_paired_1.fastq.gz #metawrap binning -o /home/mhyleung/workspace/loreal_shotgun2/NovaSeq_new/metawrap_assembly_R2/LOR303C -t 16 -a /home/mhyleung/workspace/loreal_shotgun2/NovaSeq_new/metawrap_assembly_R2/LOR303C_S134_R1_001_kneaddata_paired/LOR303C_final_assembly.fasta &#8211;maxbin2 &#8211;metabat2 &#8211;concoct /disk/rdisk08/mhyleung/loreal_shotgun2/NovaSeq_new/decontam_output_genome/paired_completed/szy_test/LOR303C_S134_R1_001_kneaddata_paired_*.fastq #host = subprocess.Popen([&#8216;host&#8217;, target], stdout = subprocess.PIPE).communicate()[0] #p1 = subprocess.Popen([&#8216;ping&#8217;, &#8216;-c 2&#8242;, host], stdout=subprocess.PIPE)</p> <p>&#160;</p> <p>&#160;</p> <p>def main(): for d in os.listdir(&#8216;/disk/rdisk08/mhyleung/loreal_shotgun2/NovaSeq_new/decontam_output_genome/paired_completed/szy_test&#8217;): #print(d) if re.search(&#8220;_1.fastq&#8221;, d): a = [] a = re.split(&#8220;_&#8221;,d) [...]]]></description>
				<content:encoded><![CDATA[<p>#!/usr/bin/env python<br />
import os,re,sys,string,commands,getopt,subprocess,glob<br />
from os import path<br />
#/disk/rdisk08/mhyleung/loreal_shotgun2/NovaSeq_new/decontam_output_genome/paired_completed/szy_test<br />
#LOR294C_S130_R1_001_kneaddata_paired_1_decontaminated.fastq.paired.fq.gz</p>
<p>#LOR294C_S130_R1_001_kneaddata_paired_1.fastq.gz<br />
#metawrap binning -o /home/mhyleung/workspace/loreal_shotgun2/NovaSeq_new/metawrap_assembly_R2/LOR303C -t 16 -a /home/mhyleung/workspace/loreal_shotgun2/NovaSeq_new/metawrap_assembly_R2/LOR303C_S134_R1_001_kneaddata_paired/LOR303C_final_assembly.fasta &#8211;maxbin2 &#8211;metabat2 &#8211;concoct /disk/rdisk08/mhyleung/loreal_shotgun2/NovaSeq_new/decontam_output_genome/paired_completed/szy_test/LOR303C_S134_R1_001_kneaddata_paired_*.fastq<br />
#host = subprocess.Popen([&#8216;host&#8217;, target], stdout = subprocess.PIPE).communicate()[0]<br />
#p1 = subprocess.Popen([&#8216;ping&#8217;, &#8216;-c 2&#8242;, host], stdout=subprocess.PIPE)</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>def main():<br />
for d in os.listdir(&#8216;/disk/rdisk08/mhyleung/loreal_shotgun2/NovaSeq_new/decontam_output_genome/paired_completed/szy_test&#8217;):<br />
#print(d)<br />
if re.search(&#8220;_1.fastq&#8221;, d):<br />
a = []<br />
a = re.split(&#8220;_&#8221;,d)<br />
b = re.split(&#8220;kneaddata_paired&#8221;, d)<br />
outputfile = &#8220;/home/mhyleung/workspace/loreal_shotgun2/NovaSeq_new/metawrap_assembly_R2/&#8221; + a[0]<br />
assemfasta_file = &#8220;/home/mhyleung/workspace/loreal_shotgun2/NovaSeq_new/metawrap_assembly_R2/&#8221;+a[0]+&#8221;_&#8221;+a[1]+&#8221;_&#8221;+a[2]+&#8221;_&#8221;+a[3]+&#8221;_&#8221;+a[4]+&#8221;_&#8221;+a[5]+&#8221;/&#8221;+a[0]+&#8221;_final_assembly.fasta&#8221;<br />
fastq_file = &#8220;/disk/rdisk08/mhyleung/loreal_shotgun2/NovaSeq_new/decontam_output_genome/paired_completed/szy_test/&#8221; + b[0] +&#8221;kneaddata_paired_*.fastq&#8221;<br />
fastq_file1 = &#8220;/disk/rdisk08/mhyleung/loreal_shotgun2/NovaSeq_new/decontam_output_genome/paired_completed/szy_test/&#8221; + b[0] +&#8221;kneaddata_paired_1.fastq&#8221;<br />
fastq_file2 = &#8220;/disk/rdisk08/mhyleung/loreal_shotgun2/NovaSeq_new/decontam_output_genome/paired_completed/szy_test/&#8221; + b[0] +&#8221;kneaddata_paired_2.fastq&#8221;<br />
fastq_file1_gz = &#8220;/disk/rdisk08/mhyleung/loreal_shotgun2/NovaSeq_new/decontam_output_genome/paired_completed/szy_test/&#8221; + b[0] +&#8221;kneaddata_paired_1.fastq.gz&#8221;<br />
fastq_file2_gz = &#8220;/disk/rdisk08/mhyleung/loreal_shotgun2/NovaSeq_new/decontam_output_genome/paired_completed/szy_test/&#8221; + b[0] +&#8221;kneaddata_paired_2.fastq.gz&#8221;<br />
command_line = &#8221; metawrap binning -o &#8221; + outputfile + &#8221; -t 10 -a &#8221; + assemfasta_file + &#8221; &#8211;maxbin2 &#8211;metabat2 &#8221; + fastq_file<br />
print command_line<br />
#tar_command_line1 = &#8220;&#8211;remove-files -zcvf &#8221; + fastq_file1_gz + &#8221; &#8221; + fastq_file1<br />
#tar_command_line2 = fastq_file2_gz + &#8221; &#8221; + fastq_file2<br />
subprocess.Popen([&#8216;gunzip&#8217;, fastq_file1_gz], stdout = subprocess.PIPE).communicate()[0]<br />
subprocess.Popen([&#8216;gunzip&#8217;, fastq_file2_gz], stdout = subprocess.PIPE).communicate()[0]<br />
subprocess.Popen([&#8216;metawrap&#8217;,&#8217;binning&#8217;,&#8217;-o&#8217;, outputfile,&#8217;-t 10&#8242;,&#8217;-a&#8217;, assemfasta_file, &#8216;&#8211;maxbin2&#8242;, &#8216;&#8211;metabat2&#8242;, fastq_file], stdout = subprocess.PIPE).communicate()[0]<br />
subprocess.Popen([&#8216;tar&#8217;, &#8216;&#8211;remove-files&#8217;, &#8216;-zcvf&#8217;, fastq_file1_gz, fastq_file1], stdout = subprocess.PIPE).communicate()[0]<br />
subprocess.Popen([&#8216;tar&#8217;, &#8216;&#8211;remove-files&#8217;, &#8216;-zcvf&#8217;, fastq_file2_gz, fastq_file2], stdout = subprocess.PIPE).communicate()[0]</p>
<p>work_files_path = outputfile + &#8220;/work_files&#8221;<br />
subprocess.Popen([&#8216;rm&#8217;,&#8217;-rf&#8217;, work_files_path], stdout = subprocess.PIPE).communicate()[0]</p>
<p># os.system(&#8216;gunzip newname&#8217;)<br />
#for file in glob.glob(&#8220;*.fasta&#8221;):<br />
# os.rename(&#8220;final_assembly.fasta&#8221;, newname)<br />
#os.chdir(&#8216;/home/mhyleung/workspace/loreal_shotgun2/NovaSeq_new/metawrap_assembly_R2/&#8217;)<br />
#change_dir = os.system(&#8216;pwd&#8217;)<br />
#print change_dir<br />
#os.chdir(change_dir)</p>
<p>if __name__== &#8216;__main__':<br />
main()</p>
]]></content:encoded>
			<wfw:commentRss>https://www.biofacebook.com/?feed=rss2&#038;p=1413</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>linux shell 用sed命令在文本的行尾或行首添加字符</title>
		<link>https://www.biofacebook.com/?p=729</link>
		<comments>https://www.biofacebook.com/?p=729#comments</comments>
		<pubDate>Fri, 01 Mar 2013 03:28:48 +0000</pubDate>
		<dc:creator><![CDATA[szypanther]]></dc:creator>
				<category><![CDATA[Linux相关]]></category>
		<category><![CDATA[脚本语言]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://www.biofacebook.com/?p=729</guid>
		<description><![CDATA[ <p>昨天写一个脚本花了一天的2/3的时间，而且大部分时间都耗在了sed命令上，今天不总结一下都对不起昨天流逝的时间啊~~~</p> <p>&#160;</p> <p>用sed命令在行首或行尾添加字符的命令有以下几种：</p> <p>假设处理的文本为test.file</p> <p></p> <p>在每行的头添加字符，比如&#8221;HEAD&#8221;，命令如下：</p> <p>sed &#8216;s/^/HEAD&#38;/g&#8217; test.file</p> <p>在每行的行尾添加字符，比如“TAIL”，命令如下：</p> <p>sed &#8216;s/$/&#38;TAIL/g&#8217; test.file</p> <p>运行结果如下图：</p> <p></p> <p>几点说明：</p> <p>1.&#8221;^&#8221;代表行首，&#8221;$&#8221;代表行尾</p> <p>2.&#8217;s/$/&#38;TAIL/g&#8217;中的字符g代表每行出现的字符全部替换，如果想在特定字符处添加，g就有用了，否则只会替换每行第一个，而不继续往后找了</p> <p>例：</p> <p></p> <p>3.如果想导出文件，在命令末尾加&#8221;&#62; outfile_name&#8221;；如果想在原文件上更改，添加选项&#8221;-i&#8221;，如</p> <p></p> <p>4.也可以把两条命令和在一起，在test.file的每一行的行头和行尾分别添加字符&#8221;HEAD&#8221;、“TAIL”，命令：sed &#8216;/./{s/^/HEAD&#38;/;s/$/&#38;TAIL/}&#8217; test.file</p> <p>&#160;</p> <p>&#160;</p> <p>以上其实都还OK，昨天花太多时间，主要因为被处理的文件是用mysql从数据库提取的结果导出来的，别人给我之后我就直接处理，太脑残了= -我一直有点怀疑之所以结果不对，有可能是windows和linux换行的问题，可是因为对sed不熟，就一直在搞sed。。。。。。。</p> <p>&#160;</p> <p>众所周知（= -），window和linux的回车换行之云云，如果你知道了，跳过这一段，不知道，读一下呗：</p> <p>Unix系统里，每行结尾只有“&#60;换行&#62;”，即“\n”；Windows系统里面，每行结尾是“&#60;换行&#62;&#60;回 车&#62;”，即“\n\r”。一个直接后果是，Unix系统下的文件在Windows里打开的话，所有文字会变成一行；而Windows里的文件在Unix下打开的话，在每行的结尾可能会多出一个^M符号。</p> <p>&#160;</p> <p>好了，所以我的问题就出在被处理的文件的每行末尾都有^M符号，而这通常是看不出来的。可以用&#8221;cat -A test.file&#8221;命令查看。因此当我想在行尾添加字符的时候，它总是添加在行首且会覆盖掉原来行首的字符。</p> <p>要把文件转换一下，有两种方法：</p> <p>1.命令dos2unix test.file</p> <p>2.去掉&#8221;\r&#8221; ，用命令sed -i &#8216;s/\r//&#8217; test.file</p> <p>好了，这样处理完，就OK啦！！！</p> [...]]]></description>
				<content:encoded><![CDATA[<div id="cnblogs_post_body">
<p>昨天写一个脚本花了一天的2/3的时间，而且大部分时间都耗在了sed命令上，今天不总结一下都对不起昨天流逝的时间啊~~~</p>
<p>&nbsp;</p>
<p>用sed命令在行首或行尾添加字符的命令有以下几种：</p>
<p>假设处理的文本为test.file</p>
<p><img src="http://pic002.cnblogs.com/images/2011/322833/2011081909444013.png" alt="" /></p>
<p>在每行的头添加字符，比如&#8221;HEAD&#8221;，命令如下：</p>
<p>sed &#8216;s/^/HEAD&amp;/g&#8217; test.file</p>
<p>在每行的行尾添加字符，比如“TAIL”，命令如下：</p>
<p>sed &#8216;s/$/&amp;TAIL/g&#8217; test.file</p>
<p>运行结果如下图：</p>
<p><img src="http://pic002.cnblogs.com/images/2011/322833/2011081909472161.png" alt="" /></p>
<p>几点说明：</p>
<p>1.&#8221;^&#8221;代表行首，&#8221;$&#8221;代表行尾</p>
<p>2.&#8217;s/$/&amp;TAIL/g&#8217;中的字符g代表每行出现的字符全部替换，如果想在特定字符处添加，g就有用了，否则只会替换每行第一个，而不继续往后找了</p>
<p>例：</p>
<p><img src="http://pic002.cnblogs.com/images/2011/322833/2011081909524973.png" alt="" /></p>
<p>3.如果想导出文件，在命令末尾加&#8221;&gt; outfile_name&#8221;；如果想在原文件上更改，添加选项&#8221;-i&#8221;，如</p>
<p><img src="http://pic002.cnblogs.com/images/2011/322833/2011081909563451.png" alt="" /></p>
<p>4.也可以把两条命令和在一起，在test.file的每一行的行头和行尾分别添加字符&#8221;HEAD&#8221;、“TAIL”，命令：sed &#8216;/./{s/^/HEAD&amp;/;s/$/&amp;TAIL/}&#8217; test.file</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>以上其实都还OK，昨天花太多时间，主要因为被处理的文件是用mysql从数据库提取的结果导出来的，别人给我之后我就直接处理，太脑残了= -我一直有点怀疑之所以结果不对，有可能是windows和linux换行的问题，可是因为对sed不熟，就一直在搞sed。。。。。。。</p>
<p>&nbsp;</p>
<p>众所周知（= -），window和linux的回车换行之云云，如果你知道了，跳过这一段，不知道，读一下呗：</p>
<p>Unix系统里，每行结尾只有“&lt;换行&gt;”，即“\n”；Windows系统里面，每行结尾是“&lt;换行&gt;&lt;回 车&gt;”，即“\n\r”。一个直接后果是，Unix系统下的文件在Windows里打开的话，所有文字会变成一行；而Windows里的文件在Unix下打开的话，在每行的结尾可能会多出一个^M符号。</p>
<p>&nbsp;</p>
<p>好了，所以我的问题就出在被处理的文件的每行末尾都有^M符号，而这通常是看不出来的。可以用&#8221;cat -A test.file&#8221;命令查看。因此当我想在行尾添加字符的时候，它总是添加在行首且会覆盖掉原来行首的字符。</p>
<p>要把文件转换一下，有两种方法：</p>
<p>1.命令dos2unix test.file</p>
<p>2.去掉&#8221;\r&#8221; ，用命令sed -i &#8216;s/\r//&#8217; test.file</p>
<p>好了，这样处理完，就OK啦！！！</p>
<div></div>
</div>
<div id="MySignature"></div>
<div></div>
<div id="blog_post_info_block"></div>
]]></content:encoded>
			<wfw:commentRss>https://www.biofacebook.com/?feed=rss2&#038;p=729</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python 字符串操作（string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等</title>
		<link>https://www.biofacebook.com/?p=656</link>
		<comments>https://www.biofacebook.com/?p=656#comments</comments>
		<pubDate>Fri, 30 Nov 2012 07:50:56 +0000</pubDate>
		<dc:creator><![CDATA[szypanther]]></dc:creator>
				<category><![CDATA[脚本语言]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.biofacebook.com/?p=656</guid>
		<description><![CDATA[Python 字符串操作（string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等） s.strip() .lstrip() .rstrip(',') 去空格及特殊符号 复制字符串 <p>Python</p> 1 #strcpy(sStr1,sStr2) 2 sStr1 = 'strcpy' 3 sStr2 = sStr1 4 sStr1 = 'strcpy2' 5 print sStr2 连接字符串 <p>Python</p> 1 #strcat(sStr1,sStr2) 2 sStr1 = 'strcat' 3 sStr2 = 'append' 4 sStr1 += sStr2 5 print sStr1 查找字符 <p>&#60; 0 未找到</p> <p>Python</p> 1 #strchr(sStr1,sStr2) 2 sStr1 = 'strchr' [...]]]></description>
				<content:encoded><![CDATA[<div><a name="cb_post_title_url" href="http://www.cnblogs.com/huangcong/archive/2011/08/29/2158268.html"></a>Python 字符串操作（string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等）</div>
<pre>s.strip() .lstrip() .rstrip(',') 去空格及特殊符号</pre>
<h2>复制字符串</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>#strcpy(sStr1,sStr2)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>sStr1</code> <code>=</code> <code>'strcpy'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>sStr2</code> <code>=</code> <code>sStr1</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>sStr1</code> <code>=</code> <code>'strcpy2'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>print</code> <code>sStr2</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>连接字符串</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>#strcat(sStr1,sStr2)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>sStr1</code> <code>=</code> <code>'strcat'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>sStr2</code> <code>=</code> <code>'append'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>sStr1</code> <code>+</code><code>=</code> <code>sStr2</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>print</code> <code>sStr1</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>查找字符</h2>
<p>&lt; 0 未找到</p>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>#strchr(sStr1,sStr2)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>sStr1</code> <code>=</code> <code>'strchr'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>sStr2</code> <code>=</code> <code>'s'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>nPos</code> <code>=</code> <code>sStr1.index(sStr2)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>print</code> <code>nPos</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>比较字符串</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>#strcmp(sStr1,sStr2)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>sStr1</code> <code>=</code> <code>'strchr'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>sStr2</code> <code>=</code> <code>'strch'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>print</code> <code>cmp</code><code>(sStr1,sStr2)</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>扫描字符串是否包含指定的字符</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>#strspn(sStr1,sStr2)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>sStr1</code> <code>=</code> <code>'12345678'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>sStr2</code> <code>=</code> <code>'456'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>#sStr1 and chars both in sStr1 and sStr2</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>print</code> <code>len</code><code>(sStr1</code> <code>and</code> <code>sStr2)</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>字符串长度</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>#strlen(sStr1)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>sStr1</code> <code>=</code> <code>'strlen'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>print</code> <code>len</code><code>(sStr1)</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>将字符串中的大小写转换</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>#strlwr(sStr1)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>sStr1</code> <code>=</code> <code>'JCstrlwr'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>sStr1</code> <code>=</code> <code>sStr1.upper()</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>#sStr1 = sStr1.lower()</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>print</code> <code>sStr1</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>追加指定长度的字符串</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>#strncat(sStr1,sStr2,n)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>sStr1</code> <code>=</code> <code>'12345'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>sStr2</code> <code>=</code> <code>'abcdef'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>n</code> <code>=</code> <code>3</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>sStr1</code> <code>+</code><code>=</code> <code>sStr2[</code><code>0</code><code>:n]</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>6</code></td>
<td><code>print</code> <code>sStr1</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>字符串指定长度比较</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>#strncmp(sStr1,sStr2,n)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>sStr1</code> <code>=</code> <code>'12345'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>sStr2</code> <code>=</code> <code>'123bc'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>n</code> <code>=</code> <code>3</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>print</code> <code>cmp</code><code>(sStr1[</code><code>0</code><code>:n],sStr2[</code><code>0</code><code>:n])</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>复制指定长度的字符</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>#strncpy(sStr1,sStr2,n)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>sStr1</code> <code>=</code> <code>''</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>sStr2</code> <code>=</code> <code>'12345'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>n</code> <code>=</code> <code>3</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>sStr1</code> <code>=</code> <code>sStr2[</code><code>0</code><code>:n]</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>6</code></td>
<td><code>print</code> <code>sStr1</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>将字符串前n个字符替换为指定的字符</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>#strnset(sStr1,ch,n)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>sStr1</code> <code>=</code> <code>'12345'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>ch</code> <code>=</code> <code>'r'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>n</code> <code>=</code> <code>3</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>sStr1</code> <code>=</code> <code>n</code> <code>*</code> <code>ch</code> <code>+</code> <code>sStr1[</code><code>3</code><code>:]</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>6</code></td>
<td><code>print</code> <code>sStr1</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>扫描字符串</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>#strpbrk(sStr1,sStr2)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>sStr1</code> <code>=</code> <code>'cekjgdklab'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>sStr2</code> <code>=</code> <code>'gka'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>nPos</code> <code>=</code> <code>-</code><code>1</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>for</code> <code>c</code> <code>in</code> <code>sStr1:</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>6</code></td>
<td><code> <wbr> <wbr> <wbr> <wbr></wbr></wbr></wbr></wbr></code><code>if</code> <code>c</code> <code>in</code> <code>sStr2:</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>7</code></td>
<td><code> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></code><code>nPos</code> <code>=</code> <code>sStr1.index(c)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>8</code></td>
<td><code> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></code><code>break</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>9</code></td>
<td><code>print</code> <code>nPos</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>翻转字符串</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>#strrev(sStr1)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>sStr1</code> <code>=</code> <code>'abcdefg'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>sStr1</code> <code>=</code> <code>sStr1[::</code><code>-</code><code>1</code><code>]</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>print</code> <code>sStr1</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>查找字符串</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>#strstr(sStr1,sStr2)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>sStr1</code> <code>=</code> <code>'abcdefg'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>sStr2</code> <code>=</code> <code>'cde'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>print</code> <code>sStr1.find(sStr2)</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>分割字符串</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>#strtok(sStr1,sStr2)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>sStr1</code> <code>=</code> <code>'ab,cde,fgh,ijk'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>sStr2</code> <code>=</code> <code>','</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>sStr1</code> <code>=</code> <code>sStr1[sStr1.find(sStr2)</code> <code>+</code> <code>1</code><code>:]</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>print</code> <code>sStr1</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>6</code></td>
<td><code>或者</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>7</code></td>
<td><code>s</code> <code>=</code> <code>'ab,cde,fgh,ijk'</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>8</code></td>
<td><code>print</code><code>(s.split(</code><code>','</code><code>))</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>连接字符串</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>delimiter</code> <code>=</code> <code>','</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>mylist</code> <code>=</code> <code>[</code><code>'Brazil'</code><code>,</code> <code>'Russia'</code><code>,</code> <code>'India'</code><code>,</code> <code>'China'</code><code>]</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>print</code> <code>delimiter.join(mylist)</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>PHP 中 addslashes 的实现</h2>
<p>Python</p>
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>def</code> <code>addslashes(s):</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code> <wbr> <wbr> <wbr> <wbr></wbr></wbr></wbr></wbr></code><code>d</code> <code>=</code> <code>{</code><code>'"'</code><code>:</code><code>'\"'</code><code>, "</code><code>'":"\\'</code><code>", "</code><code>\</code><code>0</code><code>":"</code><code>\\\</code><code>0</code><code>", "</code><code>\\</code><code>":"</code><code>\\\"}</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code> <wbr> <wbr> <wbr> <wbr></wbr></wbr></wbr></wbr></code><code>return</code> <code>''.join(d.get(c, c)</code> <code>for</code> <code>c</code> <code>in</code> <code>s)</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td> <wbr></wbr></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>s</code> <code>=</code> <code>"John 'Johny' Doe (a.k.a. "Super Joe")\\\0"</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>6</code></td>
<td><code>print</code> <code>s</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>7</code></td>
<td><code>print</code> <code>addslashes(s)</code></td>
</tr>
</tbody>
</table>
</div>
</div>
<h2>只显示字母与数字</h2>
<p>Python</p>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>def</code> <code>OnlyCharNum(s,oth</code><code>=</code><code>''):</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code> <wbr> <wbr> <wbr> <wbr></wbr></wbr></wbr></wbr></code><code>s2</code> <code>=</code> <code>s.lower();</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code> <wbr> <wbr> <wbr> <wbr></wbr></wbr></wbr></wbr></code><code>fomart</code> <code>=</code> <code>'abcdefghijklmnopqrstuvwx<wbr>yz0123456789'</wbr></code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code> <wbr> <wbr> <wbr> <wbr></wbr></wbr></wbr></wbr></code><code>for</code> <code>c</code> <code>in</code> <code>s2:</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></code><code>if</code> <code>not</code> <code>c</code> <code>in</code> <code>fomart:</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>6</code></td>
<td><code> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr> <wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></code><code>s</code> <code>=</code> <code>s.replace(c,'');</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>7</code></td>
<td><code> <wbr> <wbr> <wbr> <wbr></wbr></wbr></wbr></wbr></code><code>return</code> <code>s;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>8</code></td>
<td> <wbr></wbr></td>
</tr>
</tbody>
</table>
</div>
<table>
<tbody>
<tr>
<td><code>9</code></td>
<td><code>print</code><code>(OnlyStr(</code><code>"a000 aa-b"</code><code>))</code></td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>https://www.biofacebook.com/?feed=rss2&#038;p=656</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python 自建标准差函数</title>
		<link>https://www.biofacebook.com/?p=499</link>
		<comments>https://www.biofacebook.com/?p=499#comments</comments>
		<pubDate>Wed, 22 Aug 2012 01:42:22 +0000</pubDate>
		<dc:creator><![CDATA[szypanther]]></dc:creator>
				<category><![CDATA[脚本语言]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.biofacebook.com/?p=499</guid>
		<description><![CDATA[<p>def stdDeviation(a): l=len(a) m=sum(a)/l d=0 for i in a:</p> <p>d+=(i-m)**2 return (d*(1/l))**0.5</p> <p>a=[5,6,8,9] print(stdDeviation(a)) ======== 1.5811388300841898</p> ]]></description>
				<content:encoded><![CDATA[<p>def stdDeviation(a):<br />
l=len(a)<br />
m=sum(a)/l<br />
d=0<br />
for i in a:</p>
<p>d+=(i-m)**2<br />
return (d*(1/l))**0.5</p>
<p>a=[5,6,8,9]<br />
print(stdDeviation(a))<br />
========<br />
1.5811388300841898</p>
]]></content:encoded>
			<wfw:commentRss>https://www.biofacebook.com/?feed=rss2&#038;p=499</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Good Useful one-line scripts for sed!</title>
		<link>https://www.biofacebook.com/?p=442</link>
		<comments>https://www.biofacebook.com/?p=442#comments</comments>
		<pubDate>Fri, 03 Aug 2012 02:20:02 +0000</pubDate>
		<dc:creator><![CDATA[szypanther]]></dc:creator>
				<category><![CDATA[Linux相关]]></category>
		<category><![CDATA[脚本语言]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://www.biofacebook.com/?p=442</guid>
		<description><![CDATA[------------------------------------------------------------------------- SED单行脚本快速参考（Unix 流编辑器） 2005年12月29日 英文标题：USEFUL ONE-LINE SCRIPTS FOR SED (Unix stream editor) 原标题：HANDY ONE-LINERS FOR SED (Unix stream editor) 整理：Eric Pement - 电邮：pemente[at]northpark[dot]edu 版本5.5 译者：Joe Hong - 电邮：hq00e[at]126[dot]com 在以下地址可找到本文档的最新（英文）版本： http://sed.sourceforge.net/sed1line.txt http://www.pement.org/sed/sed1line.txt 其他语言版本： 中文 - http://sed.sourceforge.net/sed1line_zh-CN.html 捷克语 - http://sed.sourceforge.net/sed1line_cz.html 荷语 - http://sed.sourceforge.net/sed1line_nl.html 法语 - http://sed.sourceforge.net/sed1line_fr.html 德语 - http://sed.sourceforge.net/sed1line_de.html 葡语 - http://sed.sourceforge.net/sed1line_pt-BR.html 文本间隔： -------- # 在每一行后面增加一空行 sed [...]]]></description>
				<content:encoded><![CDATA[<pre>-------------------------------------------------------------------------
SED单行脚本快速参考（Unix 流编辑器）                       2005年12月29日

英文标题：USEFUL ONE-LINE SCRIPTS FOR SED (Unix stream editor)
原标题：HANDY ONE-LINERS FOR SED (Unix stream editor)

整理：Eric Pement  - 电邮：pemente[at]northpark[dot]edu         版本5.5
译者：Joe Hong     - 电邮：hq00e[at]126[dot]com

在以下地址可找到本文档的最新（英文）版本：
   <a href="http://sed.sourceforge.net/sed1line.txt">http://sed.sourceforge.net/sed1line.txt</a>
   <a href="http://www.pement.org/sed/sed1line.txt">http://www.pement.org/sed/sed1line.txt</a>

其他语言版本：
  中文          - <a href="http://sed.sourceforge.net/sed1line_zh-CN.html">http://sed.sourceforge.net/sed1line_zh-CN.html</a>
  捷克语        - <a href="http://sed.sourceforge.net/sed1line_cz.html">http://sed.sourceforge.net/sed1line_cz.html</a>
  荷语          - <a href="http://sed.sourceforge.net/sed1line_nl.html">http://sed.sourceforge.net/sed1line_nl.html</a>
  法语          - <a href="http://sed.sourceforge.net/sed1line_fr.html">http://sed.sourceforge.net/sed1line_fr.html</a>
  德语          - <a href="http://sed.sourceforge.net/sed1line_de.html">http://sed.sourceforge.net/sed1line_de.html</a>

  葡语          - <a href="http://sed.sourceforge.net/sed1line_pt-BR.html">http://sed.sourceforge.net/sed1line_pt-BR.html</a>

文本间隔：
--------

 # 在每一行后面增加一空行
 sed G

 # 将原来的所有空行删除并在每一行后面增加一空行。
 # 这样在输出的文本中每一行后面将有且只有一空行。
 sed '/^$/d;G'

 # 在每一行后面增加两行空行
 sed 'G;G'

 # 将第一个脚本所产生的所有空行删除（即删除所有偶数行）
 sed 'n;d'

 # 在匹配式样“regex”的行之前插入一空行
 sed '/regex/{x;p;x;}'

 # 在匹配式样“regex”的行之后插入一空行
 sed '/regex/G'

 # 在匹配式样“regex”的行之前和之后各插入一空行
 sed '/regex/{x;p;x;G;}'

编号：
--------

 # 为文件中的每一行进行编号（简单的左对齐方式）。这里使用了“制表符”
 # （tab，见本文末尾关于'\t'的用法的描述）而不是空格来对齐边缘。
 sed = filename | sed 'N;s/\n/\t/'

 # 对文件中的所有行编号（行号在左，文字右端对齐）。
 sed = filename | sed 'N; s/^/     /; s/ *\(.\{6,\}\)\n/\1  /'

 # 对文件中的所有行编号，但只显示非空白行的行号。
 sed '/./=' filename | sed '/./N; s/\n/ /'

 # 计算行数 （模拟 "wc -l"）
 sed -n '$='

文本转换和替代：
--------

 # Unix环境：转换DOS的新行符（CR/LF）为Unix格式。
 sed 's/.$//'                     # 假设所有行以CR/LF结束
 sed 's/^M$//'                    # 在bash/tcsh中，将按Ctrl-M改为按Ctrl-V
 sed 's/\x0D$//'                  # ssed、gsed 3.02.80，及更高版本

 # Unix环境：转换Unix的新行符（LF）为DOS格式。
 sed "s/$/`echo -e \\\r`/"        # 在ksh下所使用的命令
 sed 's/$'"/`echo \\\r`/"         # 在bash下所使用的命令
 sed "s/$/`echo \\\r`/"           # 在zsh下所使用的命令
 sed 's/$/\r/'                    # gsed 3.02.80 及更高版本

 # DOS环境：转换Unix新行符（LF）为DOS格式。
 sed "s/$//"                      # 方法 1
 sed -n p                         # 方法 2

 # DOS环境：转换DOS新行符（CR/LF）为Unix格式。
 # 下面的脚本只对UnxUtils sed 4.0.7 及更高版本有效。要识别UnxUtils版本的
 #  sed可以通过其特有的“--text”选项。你可以使用帮助选项（“--help”）看
 # 其中有无一个“--text”项以此来判断所使用的是否是UnxUtils版本。其它DOS
 # 版本的的sed则无法进行这一转换。但可以用“tr”来实现这一转换。
 sed "s/\r//" infile &gt;outfile     # UnxUtils sed v4.0.7 或更高版本
 tr -d \r &lt;infile &gt;outfile        # GNU tr 1.22 或更高版本

 # 将每一行前导的“空白字符”（空格，制表符）删除
 # 使之左对齐
 sed 's/^[ \t]*//'                # 见本文末尾关于'\t'用法的描述

 # 将每一行拖尾的“空白字符”（空格，制表符）删除
 sed 's/[ \t]*$//'                # 见本文末尾关于'\t'用法的描述

 # 将每一行中的前导和拖尾的空白字符删除
 sed 's/^[ \t]*//;s/[ \t]*$//'

 # 在每一行开头处插入5个空格（使全文向右移动5个字符的位置）
 sed 's/^/     /'

 # 以79个字符为宽度，将所有文本右对齐
 sed -e :a -e 's/^.\{1,78\}$/ &amp;/;ta'  # 78个字符外加最后的一个空格

 # 以79个字符为宽度，使所有文本居中。在方法1中，为了让文本居中每一行的前
 # 头和后头都填充了空格。 在方法2中，在居中文本的过程中只在文本的前面填充
 # 空格，并且最终这些空格将有一半会被删除。此外每一行的后头并未填充空格。
 sed  -e :a -e 's/^.\{1,77\}$/ &amp; /;ta'                     # 方法1
 sed  -e :a -e 's/^.\{1,77\}$/ &amp;/;ta' -e 's/\( *\)\1/\1/'  # 方法2

 # 在每一行中查找字串“foo”，并将找到的“foo”替换为“bar”
 sed 's/foo/bar/'                 # 只替换每一行中的第一个“foo”字串
 sed 's/foo/bar/4'                # 只替换每一行中的第四个“foo”字串
 sed 's/foo/bar/g'                # 将每一行中的所有“foo”都换成“bar”
 sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # 替换倒数第二个“foo”
 sed 's/\(.*\)foo/\1bar/'            # 替换最后一个“foo”

 # 只在行中出现字串“baz”的情况下将“foo”替换成“bar”
 sed '/baz/s/foo/bar/g'

 # 将“foo”替换成“bar”，并且只在行中未出现字串“baz”的情况下替换
 sed '/baz/!s/foo/bar/g'

 # 不管是“scarlet”“ruby”还是“puce”，一律换成“red”
 sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'  #对多数的sed都有效
 gsed 's/scarlet\|ruby\|puce/red/g'               # 只对GNU sed有效

 # 倒置所有行，第一行成为最后一行，依次类推（模拟“tac”）。
 # 由于某些原因，使用下面命令时HHsed v1.5会将文件中的空行删除
 sed '1!G;h;$!d'               # 方法1
 sed -n '1!G;h;$p'             # 方法2

 # 将行中的字符逆序排列，第一个字成为最后一字，……（模拟“rev”）
 sed '/\n/!G;s/\(.\)\(.*\n\)/&amp;\2\1/;//D;s/.//'

 # 将每两行连接成一行（类似“paste”）
 sed '$!N;s/\n/ /'

 # 如果当前行以反斜杠“\”结束，则将下一行并到当前行末尾
 # 并去掉原来行尾的反斜杠
 sed -e :a -e '/\\$/N; s/\\\n//; ta'

 # 如果当前行以等号开头，将当前行并到上一行末尾
 # 并以单个空格代替原来行头的“=”
 sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'

 # 为数字字串增加逗号分隔符号，将“1234567”改为“1,234,567”
 gsed ':a;s/\B[0-9]\{3\}\&gt;/,&amp;/;ta'                     # GNU sed
 sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'  # 其他sed

 # 为带有小数点和负号的数值增加逗号分隔符（GNU sed）
 gsed -r ':a;s/(^|[^0-9.])([0-9]+)([0-9]{3})/\1\2,\3/g;ta'

 # 在每5行后增加一空白行 （在第5，10，15，20，等行后增加一空白行）
 gsed '0~5G'                      # 只对GNU sed有效
 sed 'n;n;n;n;G;'                 # 其他sed

选择性地显示特定行：
--------

 # 显示文件中的前10行 （模拟“head”的行为）
 sed 10q

 # 显示文件中的第一行 （模拟“head -1”命令）
 sed q

 # 显示文件中的最后10行 （模拟“tail”）
 sed -e :a -e '$q;N;11,$D;ba'

 # 显示文件中的最后2行（模拟“tail -2”命令）
 sed '$!N;$!D'

 # 显示文件中的最后一行（模拟“tail -1”）
 sed '$!d'                        # 方法1
 sed -n '$p'                      # 方法2

 # 显示文件中的倒数第二行
 sed -e '$!{h;d;}' -e x              # 当文件中只有一行时，输入空行
 sed -e '1{$q;}' -e '$!{h;d;}' -e x  # 当文件中只有一行时，显示该行
 sed -e '1{$d;}' -e '$!{h;d;}' -e x  # 当文件中只有一行时，不输出

 # 只显示匹配正则表达式的行（模拟“grep”）
 sed -n '/regexp/p'               # 方法1
 sed '/regexp/!d'                 # 方法2

 # 只显示“不”匹配正则表达式的行（模拟“grep -v”）
 sed -n '/regexp/!p'              # 方法1，与前面的命令相对应
 sed '/regexp/d'                  # 方法2，类似的语法

 # 查找“regexp”并将匹配行的上一行显示出来，但并不显示匹配行
 sed -n '/regexp/{g;1!p;};h'

 # 查找“regexp”并将匹配行的下一行显示出来，但并不显示匹配行
 sed -n '/regexp/{n;p;}'

 # 显示包含“regexp”的行及其前后行，并在第一行之前加上“regexp”所
 # 在行的行号 （类似“grep -A1 -B1”）
 sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h

 # 显示包含“AAA”、“BBB”或“CCC”的行（任意次序）
 sed '/AAA/!d; /BBB/!d; /CCC/!d'  # 字串的次序不影响结果

 # 显示包含“AAA”、“BBB”和“CCC”的行（固定次序）
 sed '/AAA.*BBB.*CCC/!d'

 # 显示包含“AAA”“BBB”或“CCC”的行 （模拟“egrep”）
 sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d    # 多数sed
 gsed '/AAA\|BBB\|CCC/!d'                        # 对GNU sed有效

 # 显示包含“AAA”的段落 （段落间以空行分隔）
 # HHsed v1.5 必须在“x;”后加入“G;”，接下来的3个脚本都是这样
 sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'

 # 显示包含“AAA”“BBB”和“CCC”三个字串的段落 （任意次序）
 sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'

 # 显示包含“AAA”、“BBB”、“CCC”三者中任一字串的段落 （任意次序）
 sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
 gsed '/./{H;$!d;};x;/AAA\|BBB\|CCC/b;d'         # 只对GNU sed有效

 # 显示包含65个或以上字符的行
 sed -n '/^.\{65\}/p'

 # 显示包含65个以下字符的行
 sed -n '/^.\{65\}/!p'            # 方法1，与上面的脚本相对应
 sed '/^.\{65\}/d'                # 方法2，更简便一点的方法

 # 显示部分文本——从包含正则表达式的行开始到最后一行结束
 sed -n '/regexp/,$p'

 # 显示部分文本——指定行号范围（从第8至第12行，含8和12行）
 sed -n '8,12p'                   # 方法1
 sed '8,12!d'                     # 方法2

 # 显示第52行
 sed -n '52p'                     # 方法1
 sed '52!d'                       # 方法2
 sed '52q;d'                      # 方法3, 处理大文件时更有效率

 # 从第3行开始，每7行显示一次    
 gsed -n '3~7p'                   # 只对GNU sed有效
 sed -n '3,${p;n;n;n;n;n;n;}'     # 其他sed

 # 显示两个正则表达式之间的文本（包含）
 sed -n '/Iowa/,/Montana/p'       # 区分大小写方式

选择性地删除特定行：
--------

 # 显示通篇文档，除了两个正则表达式之间的内容
 sed '/Iowa/,/Montana/d'

 # 删除文件中相邻的重复行（模拟“uniq”）
 # 只保留重复行中的第一行，其他行删除
 sed '$!N; /^\(.*\)\n\1$/!P; D'

 # 删除文件中的重复行，不管有无相邻。注意hold space所能支持的缓存
 # 大小，或者使用GNU sed。
 sed -n 'G; s/\n/&amp;&amp;/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'

 # 删除除重复行外的所有行（模拟“uniq -d”）
 sed '$!N; s/^\(.*\)\n\1$/\1/; t; D'

 # 删除文件中开头的10行
 sed '1,10d'

 # 删除文件中的最后一行
 sed '$d'

 # 删除文件中的最后两行
 sed 'N;$!P;$!D;$d'

 # 删除文件中的最后10行
 sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # 方法1
 sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # 方法2

 # 删除8的倍数行
 gsed '0~8d'                           # 只对GNU sed有效
 sed 'n;n;n;n;n;n;n;d;'                # 其他sed

 # 删除匹配式样的行
 sed '/pattern/d'                      # 删除含pattern的行。当然pattern
                                       # 可以换成任何有效的正则表达式

 # 删除文件中的所有空行（与“grep '.' ”效果相同）
 sed '/^$/d'                           # 方法1
 sed '/./!d'                           # 方法2

 # 只保留多个相邻空行的第一行。并且删除文件顶部和尾部的空行。
 # （模拟“cat -s”）
 sed '/./,/^$/!d'        #方法1，删除文件顶部的空行，允许尾部保留一空行
 sed '/^$/N;/\n$/D'      #方法2，允许顶部保留一空行，尾部不留空行

 # 只保留多个相邻空行的前两行。
 sed '/^$/N;/\n$/N;//D'

 # 删除文件顶部的所有空行
 sed '/./,$!d'

 # 删除文件尾部的所有空行
 sed -e :a -e '/^\n*$/{$d;N;ba' -e '}'  # 对所有sed有效
 sed -e :a -e '/^\n*$/N;/\n$/ba'        # 同上，但只对 gsed 3.02.*有效

 # 删除每个段落的最后一行
 sed -n '/^$/{p;h;};/./{x;/./p;}'

特殊应用：
--------

 # 移除手册页（man page）中的nroff标记。在Unix System V或bash shell下使
 # 用'echo'命令时可能需要加上 -e 选项。
 sed "s/.`echo \\\b`//g"    # 外层的双括号是必须的（Unix环境）
 sed 's/.^H//g'             # 在bash或tcsh中, 按 Ctrl-V 再按 Ctrl-H
 sed 's/.\x08//g'           # sed 1.5，GNU sed，ssed所使用的十六进制的表示方法

 # 提取新闻组或 e-mail 的邮件头
 sed '/^$/q'                # 删除第一行空行后的所有内容

 # 提取新闻组或 e-mail 的正文部分
 sed '1,/^$/d'              # 删除第一行空行之前的所有内容

 # 从邮件头提取“Subject”（标题栏字段），并移除开头的“Subject:”字样
 sed '/^Subject: */!d; s///;q'

 # 从邮件头获得回复地址
 sed '/^Reply-To:/q; /^From:/h; /./d;g;q'

 # 获取邮件地址。在上一个脚本所产生的那一行邮件头的基础上进一步的将非电邮
 # 地址的部分剃除。（见上一脚本）
 sed 's/ *(.*)//; s/&gt;.*//; s/.*[:&lt;] *//'

 # 在每一行开头加上一个尖括号和空格（引用信息）
 sed 's/^/&gt; /'

 # 将每一行开头处的尖括号和空格删除（解除引用）
 sed 's/^&gt; //'

 # 移除大部分的HTML标签（包括跨行标签）
 sed -e :a -e 's/&lt;[^&gt;]*&gt;//g;/&lt;/N;//ba'

 # 将分成多卷的uuencode文件解码。移除文件头信息，只保留uuencode编码部分。
 # 文件必须以特定顺序传给sed。下面第一种版本的脚本可以直接在命令行下输入；
 # 第二种版本则可以放入一个带执行权限的shell脚本中。（由Rahul Dhesi的一
 # 个脚本修改而来。）
 sed '/^end/,/^begin/d' file1 file2 ... fileX | uudecode   # vers. 1
 sed '/^end/,/^begin/d' "$@" | uudecode                    # vers. 2

 # 将文件中的段落以字母顺序排序。段落间以（一行或多行）空行分隔。GNU sed使用
 # 字元“\v”来表示垂直制表符，这里用它来作为换行符的占位符——当然你也可以
 # 用其他未在文件中使用的字符来代替它。
 sed '/./{H;d;};x;s/\n/={NL}=/g' file | sort | sed '1s/={NL}=//;s/={NL}=/\n/g'
 gsed '/./{H;d};x;y/\n/\v/' file | sort | sed '1s/\v//;y/\v/\n/'

 # 分别压缩每个.TXT文件，压缩后删除原来的文件并将压缩后的.ZIP文件
 # 命名为与原来相同的名字（只是扩展名不同）。（DOS环境：“dir /b”
 # 显示不带路径的文件名）。
 echo @echo off &gt;zipup.bat
 dir /b *.txt | sed "s/^\(.*\)\.TXT/pkzip -mo \1 \1.TXT/" &gt;&gt;zipup.bat

使用SED：Sed接受一个或多个编辑命令，并且每读入一行后就依次应用这些命令。
当读入第一行输入后，sed对其应用所有的命令，然后将结果输出。接着再读入第二
行输入，对其应用所有的命令……并重复这个过程。上一个例子中sed由标准输入设
备（即命令解释器，通常是以管道输入的形式）获得输入。在命令行给出一个或多
个文件名作为参数时，这些文件取代标准输入设备成为sed的输入。sed的输出将被
送到标准输出（显示器）。因此：

 cat filename | sed '10q'         # 使用管道输入
 sed '10q' filename               # 同样效果，但不使用管道输入
 sed '10q' filename &gt; newfile     # 将输出转移（重定向）到磁盘上

要了解sed命令的使用说明，包括如何通过脚本文件（而非从命令行）来使用这些命
令，请参阅《sed &amp; awk》第二版，作者Dale Dougherty和Arnold Robbins
（O'Reilly，1997；http://www.ora.com），《UNIX Text Processing》，作者
Dale Dougherty和Tim O'Reilly（Hayden Books，1987）或者是Mike Arst写的教
程——压缩包的名称是“U-SEDIT2.ZIP”（在许多站点上都找得到）。要发掘sed
的潜力，则必须对“正则表达式”有足够的理解。正则表达式的资料可以看
《Mastering Regular Expressions》作者Jeffrey Friedl（O'reilly 1997）。
Unix系统所提供的手册页（“man”）也会有所帮助（试一下这些命令
“man sed”、“man regexp”，或者看“man ed”中关于正则表达式的部分），但
手册提供的信息比较“抽象”——这也是它一直为人所诟病的。不过，它本来就不
是用来教初学者如何使用sed或正则表达式的教材，而只是为那些熟悉这些工具的人
提供的一些文本参考。

括号语法：前面的例子对sed命令基本上都使用单引号（'...'）而非双引号
（"..."）这是因为sed通常是在Unix平台上使用。单引号下，Unix的shell（命令
解释器）不会对美元符（$）和后引号（`...`）进行解释和执行。而在双引号下
美元符会被展开为变量或参数的值，后引号中的命令被执行并以输出的结果代替
后引号中的内容。而在“csh”及其衍生的shell中使用感叹号（!）时需要在其前
面加上转义用的反斜杠（就像这样：\!）以保证上面所使用的例子能正常运行
（包括使用单引号的情况下）。DOS版本的Sed则一律使用双引号（"..."）而不是
引号来圈起命令。

'\t'的用法：为了使本文保持行文简洁，我们在脚本中使用'\t'来表示一个制表
符。但是现在大部分版本的sed还不能识别'\t'的简写方式，因此当在命令行中为
脚本输入制表符时，你应该直接按TAB键来输入制表符而不是输入'\t'。下列的工
具软件都支持'\t'做为一个正则表达式的字元来表示制表符：awk、perl、HHsed、
sedmod以及GNU sed v3.02.80。

不同版本的SED：不同的版本间的sed会有些不同之处，可以想象它们之间在语法上
会有差异。具体而言，它们中大部分不支持在编辑命令中间使用标签（:name）或分
支命令（b,t），除非是放在那些的末尾。这篇文档中我们尽量选用了可移植性较高
的语法，以使大多数版本的sed的用户都能使用这些脚本。不过GNU版本的sed允许使
用更简洁的语法。想像一下当读者看到一个很长的命令时的心情：

   sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d

好消息是GNU sed能让命令更紧凑：

   sed '/AAA/b;/BBB/b;/CCC/b;d'      # 甚至可以写成
   sed '/AAA\|BBB\|CCC/b;d'

此外，请注意虽然许多版本的sed接受象“/one/ s/RE1/RE2/”这种在's'前带有空
格的命令，但这些版本中有些却不接受这样的命令:“/one/! s/RE1/RE2/”。这时
只需要把中间的空格去掉就行了。

速度优化：当由于某种原因（比如输入文件较大、处理器或硬盘较慢等）需要提高
命令执行速度时，可以考虑在替换命令（“s/.../.../”）前面加上地址表达式来
提高速度。举例来说：

   sed 's/foo/bar/g' filename         # 标准替换命令
   sed '/foo/ s/foo/bar/g' filename   # 速度更快
   sed '/foo/ s//bar/g' filename      # 简写形式

当只需要显示文件的前面的部分或需要删除后面的内容时，可以在脚本中使用“q”
命令（退出命令）。在处理大的文件时，这会节省大量时间。因此：

   sed -n '45,50p' filename           # 显示第45到50行
   sed -n '51q;45,50p' filename       # 一样，但快得多

如果你有其他的单行脚本想与大家分享或者你发现了本文档中错误的地方，请发电
子邮件给本文档的作者（Eric Pement）。邮件中请记得提供你所使用的sed版本、 
该sed所运行的操作系统及对问题的适当描述。本文所指的单行脚本指命令行的长
度在65个字符或65个以下的sed脚本〔译注1〕。本文档的各种脚本是由以下所列作
者所写或提供：

 Al Aab                               # 建立了“seders”邮件列表
 Edgar Allen                          # 许多方面
 Yiorgos Adamopoulos                  # 许多方面
 Dale Dougherty                       # 《sed &amp; awk》作者
 Carlos Duarte                        # 《do it with sed》作者
 Eric Pement                          # 本文档的作者
 Ken Pizzini                          # GNU sed v3.02 的作者
 S.G. Ravenhall                       # 去html标签脚本
 Greg Ubben                           # 有诸多贡献并提供了许多帮助
-------------------------------------------------------------------------

译注1：大部分情况下，sed脚本无论多长都能写成单行的形式（通过`-e'选项和`;'
号）——只要命令解释器支持，所以这里说的单行脚本除了能写成一行还对长度有
所限制。因为这些单行脚本的意义不在于它们是以单行的形式出现。而是让用户能
方便地在命令行中使用这些紧凑的脚本才是其意义所在。</pre>
<p>&nbsp;</p>
<p><img id="feedlyMiniIcon" title="feedly mini" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAYlJREFUeNrMVD1rwlAUPfkwBBxFKQQlDhmjqzj4F9w7FfwThZaW9n8UnDrrn1BEXNTBRQQhEE1ERRR0Sd990A7VaKJJ6YHzEu4N99z3zrsRGo2GAuCF8YFRQzSwGOuM7zJb3hgfES2o0SdGSWRLDfGhRgLpGAXS4q0VCoUCBEHwzd8skM/nUSqVoCjKybz8O1CtVn2LeZ4H27YxnU6xXC6x3+95PJPJoFKpoN1uY7vdnhfww3w+x3A4xGaz4d3quo5sNvuTTyaTXKTb7cJ13csCzWbzKKaqKj/zXC4HSZKO8iRcLpcxGAwwmUzC7UDTNBSLRSQSiVAeyUGNpM4v4XA4oNfr8eMMLJBKpWCaJn9fLBawLAur1Qq73Y6b/H0pyNyrTDYMA7PZDKPRCOv1+uQ3ZCqZe9UcOI6DTqfjW5zMbLVa3Bu6RaE9GI/HZ/P9fp8/qfipYROj+ukEnuQgEx0GImKGHGSC//UO/kTAibG+QwIfMQrUyeRXusaM94x3ERW2GT8Zn78EGACRmoKUJhB1TQAAAABJRU5ErkJggg==" alt="" width="24" height="24" /></p>
]]></content:encoded>
			<wfw:commentRss>https://www.biofacebook.com/?feed=rss2&#038;p=442</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[python] 关于python中的string.atoi()的问题</title>
		<link>https://www.biofacebook.com/?p=438</link>
		<comments>https://www.biofacebook.com/?p=438#comments</comments>
		<pubDate>Wed, 01 Aug 2012 08:32:20 +0000</pubDate>
		<dc:creator><![CDATA[szypanther]]></dc:creator>
				<category><![CDATA[脚本语言]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.biofacebook.com/?p=438</guid>
		<description><![CDATA[ #!/usr/bin/python #filename:string.py</p> <p>import string</p> <p>print string.atoi(&#8217;13&#8217;,8) #这里转换8进制的数 print string.atoi(&#8217;13&#8217;,16) #这里转换成16进制的数</p> <p>运行后的结果： 11 19 </p> <p>&#62;&#62;&#62; import string &#62;&#62;&#62; string.atoi(&#8217;13&#8217;,8) 11 &#62;&#62;&#62; string.atoi(&#8217;13&#8217;,10) 13 &#62;&#62;&#62; string.atoi(&#8217;13&#8217;) 13 &#62;&#62;&#62;</p> <p> ]]></description>
				<content:encoded><![CDATA[<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td id="postmessage_103859"><span class="Apple-style-span" style="font-size: 12px; line-height: 18px;">#!/usr/bin/python<br />
#filename:string.py</p>
<p>import string</p>
<p>print  string.atoi(&#8217;13&#8217;,8)      #这里转换8进制的数<br />
print string.atoi(&#8217;13&#8217;,16)     #这里转换成16进制的数</p>
<p>运行后的结果：<br />
11<br />
19    </span></p>
<p>&gt;&gt;&gt; import string<br />
&gt;&gt;&gt; string.atoi(&#8217;13&#8217;,8)<br />
11<br />
&gt;&gt;&gt; string.atoi(&#8217;13&#8217;,10)<br />
13<br />
&gt;&gt;&gt; string.atoi(&#8217;13&#8217;)<br />
13<br />
&gt;&gt;&gt;</p>
<p><span class="Apple-style-span" style="font-size: 12px; line-height: 18px;">   </span></td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>https://www.biofacebook.com/?feed=rss2&#038;p=438</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>python 正则表达 前向界定与后向界定</title>
		<link>https://www.biofacebook.com/?p=410</link>
		<comments>https://www.biofacebook.com/?p=410#comments</comments>
		<pubDate>Thu, 19 Jul 2012 08:11:12 +0000</pubDate>
		<dc:creator><![CDATA[szypanther]]></dc:creator>
				<category><![CDATA[脚本语言]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.biofacebook.com/?p=410</guid>
		<description><![CDATA[<p>&#62;&#62;&#62; a &#8216;[acyl-carrier-protein] S-malonyltransferase [EC:2.3.1.39]&#8217; &#62;&#62;&#62; b = re.findall(r'(?&#60;=\[EC:).+?(?=\])&#8217;,a) &#62;&#62;&#62; b [&#8216;2.3.1.39&#8242;] &#62;&#62;&#62; b = re.findall(r'(?&#60;=\[).+?(?=\])&#8217;,a) &#62;&#62;&#62; b [&#8216;acyl-carrier-protein&#8217;, &#8216;EC:2.3.1.39&#8242;]</p> <p> 前向界定与后向界定</p> <p>有时候需要匹配一个跟在特定内容后面的或者在特定内容前面的字符串，Python提供一个简便的前向界定和后向界定功能，或者叫前导指定和跟从指定功能。它们是：</p> <p>‘(?&#60;=…)’ 前向界定</p> <p>括号中’…’代表你希望匹配的字符串的前面应该出现的字符串。</p> <p>‘(?=…)’ 后向界定</p> <p>括号中的’…’代表你希望匹配的字符串后面应该出现的字符串。</p> <p>例： 你希望找出c语言的注释中的内容，它们是包含在’/*’和’*/’之间，不过你并不希望匹配的结果把’/*’和’*/’也包括进来，那么你可以这样用：</p> <p>&#62;&#62;&#62; s=r’/* comment 1 */ code /* comment 2 */’</p> <p>&#62;&#62;&#62; re.findall( r’(?&#60;=//*).+?(?=/*/)’ , s )</p> <p>[&#8216; comment 1 &#8216;, &#8216; comment 2 &#8216;]</p> [...]]]></description>
				<content:encoded><![CDATA[<p>&gt;&gt;&gt; a<br />
&#8216;[acyl-carrier-protein] S-malonyltransferase [EC:2.3.1.39]&#8217;<br />
&gt;&gt;&gt; b = re.findall(r'(?&lt;=\[EC:).+?(?=\])&#8217;,a)<br />
&gt;&gt;&gt; b<br />
[&#8216;2.3.1.39&#8242;]<br />
&gt;&gt;&gt; b = re.findall(r'(?&lt;=\[).+?(?=\])&#8217;,a)<br />
&gt;&gt;&gt; b<br />
[&#8216;acyl-carrier-protein&#8217;, &#8216;EC:2.3.1.39&#8242;]</p>
<p><strong>   </strong><strong>前向界定与后向界定</strong></p>
<p>有时候需要匹配一个跟在特定内容后面的或者在特定内容前面的字符串，Python提供一个简便的前向界定和后向界定功能，或者叫前导指定和跟从指定功能。它们是：</p>
<p><strong>‘(?&lt;=…)’ </strong><strong>前向界定</strong></p>
<p>括号中’…’代表你希望匹配的字符串的前面应该出现的字符串。</p>
<p><strong>‘(?=…)’  </strong><strong>后向界定</strong></p>
<p>括号中的’…’代表你希望匹配的字符串后面应该出现的字符串。</p>
<p>例： 你希望找出c语言的注释中的内容，它们是包含在’/*’和’*/’之间，不过你并不希望匹配的结果把’/*’和’*/’也包括进来，那么你可以这样用：</p>
<p>&gt;&gt;&gt; s=r’/* comment 1 */  code  /* comment 2 */’</p>
<p>&gt;&gt;&gt; re.findall( r’(?&lt;=//*).+?(?=/*/)’ , s )</p>
<p>[&#8216; comment 1 &#8216;, &#8216; comment 2 &#8216;]</p>
<p>注意这里我们仍然使用了最小匹配，以避免把整个字符串给匹配进去了。</p>
<p>要注意的是，前向界定括号中的表达式必须是常值，也即你不可以在前向界定的括号里写正则式。比如你如果在下面的字符串中想找到被字母夹在中间的数字，你不可以用前向界定：</p>
<p>例：</p>
<p>&gt;&gt;&gt; s = ‘aaa111aaa , bbb222 , 333ccc ‘</p>
<p>&gt;&gt;&gt; re.findall( r’(?&lt;=[a-z]+)/d+(?=[a-z]+)&#8217; , s )          # 错误的用法</p>
<p>它会给出一个错误信息：</p>
<p>error: look-behind requires fixed-width pattern</p>
<p>&nbsp;</p>
<p>不过如果你只要找出后面接着有字母的数字，你可以在后向界定写正则式：</p>
<p>&gt;&gt;&gt; re.findall( r’/d+(?=[a-z]+)’, s )</p>
<p>[&#8216;111&#8242;, &#8216;333&#8217;]</p>
<p>如果你一定要匹配包夹在字母中间的数字，你可以使用组（group）的方式</p>
<p>&gt;&gt;&gt; re.findall (r'[a-z]+(/d+)[a-z]+&#8217; , s )</p>
<p>[&#8216;111&#8242;]</p>
<p>组的使用将在后面详细讲解。</p>
<p>&nbsp;</p>
<p>除了前向界定前向界定和后向界定外，还有前向非界定和后向非界定，它的写法为：</p>
<p><code><strong>‘(?&lt;!...)’</strong></code><code><strong>前向非界定</strong></code></p>
<p><code>只有当你希望的字符串前面不是’…’的内容时才匹配</code></p>
<p><code><strong>‘(?!...)’</strong></code><code><strong>后向非界定</strong></code></p>
<p>只有当你希望的字符串后面不跟着’…’内容时才匹配。</p>
<p>接上例，希望匹配后面不跟着字母的数字</p>
<p>&gt;&gt;&gt; re.findall( r’/d+(?!/w+)’ , s )</p>
<p>[&#8216;222&#8242;]</p>
<p>注意这里我们使用了/w而不是像上面那样用[a-z]，因为如果这样写的话，结果会是：</p>
<p>&gt;&gt;&gt; re.findall( r’/d+(?![a-z]+)’ , s )</p>
<p>[&#8217;11&#8217;, &#8216;222&#8217;, &#8217;33&#8217;]</p>
<p>这和我们期望的似乎有点不一样。它的原因，是因为’111’和’222’中的前两个数字也是满足这个要求的。因此可看出，正则式的使用还是要相当小心的，因为我开始就是这样写的，看到结果后才明白过来。不过Python试验起来很方便，这也是脚本语言的一大优点，可以一步一步的试验，快速得到结果，而不用经过烦琐的编译、链接过程。也因此学习Python就要多试，跌跌撞撞的走过来，虽然曲折，却也很有乐趣。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.biofacebook.com/?feed=rss2&#038;p=410</wfw:commentRss>
		<slash:comments>54</slash:comments>
		</item>
		<item>
		<title>python dic problem</title>
		<link>https://www.biofacebook.com/?p=397</link>
		<comments>https://www.biofacebook.com/?p=397#comments</comments>
		<pubDate>Thu, 12 Jul 2012 09:01:28 +0000</pubDate>
		<dc:creator><![CDATA[szypanther]]></dc:creator>
				<category><![CDATA[My project]]></category>
		<category><![CDATA[脚本语言]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.biofacebook.com/?p=397</guid>
		<description><![CDATA[<p>dic_cog_annot[h2[1]]={&#8216;orthologous_group':h[3]} dic_cog_annot[h2[1]][&#8216;protein_annot&#8217;]=h[4] 以上是增加个protein_annot键值，</p> <p>如果是以下,将会被替换，只一个键值！</p> <p>dic_cog_annot[h2[1]]={&#8216;orthologous_group':h[3]} dic_cog_annot[h2[1]]={&#8216;protein_annot':h[4]}</p> <p>&#160;</p> <p>dic[name]=&#8221;NA&#8221;</p> <p>dic[name] = {&#8216;nr_annot':&#8221;NA&#8221;} dic[name][&#8216;go_num&#8217;]=&#8221;NA&#8221; dic[name][&#8216;go_info&#8217;] = &#8220;NA&#8221; dic[name][&#8216;kegg_hit&#8217;] = &#8220;NA&#8221; dic[name][&#8216;kegg_annot&#8217;]=&#8221;NA&#8221; dic[name][&#8216;swiss_annot&#8217;]=&#8221;NA&#8221; dic[name][&#8216;cog_hit&#8217;] =&#8221;NA&#8221; dic[name][&#8216;orthologous_group&#8217;]=&#8221;NA&#8221; dic[name][&#8216;protein_annot&#8217;]=&#8221;NA&#8221;</p> <p>&#160;</p> ]]></description>
				<content:encoded><![CDATA[<p>dic_cog_annot[h2[1]]={&#8216;orthologous_group':h[3]}<br />
dic_cog_annot[h2[1]][&#8216;protein_annot&#8217;]=h[4]<br />
以上是增加个protein_annot键值，</p>
<p>如果是以下,将会被替换，只一个键值！</p>
<p>dic_cog_annot[h2[1]]={&#8216;orthologous_group':h[3]}<br />
dic_cog_annot[h2[1]]={&#8216;protein_annot':h[4]}</p>
<p>&nbsp;</p>
<p>dic[name]=&#8221;NA&#8221;</p>
<p>dic[name] = {&#8216;nr_annot':&#8221;NA&#8221;}<br />
dic[name][&#8216;go_num&#8217;]=&#8221;NA&#8221;<br />
dic[name][&#8216;go_info&#8217;] = &#8220;NA&#8221;<br />
dic[name][&#8216;kegg_hit&#8217;] = &#8220;NA&#8221;<br />
dic[name][&#8216;kegg_annot&#8217;]=&#8221;NA&#8221;<br />
dic[name][&#8216;swiss_annot&#8217;]=&#8221;NA&#8221;<br />
dic[name][&#8216;cog_hit&#8217;] =&#8221;NA&#8221;<br />
dic[name][&#8216;orthologous_group&#8217;]=&#8221;NA&#8221;<br />
dic[name][&#8216;protein_annot&#8217;]=&#8221;NA&#8221;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>https://www.biofacebook.com/?feed=rss2&#038;p=397</wfw:commentRss>
		<slash:comments>44</slash:comments>
		</item>
		<item>
		<title>python 键值排序输出</title>
		<link>https://www.biofacebook.com/?p=389</link>
		<comments>https://www.biofacebook.com/?p=389#comments</comments>
		<pubDate>Tue, 10 Jul 2012 03:43:05 +0000</pubDate>
		<dc:creator><![CDATA[szypanther]]></dc:creator>
				<category><![CDATA[脚本语言]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.biofacebook.com/?p=389</guid>
		<description><![CDATA[<p>method1:</p> <p>dic = {}</p> <p>keys= dic.keys()</p> <p>keys.sort()</p> <p>for key in keys:</p> <p>print dic[key]</p> <p>&#160;</p> <p>method2:</p> <p>for key in sorted(write_list_hash.keys()): print write_list_hash[key]</p> ]]></description>
				<content:encoded><![CDATA[<p>method1:</p>
<p>dic = {}</p>
<p>keys= dic.keys()</p>
<p>keys.sort()</p>
<p>for key in keys:</p>
<p>print dic[key]</p>
<p>&nbsp;</p>
<p>method2:</p>
<p>for key in sorted(write_list_hash.keys()):<br />
print write_list_hash[key]</p>
]]></content:encoded>
			<wfw:commentRss>https://www.biofacebook.com/?feed=rss2&#038;p=389</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
