【问题标题】:mac sed add new lines before the matched contentmac sed 在匹配的内容之前添加新行
【发布时间】:2018-10-21 13:22:49
【问题描述】:

我们需要在我们的ios项目中添加cmets,OC方法声明如- (void)...,我想写一个脚本来帮我做这个。 在源文件中,我想在//method name: ....的方法声明之前添加cmets,但是我不擅长shell ...

例如,

- (id)initWithWindow:(UIWindow *)window;

- (id)initWithView:(UIView *)view;

- (void)show:(BOOL)animated;

- (void)hide:(BOOL)animated;

- (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay;

我想像这样添加新行:

.
.
.
//method: - (void)hide:(BOOL)animated;
//parma: animated
- (void)hide:(BOOL)animated;

//method: - (id)initWithView:(UIView *)view;
//parma: view
- (id)initWithView:(UIView *)view;
.
.
.

mac上的shell怎么办?

【问题讨论】:

  • 欢迎来到 SO,您的问题不清楚。请编辑它并让我们知道添加这些行的条件是什么以及应该在哪里添加它们?请使用适当的详细信息编辑您的帖子。
  • 对不起,我描述一下情况,清楚吗?如果没有,请告诉我。谢谢。
  • 您希望脚本重新排序您的方法定义并删除其中一些?根据您提供的输入,确保您发布的预期输出是 THE 您想要的预期输出,而不仅仅是一些模糊的近似值。
  • 只需要替换以'-'开头的行吗?
  • 是的,可能以'+'或'-'开头,我使用gsed 's|^[-+] (.*)\([a-zA-Z]*\)|//method: &\n//parma: \n\n&|g' filename,但我无法获取所有参数(只有最后一个),你知道怎么做吗?谢谢。

标签: macos shell awk sed


【解决方案1】:

我想你想要这样的东西:

sed '/-/{h;s|^|//method: |;p;g;s|.*)|//parma: |;s|;$||;p;g;}' filename

这会将结果打印到屏幕上。您可以将其重定向到另一个文件:

sed '/-/{h;s|^|//method: |;p;g;s|.*)|//parma: |;s|;$||;p;g;}' filename > newfile

或者在原地修改旧文件:

sed -i '' '/-/{h;s|^|//method: |;p;g;s|.*)|//parma: |;s|;$||;p;g;}' filename

这是一个中等复杂的 sed 命令。如果你想理解它,我建议你先练习更简单的 sed 命令。

【讨论】:

    【解决方案2】:

    您可以使用以下命令:

    sed 's|^- (.*)\([a-z]*\);|//method: &\n//parma: \1\n&|g' inputfile > outputfile
    

    示例输入:

    - (id)initWithWindow:(UIWindow *)window;
    
    - (void)hide:(BOOL)animated;
    
    - (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay;
    

    示例输出:

    //method: - (id)initWithWindow:(UIWindow *)window;
    //parma: window
    - (id)initWithWindow:(UIWindow *)window;   
    
    //method: - (void)hide:(BOOL)animated;
    //parma: animated
    - (void)hide:(BOOL)animated;
    
    //method: - (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay;
    //parma: delay
    - (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay;
    

    【讨论】:

    • 我在 mac 上使用了gsed,它可以工作。我使用gsed 's|^[-+] (.*)\([a-zA-Z]*\)|//method: &\n//parma: \n\n&|g'。但是你能同时提取animateddelay 吗?也许更多的帕尔马斯。
    猜你喜欢
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多