window文件的CRLF转成linux文件

经常碰到window提交的文件,然后在linux拉下来有这个问题,下面列举几种解决法子

  • sed
    sed 's/\r$//' winfile.txt > unixfile.txt
    
  • awk
      awk '{ sub("\r$", ""); print }' winfile.txt > unixfile.txt
    
  • perl
      perl -lne 's/\r//g; print' winfile.txt > unixfile.txt
    
  • doc2unix
    dos2unix -n winfile unixfile
    
  • fromdocs
      fromdos yourtextfile
    
  • bash
      tr -d '\r' < file1 > file2
    
  • emacs
      ctrl-s ctrl-q ctrl-j
    

格式转化

碰到个有很多服务器信息是这种格式的

host
port
name
address
#...还有五十个

需要转化为如下格式

host1,port1,name1,address1
host2,port2,name2,address2
#...还有五十个

解决方法,定义宏命令,用F4做,也可以保存宏

  • emacs
  # macro definition
  F3
  # start define action
  ctrl+e  + , + -> + backspace
  # end of definition
  F4

替换配置文件某一个字段的内容

  • 匹配某一个配置文件,把配置文件里的BASE_URL: http://www.baidu.com动态的配置为BASE_URL: http:${var}
 sed -i "" 's|BASE_URL:.*,|BASE_URL: '\"http://${BASE_URL}\",'|g' /fe/APIConst.json    
  • Note: 如果你是mac环境,用上面的脚本没问题,如果是linux环境,则要把-i去掉才可以