1.获取第k行(以k=10为例) 要注意的是,如果文件包含内容不足10行,应该不输出. # Read from the file file.txt and output the tenth line to stdout. # 解法一,使用awk awk 'NR == 10' file.txt # 解法二,使用sed(个人测试结果:sed方法比awk快) sed -n '10p' file.txt # 解法三,组合使用tail与head file.txt | head -n1 # 另外,以下这种方
sed -i '$a service snmpd start' /etc/rc.local sed -i "41s:public:mykey:g" /etc/snmp/snmpd.conf sed -i "85s:#::g" /etc/snmp/snmpd.conf 参考资料: http://www.oschina.net/question/179732_114320 昨天在linux下的某個文件插入另外一個文件的內容 發現原來awk也可以同樣處理,我承認我不是很熟
用类方法和静态方法实现:一个是追加写文件一行内容,一个是读指定行号的内容 #coding=utf-8 class handle_file(object): def __init__(self,file_path): self.file_path=file_path @classmethod def write_file(cls,file_path,content): with open(file_path,'a') as