2017-02-09 08:14 阅读 25


I have some large templates as html:

<% /*   
  This is basically all the markup and interface
/* %>
<div id="test" class="test-right"></div>
  <div id="test" class="test-right"></div>

Which I need to have as one line string, for example:

<% /*
  This is basically all the markup and interface
*/ %>
<div id=\"test\" class=\"test-right\"></div>
   <div id=\"test\" class=\"test-right\"></div>

How can you do that?

original_string='test this id="one" test this id="two"'
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongzhan3937 dongzhan3937 2017-02-09 08:19

    If you are looking for a pure bash way to do this, you can run them in command-line one at a time or in a script.

    # Store the contents of the file into a variable
    # Create a temporary string using 'GNU 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
    " "${fileContents//$'
    '//}" > "$tempfile"
    # Revert the temp file to original file
    mv "$tempfile" file

    But considering bash is slower for this trivial task, use Awk by restting the ORS from new-line to empty string,

    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>
    点赞 评论 复制链接分享
  • dongse3348 dongse3348 2017-02-09 08:29

    If you want to replace newlines (carriage return) by a litteral , you can use awk:

    awk -v ORS='\
    ' 1 file

    This will replace the output record separator ORS to a litteral . The 1 triggers the default action in awk, print the entire record.

    点赞 评论 复制链接分享