我有一些像html这样的大模板:
<% /*
This is basically all the markup and interface
/* %>
<div id="test" class="test-right"></div>
<div id="test" class="test-right"></div>我需要将其作为一行字符串,例如:
<% /*\n This is basically all the markup and interface\n*/ %>\n<div id=\"test\" class=\"test-right\"></div>\n <div id=\"test\" class=\"test-right\"></div>你怎么能这样做呢?
original_string='test this id="one" test this id="two"'
string_to_replace_Suzi_with=\"
result_string="${original_string/"/$string_to_replace_Suzi_with}"发布于 2017-02-09 16:19:16
如果您正在寻找一种纯bash方式来完成此任务,您可以在命令行中一次一个地运行它们,或者在脚本中运行它们。
# Store the contents of the file into a variable
fileContents="$(<file)"
# Create a temporary string using 'GNU mktemp'
tempfile="$(mktemp)"
# Parameter substitution syntax to replace the new-line character by
# empty string and store it in the file identified by the temporary
# name
printf "%s\n" "${fileContents//$'\n'//}" > "$tempfile"
# Revert the temp file to original file
mv "$tempfile" file但是考虑到bash对于这个微不足道的任务来说比较慢,可以通过将ORS从new-line重新输入到empty字符串来使用Awk,
awk -v ORS="" 1 file
<% /* This is basically all the markup and interface/* %><div id="test" class="test-right"></div> <div id="test" class="test-right"></div>发布于 2017-02-09 16:29:13
如果要用字母\n替换换行符(回车符),可以使用awk
awk -v ORS='\\n' 1 file这将替换litteral \n的输出记录分隔符ORS。1触发awk中的默认操作,打印整个记录。
https://stackoverflow.com/questions/42131332
复制相似问题