regex - 使用 sed 或 awk 转换键值对
问题描述
寻找 sed 或 awk 表达式以将键值转换为特定格式。在这种情况下, pass 没有值,所以它应该显示一个空字符串。
输入:
login: me pass: time: Wed 2020 07 message: There is a cat in the house
期望的输出:
login="me" pass="" time="Wed 2020 07" message="There is a cat in the house"
解决方案
使用 GNU sed
:
echo "login: me pass: time: Wed 2020 07 message: There is a cat in the house" |
sed -E 'G; s/ [[:alpha:]][[:alnum:]]*:/\n&/g; s/: ?([^\n]*)\n/="\1"/g'
印刷:
login="me" pass="" time="Wed 2020 07" message="There is a cat in the house"
请注意,这会将任何key: value
对转换为key="value"
,前提是该:
字符仅用于指定 thekey
并且没有其他用途。
推荐阅读
- java - 从排序数组中删除重复项 Leetcode
- lstm - Keras:通过共享 LSTM 层传递数组?
- eclipse - Eclipse 插件 --add-exports Java 16+
- javascript - 为什么我的结果在我删除输入的输入后显示它们的方式
- google-chrome - 如何在 Chrome 浏览器中看到“图标化”图像?
- javascript - c#中如何使用变量的值来访问类属性?
- python - 使用函数时如何删除没有全局变量的小部件
- linux - 在目录中打印具有像素大小的图像的命令
- discord - 代码不会扫描我的数组中的单词,并且只会在引号中
- reactjs - 致命:未找到存储库