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

发布于 2024-10-21 23:06:37 字数 1291 浏览 1 评论 0原文

我有一个文件,我将其拉取并推送到 svn 存储库。我需要在从存储库中拉取时删除一个文件的一部分,并在推送到存储库时添加相同的部分。这将通过 'git svn fetch' 和 'git svn dcommit' 完成相关问题: 如何设置 gitattributes 来过滤文件的一部分?

我需要一个 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

在原始文件的

I have a file that i pull and push to a svn repository. I need to remove part of one file while pulling from the repository and add the same part when i push to the repository. This will be done by 'git svn fetch' and 'git svn dcommit' The related question: How to setup gitattributes to filter part of a file?

I need an sed or awk script to remove and add this:

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

From this:

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

EDIT:
With awk i can do this to get the specific part of the file

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

How do i revert this to get everything else except this part?
And how do i add this part after

Global

or before

EndGlobal

in the original file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥醉 2024-10-28 23:06:37

使用 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

Use sed to extract a particular section.

$ 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

And use sed to read that file back in.

$ 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文