如何使用bash脚本和sed用换行符替换字符串? [英] How do I replace a string with a newline using a bash script and sed?

查看:59
本文介绍了如何使用bash脚本和sed用换行符替换字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下输入内容:

Value1|Value2|Value3|Value4@@ Value5|Value6|Value7|Value8@@ Value9|etc...

在我的bash脚本中,我想用换行符替换 @@ .我曾用sed尝试过各种方法,但没有运气:

In my bash script I would like to replace the @@ with a newline. I have tried various things with sed but I'm not having any luck:

line=$(echo ${x} | sed -e $'s/@@ /\\\n/g')

最终,我需要将整个输入解析为行和值.也许我要去做错了.我打算用换行符替换 @@ ,然后通过设置 IFS ='|'来遍历输入,以拆分值.如果有更好的方法,请告诉我,我仍然是shell脚本的初学者.

Ultimately I need to parse this whole input into rows and values. Maybe I am going about it wrong. I was planning to replace the @@ with newlines and then loop through the input with setting IFS='|' to split up the values. If there is a better way please tell me, I am still a beginner with shell scripting.

推荐答案

这将起作用

sed 's/@@ /\n/g' filename

用新行替换 @@

这篇关于如何使用bash脚本和sed用换行符替换字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆