如何搜索前面没有 } 的换行符并将其删除

如何搜索前面没有 } 的换行符并将其删除

我想用来sed搜索前面没有 的换行符}。用于识别这些情况的正则表达式是:[^}]$。我想说的是:如果该行没有结束,}则删除新行。任何行都应}在换行符之前结束。

如何将其放入sed以删除它们?

编辑:

例子:

{'Date': 'Fri, 19 Apr 2019 07:23:14 GMT', 'Server': 'Apache', 'Vary': 'Qualys-Scan', 'Strict-Transport-Security': 'max-age=31536000;includeSubDomains;preload', 'Set-Cookie': 'ASP.NET_SessionId=ivoa5bhet0s2ygkylmimvkie; path=/; secure; HttpOnly;SameSite=strict, SC_ANALYTICS_GLOBAL_COOKIE=12f133ea5080403692b4ce458fd1a540; expires=Thu, 19-Apr-2029 07:23:14 GMT; path=/; secure; HttpOnly;SameSite=strict, SC_ANALYTICS_SESSION_COOKIE=336B597E7A534D6393C57DF11E047484|1|ivoa5bhet0s2ygkylmimvkie; path=/; secure; HttpOnly;SameSite=strict, incap_ses_885_270026=cDp/VlO1AHgshF9F6SZIDGJ3uVwAAAAAg7DwpecyehBCyhXgoYO5GA==; path=/; Domain=.zurich.co.uk, ___utmvmykuNyVY=dlNaoEsuXSO; path=/; Max-Age=900, ___utmvaykuNyVY=nWJx01KvGT; path=/; Max-Age=900, ___utmvbykuNyVY=JZy
    XEtOwalQ: PtR; path=/; Max-Age=900', 'X-Content-Type-Options': 'nosniff', 'X-XSS-Protection': '1; mode=block', 'Cache-Control': 'private', 'Content-Type': 'text/html; charset=utf-8', 'Keep-Alive': 'timeout=5, max=10', 'Connection': 'Keep-Alive', 'X-Iinfo': '8-3925806-3925807 NNNN CT(73 151 0) RT(1555658593583 5) q(0 0 3 0) r(6 6) U5', 'X-CDN': 'Incapsula', 'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked'}

当我应用你的脚本时,它合并了这些行。但是,新行以 开头tab。如果换行符以空格开头,如何删除空格?

答案1

您可以使用以下命令来连接前一行不以 结尾的行}

sed -e :a -e '/}$/!N; s/\n//; ta'

这本质上是一个变体39. 如果一行以反斜杠“\”结尾,则将其追加到下一行Sed 单行解释,第一部分:文件间距、编号以及文本转换和替换,将“以反斜杠结尾”条件替换为“不以右大括号结尾”。

前任。给定

$ cat > input
foo bar
baz}
foo bar baz}

然后

$ sed -e :a -e '/}$/!N; s/\n//; ta' input
foo barbaz}
foo bar baz}

如果您还想删除连接行开头的任何前导水平空白,您可以更改s/\n//s/\n[[:blank:]]*//

使用您的示例进行测试(将前导空格转换为制表符后):

$ cat example
        {'Date': 'Fri, 19 Apr 2019 07:23:14 GMT', 'Server': 'Apache', 'Vary': 'Qualys-Scan', 'Strict-Transport-Security': 'max-age=31536000;includeSubDomains;preload', 'Set-Cookie': 'ASP.NET_SessionId=ivoa5bhet0s2ygkylmimvkie; path=/; secure; HttpOnly;SameSite=strict, SC_ANALYTICS_GLOBAL_COOKIE=12f133ea5080403692b4ce458fd1a540; expires=Thu, 19-Apr-2029 07:23:14 GMT; path=/; secure; HttpOnly;SameSite=strict, SC_ANALYTICS_SESSION_COOKIE=336B597E7A534D6393C57DF11E047484|1|ivoa5bhet0s2ygkylmimvkie; path=/; secure; HttpOnly;SameSite=strict, incap_ses_885_270026=cDp/VlO1AHgshF9F6SZIDGJ3uVwAAAAAg7DwpecyehBCyhXgoYO5GA==; path=/; Domain=.zurich.co.uk, ___utmvmykuNyVY=dlNaoEsuXSO; path=/; Max-Age=900, ___utmvaykuNyVY=nWJx01KvGT; path=/; Max-Age=900, ___utmvbykuNyVY=JZy
        XEtOwalQ: PtR; path=/; Max-Age=900', 'X-Content-Type-Options': 'nosniff', 'X-XSS-Protection': '1; mode=block', 'Cache-Control': 'private', 'Content-Type': 'text/html; charset=utf-8', 'Keep-Alive': 'timeout=5, max=10', 'Connection': 'Keep-Alive', 'X-Iinfo': '8-3925806-3925807 NNNN CT(73 151 0) RT(1555658593583 5) q(0 0 3 0) r(6 6) U5', 'X-CDN': 'Incapsula', 'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked'}

$ sed -e :a -e '/}$/!N; s/\n[[:blank:]]*//; ta' example
        {'Date': 'Fri, 19 Apr 2019 07:23:14 GMT', 'Server': 'Apache', 'Vary': 'Qualys-Scan', 'Strict-Transport-Security': 'max-age=31536000;includeSubDomains;preload', 'Set-Cookie': 'ASP.NET_SessionId=ivoa5bhet0s2ygkylmimvkie; path=/; secure; HttpOnly;SameSite=strict, SC_ANALYTICS_GLOBAL_COOKIE=12f133ea5080403692b4ce458fd1a540; expires=Thu, 19-Apr-2029 07:23:14 GMT; path=/; secure; HttpOnly;SameSite=strict, SC_ANALYTICS_SESSION_COOKIE=336B597E7A534D6393C57DF11E047484|1|ivoa5bhet0s2ygkylmimvkie; path=/; secure; HttpOnly;SameSite=strict, incap_ses_885_270026=cDp/VlO1AHgshF9F6SZIDGJ3uVwAAAAAg7DwpecyehBCyhXgoYO5GA==; path=/; Domain=.zurich.co.uk, ___utmvmykuNyVY=dlNaoEsuXSO; path=/; Max-Age=900, ___utmvaykuNyVY=nWJx01KvGT; path=/; Max-Age=900, ___utmvbykuNyVY=JZyXEtOwalQ: PtR; path=/; Max-Age=900', 'X-Content-Type-Options': 'nosniff', 'X-XSS-Protection': '1; mode=block', 'Cache-Control': 'private', 'Content-Type': 'text/html; charset=utf-8', 'Keep-Alive': 'timeout=5, max=10', 'Connection': 'Keep-Alive', 'X-Iinfo': '8-3925806-3925807 NNNN CT(73 151 0) RT(1555658593583 5) q(0 0 3 0) r(6 6) U5', 'X-CDN': 'Incapsula', 'Content-Encoding': 'gzip', 'Transfer-Encoding': 'chunked'}

相关内容