[英]Bash - one-liner echo with delimiter
因此,到目前为止我的示例代码如下:
#!/bin/bash
mysampletxt="=======\nTest\n-------;----------\nDone Test\n=========="
我希望能够回显它,使其看起来如下所示:
=======
Test
-------
Some code goes here
More code
----------
Done Test
==========
但是问题是,当我尝试使用AWK,sed或IFS时,它们还使用\\n
作为分隔符,我不希望这种情况发生,因为文本变得一团糟。 无论如何我可以包括吗;
作为分隔符并忽略\\n
?
我在看这个 。 但是不幸的是,这些解决方案都不适合我。
任何帮助将不胜感激!
编辑:为进一步澄清:我想做的是将mysampletxt
从中拆分为两个;
字符。并且能够将拆分文本的第一部分一个位置插入,第二个位置插入另一个位置。
echo -e "${mysampletxt%;*}\nSome code goes here\nMore code\n${mysampletxt#*;}"
要么
printf "%b" "${mysampletxt%;*}\nSome code goes here\nMore code\n${mysampletxt#*;}"
另一个选择是用$'...'
定义您的字符串:
$ mysampletxt=$'=======\nTest\n-------;\n----------\nDone Test\n=========='
$ echo "$mysampletxt"
=======
Test
-------;
----------
Done Test
==========
还要进行替换:
$ echo "${mysampletxt/;/$'\nSome code goes here\nMore code'}"
=======
Test
-------
Some code goes here
More code
----------
Done Test
==========
如果您可以使用awk或sed以外的其他功能(可能会过大):
[ap@localhost ~]$ perl -e 'print "===\n\n---\n\n;"'
===
---
;[ap@localhost ~]$python -c "print ' ====\n====\n;'"
====
====
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.