Apologies ahead of time if this is stated wrongly as I have searched but could not find how to sed
+ echo
while using while loop
to read a file.
I have a before.txt file that looks like this:
JavaScript
x
The sun is Shining
The moon is dull tonight
I Feel Like Dancing
and I need it to look like this:
JavaScript
<message>
<source>the_sun_is_shinig</source>
<translation>The sun is Shining</translation>
</message>
<message>
<source>the_moon_is_dull_tonight</source>
<translation>The moon is dull tonight</translation>
</message>
<message>
<source>i_feel_like_dancing</source>
<translation>I Feel Like Dancing</translation>
</message>
This is what I have so far …
JavaScript
#!/bin/bash
echo -n "" > json.xml
while IFS='' read -r line || [[ -n "$line" ]]; do
sed -e 's/(.*)/L1/; s/ /_/g' < "$line" > temp.txt
sourceline="$(cat temp.txt)"
echo "<message>" >> json.xml
echo -e "t<source>${sourceline}<source>" >> json.xml
echo -e "t<translation>${line}<translation>" >> json.xml
echo "<message>" >> json.xml
done < "$1"
rm -f temp.txt
cat json.xml
exit
But it fails
JavaScript
cp@0000 ~/work $ bash .language_file_fixer.sh before.txt
.language_file_fixer.sh: line 5: The sun is Shining: No such file or directory
cat: temp.txt: No such file or directory
.language_file_fixer.sh: line 5: The moon is dull tonight: No such file or directory
cat: temp.txt: No such file or directory
.language_file_fixer.sh: line 5: I Feel Like Dancing: No such file or directory
cat: temp.txt: No such file or directory
<message>
<source><source>
<translation>The sun is Shining<translation>
<message>
<message>
<source><source>
<translation>The moon is dull tonight<translation>
<message>
<message>
<source><source>
<translation>I Feel Like Dancing<translation>
<message>
How do I pipe the $line into sed?
Advertisement
Answer
You can do it completely with GNU sed
(sed does read input file, line by line and read your instructions to process each line):
Input:
JavaScript
$ more messages
The sun is Shining
The moon is dull tonight
I Feel Like Dancing
Output:
JavaScript
$ sed -n 'h;s/(.*)/L1E/;s/ /_/g;s/(.*)/<message>nt<source>1</source>/p;x;s/(.*)/t<translation>1</translation>n</message>/p' messages
<message>
<source>the_sun_is_shining</source>
<translation>The sun is Shining</translation>
</message>
<message>
<source>the_moon_is_dull_tonight</source>
<translation>The moon is dull tonight</translation>
</message>
<message>
<source>i_feel_like_dancing</source>
<translation>I Feel Like Dancing</translation>
</message>
Explanations:
For reading purpose let me split the command on several lines:
JavaScript
sed -n 'h;
s/(.*)/L1E/;
s/ /_/g;
s/(.*)/<message>nt<source>1</source>/p;
x;
s/(.*)/t<translation>1</translation>n</message>/p' messages
- Test is done with
GNU sed 4.2.2
-n
option to deactivate the autoprint modeh
to save the line in the hold buffer (all the operations hereunder will be done on the pattern buffer)s/(.*)/L1E/g
is used to transform the whole line in lowercases/ /_/g
is used to transform the spaces in underscoress/(.*)/<message>nt<source>1</source>/p
is used to add the starting tag<message>
followed by a line feed, a tab and<source>
</source>
surrounding the transformed messagep
is used to print itx
is used to exchange the two buffers, after doing this operation the pattern buffer contains the line as-iss/(.*)/t<translation>1</translation>n</message>/p
is used to add the<translation>
tags and ending</message>
tag before printing the result viap