2017-02-09 5 views
0

私は、HTMLのようないくつかの大規模なテンプレートを持っている:htmlを1行の文字列に変換するには?

私は例えば、1行の文字列として持っている必要があります
<% /* 
    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}" 
+0

そのHTMLがさらに改行がhtmlコードは、彼は、 '\ n」がない
としてそれを望んでいることを必要とする' '
ない' \ N ' – RomanPerekhrest

+0

ないフレームワークに置き換える必要があり、レンダリングするために意図されている場合 – YumYumYum

答えて

1

あなたがこれを行うには、純粋なbash方法を探している場合は、一度にまたはスクリプトでコマンドライン1でそれらを実行することができます。

# 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は、この簡単な作業のために遅い考えると、あなたはlitteral \nで改行(キャリッジリターン)を交換したい場合はempty文字列、

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> 
2

new-lineからORSをresttingによってAwkを使用、awk

awk -v ORS='\\n' 1 file 

出力レコードセパレータORSをlitteral \nに置き換えます。 1はデフォルトアクションをawkにトリガーし、レコード全体を印刷します。

関連する問題