如何使用sed / awk或其他东西从文件中删除特定行?

时间:2011-03-14 08:14:10

标签: sed awk add lines

我有一个文件,我拉动并推送到svn存储库。我需要从存储库中删除一个文件的一部分,并在我推送到存储库时添加相同的部分。这将通过'git svn fetch'和'git svn dcommit'完成相关问题:How to setup gitattributes to filter part of a file?

我需要一个sed或awk脚本来删除并添加它:

GlobalSection(SubversionScc) = preSolution
    Svn-Managed = True
    Manager = AnkhSVN - Subversion Support for Visual Studio
EndGlobalSection

由此:

Global
    GlobalSection(SubversionScc) = preSolution
        Svn-Managed = True
        Manager = AnkhSVN - Subversion Support for Visual Studio
    EndGlobalSection
    GlobalSection(SolutionConfigurationPlatforms) = preSolution
        Debug|Any CPU = Debug|Any CPU
        Debug|Mixed Platforms = Debug|Mixed Platforms
        Debug|x86 = Debug|x86
        Release|Any CPU = Release|Any CPU
        Release|Mixed Platforms = Release|Mixed Platforms
        Release|x86 = Release|x86
    EndGlobalSection
EndGlobal

编辑: 使用awk我可以这样做以获取文件的特定部分

awk -v RS='GlobalSection' '/SubversionScc/ {print RS$0 RS} ' file

除了这部分外,我如何还原它以获得其他所有内容? 如何在

之后添加此部分
Global

或之前

EndGlobal

在原始文件中?

1 个答案:

答案 0 :(得分:1)

使用sed提取特定部分。

$ sed -n -e '/GlobalSection(SubversionScc/,/EndGlobalSection/p' yourfilename > yoursvnsection
$ cat yoursvnsection
    GlobalSection(SubversionScc) = preSolution
        Svn-Managed = True
        Manager = AnkhSVN - Subversion Support for Visual Studio
    EndGlobalSection

并使用sed重新读取该文件。

$ sed '/^Global$/ r yoursvnsection ' < yourfilename
Global
    GlobalSection(SubversionScc) = preSolution
        Svn-Managed = True
        Manager = AnkhSVN - Subversion Support for Visual Studio
    EndGlobalSection
    GlobalSection(SolutionConfigurationPlatforms) = preSolution
        Debug|Any CPU = Debug|Any CPU
        Debug|Mixed Platforms = Debug|Mixed Platforms
        Debug|x86 = Debug|x86
        Release|Any CPU = Release|Any CPU
        Release|Mixed Platforms = Release|Mixed Platforms
        Release|x86 = Release|x86
    EndGlobalSection
EndGlobal