我正在学习 Swift,软件公告github 上的 repos 太长了。
很重要,内容我看过了,所以我觉得没看也没关系。
我的问题是如何删除每个.swift
文件里面的长段落。
// 版权所有 (c) 2014-2017 Alamofire 软件基金会 (http://alamofire.org/)
// 在此免费授予任何获得
...的人许可。// 软件。
我认为处理Objective-C 的.m
/文件的方法是一样的。.h
这个问题类似于问题在多个文件中查找和替换文本。
而平台是Mac OS。
提前谢谢了 , :-)
例如长段落如下:
// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
答案1
使用搜索和替换有点过头了。如果所有文件开头的文本都相同,您可以执行以下操作:
for f in *.swift; do tail -n +20 < "$f" > "$f.tmp"; mv "$f.tmp" "$f"; done
这用于tail
给出从第 20 行开始的每个文件的内容。
但我认为你不应该从这些文件中删除许可证文本。你可能不会在它们附近的任何地方编辑代码,而且它们只是注释,所以不会对你的代码或编译后的二进制文件产生影响。
答案2
find . \( -name "*.m" -o -name "*.h" -o -name "*.swift" \) -print0 | while read -d $'\0' file
do
path=${file:1}
f="$(pwd)$path"
number=$(grep -n -m 1 "^[^\/\ ]" $f |sed 's/\([0-9]*\).*/\1/' )
if (( $number > 2 )) ; then
num=$((number-1))
tail -n +$num < "$f" > "$f.tmp"
mv "$f.tmp" "$f"
fi
done
基于@black_pearl改进
答案3
答案4
感谢@Matt Sephton,我开发了以下内容,计算最后一条评论的位置数,然后删除它
cd $1
find . -name "*.swift" -print0 | while read -d $'\0' file
do
path=${file:1}
f="$(pwd)$path"
number=$(grep -n -m 1 "^[^\/]" $f |sed 's/\([0-9]*\).*/\1/' )
num=$((number-1))
tail -n +$num < "$f" > "$f.tmp"
mv "$f.tmp" "$f"
done