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"'
string_to_replace_Suzi_with=\"
result_string="${original_string/"/$string_to_replace_Suzi_with}"
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
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
" "${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>
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.