[ruby]对文件内容进行增删改


前提:mac环境(其他环境略有差异)

1、删除某行
# 删除前两行
# sed -i '' '1,2d' 文件名
sed -i '' '1,2d' Podfile
2、每行文本前面添加字符
#sed 's/^/添加的字符串&/g' 文件名
sed 's/^/添加的字符串&/g' Podfile
3、替换字符串
sed -i '' 's/newstr/oldstr/g' Podfile
4、在某行插入
# 在第一行插入某字符串
sed -i '' "1a\\
if ARGV[2] != '--ln-s' \\
" Podfile
5、新建文件并插入多行文本
echo '创建计时脚本:pod_time.rb'
touch pod_time.rb
echo '
def pod_time
    startTime = Time.now.to_i
    target_name = "Test"
    post_install do |installer|
        installer.pods_project.targets.each do |target|
            target_name = target.name
        end 
        endTime = Time.now.to_i
        compileTime = endTime - startTime
        compileType = "3"
    end
end
'> pod_time.rb

猜你喜欢

转载自blog.csdn.net/ancientear/article/details/122655923