linux - 如何替换括号之间的换行符
问题描述
我有类似这种格式的日志文件
test {
seq-cont {
0,
67,
266
},
grp-id 505
}
}
test{
test1{
val
}
}
这是产生该输出的 echo 命令
$ echo -e "test {\nseq-cont {\n\t\t\t0,\n\t\t\t67,\n\t\t\t266\n\t\t\t},\n\t\tgrp-id 505\n\t}\n}\ntest{\n\ttest1{\n\t\tval\n\t}\n}\n"
问题是如何删除文件中可能是多个空格之间seq-cont {
的所有空格。}
我希望输出是这样的。最好使用 sed 来产生输出。
test{seq-cont{0,67,266},
grp-id 505
}
}
test{
test1{
val
}
}
OP的努力:这是一个有点工作但不完全是我想要的:
sed ':a;N;/{/s/[[:space:]]\+//;/}/s/}/}/;ta;P;D' logfile
解决方案
可以使用匹配和关闭gnu-awk
的自定义正则表达式来完成:RS
{
}
awk -v RS='{[^}]+}' 'NR==1 {gsub(/[[:space:]]+/, "", RT)} {ORS=RT} 1' file
test {seq-cont{0,67,266},
grp-id 505
}
}
test{
test1{
val
}
}
这里:
NR==1 {gsub(/[[:space:]]+/, "", RT)}
: 对于第一条记录,用空字符串替换所有空格(包括换行符)。{ORS=RT}
:设置ORS
为我们捕获的任何文本RS
PS:NR==1
如果要对整个文件执行此操作,请删除。
推荐阅读
- eclipse - 安装后lombok eclipse停止工作
- kotlin - 如何在 micronaut 中使用环境特定变量?
- javascript - 从 react-redux 中的容器调用操作时出错
- python - 如何在此网络抓取(机器人)代码中查找错误?
- java - MultipartFile 始终为 NULL
- reactjs - KeyboardDateTimePicker 材质 UI 非空验证
- node.js - Node.js Axios HTTP 请求,不记名令牌返回未定义
- r - 如何仅过滤包含R中所有字符串中所有大写字母的向量
- nginx - Nginx 配置文件未重新加载
- postgresql - 为什么 PGadmin 浏览器 Psycopg2 看不到 postgres 视图?