Using sed to replace uppercase to lowercase, space to underscore then create a xml file with results

sed examples
sed examples replace
sed replace string in file
sed '/^$/d
man sed
sed command "-i" option example
sed regex
awk

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:

The sun is Shining
The moon is dull tonight
I Feel Like Dancing

and I need it to look like this:

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

#!/bin/bash

echo -n "" > json.xml
while IFS='' read -r line || [[ -n "$line" ]]; do
   sed -e 's/\(.*\)/\L\1/; 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

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?

sed, a stream editor, GNU sed does this by creating a temporary file and sending output to this file rather than to the standard output. Executes the command that is found in pattern space and replaces the Turn the replacement to uppercase until a \L or \E is found,. \u If the substitution was made, then write out the result to the named file. Developing the command, Step 3: lowercase part of the line. The problem with the above command is that it lowercases the entire line, including the initial "Tags:" part. To solve this problem we can enclose parts of our string to replace in parenthesis and access the first enclosed part as \1, the second as \2 and so on. To lowercase just the part after "Tags:":

You're barking up the wrong tree. This is a job for awk, not some slow, fragile, convoluted, shell loop calling sed.

$ cat tst.awk
{
    print  "<message>"
    printf "\t<source>%s</source>\n", gensub(/[[:space:]]/,"_","g",tolower($0))
    printf "\t<translation>%s</translation>\n", $0
    print "</message>"
}

$ awk -f tst.awk file
<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>

The above uses GNU awk for gensub(), with other awks you'd use a variable and gsub().

Linux and Unix tr command tutorial with examples, Tutorial on using tr, a UNIX and Linux command for translating or Examples of converting uppercase to lowercase, deleting specific characters, squeezing repeating patterns and basic finding and replacing. How to build translate pipelines spaces to underscores and converts uppercase to lowercase. Sed is a useful tool for editing strings on the command line. Changing characters to uppercase or lowercase can be done easily with this tool by simply adding one or more of the following to your substitution string: * \L – convert all proceeding characters to lowercase * \U – convert all proceeding characters to uppercase

This might work for you (GNU sed):

sed -e 'i<message>' -e 'h;y/ /_/;s/.*/\L\t<source>&<\/source>/p;x;s/.*/\t<translation>&<\/translation>/;a<\/message>' file

Insert a message tag, copy the input line, translate spaces to underscores, output the line surrounded by source tags and converted to lowercase. Retrieve the original line and surround by translation tags and append a message closing tag.

Using sed to make specific text lowercase in place, The sed command allows for substitution of strings in text. the name of the file to search and replace text in $ sed "s/old/new/g" filename. This command will not modify the file, it outputs the result to stdout (prints it to screen). Saving time and space by working with gzip and bzip2 compressed files in  And IMHO it is little bit more efficient since we’re not converting whole input string to lowercase first and then convert to uppercase if needed. This function only converts first letter to lowercase and then every character after hyphen -to uppercase.

Understanding Linux / UNIX tr command, Q. Can you explain the tr command and how to use it under Linux Create a list of the words in /path/to/file, one per line, enter: responds in upper case, the character will be changed to lower case. spaces from a copy of the text in a file called input.txt and save output to replace multiple underscores Using sed to replace uppercase to lowercase, space to underscore then create a xml file with results. 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.

15 Practical Grep Command Examples In Linux / UNIX, First create the following demo_file that will be used in the examples below to This Line Has All Its First Character Of The Word With Upper Case. grep "this" demo_file this line is the 1st lower case line in this file. with white space. word - word consists of a sequence of letters, digits and underscores. I'm new to the forum so please forgive any syntactical errors in my question. I'm trying to replace spaces with underscores in a matched substring only. I figured sed would be the best editor for

Changing case with regular expressions | Vim Tips Wiki, Tip 298 Printable Monobook Previous Next created 2002 · complexity There are times that you might like to go through a file and change the case of Alternatively, use \u to uppercase only the first character of what follows, or \l to lowercase only 1 Searching; 2 Converting tabs to spaces; 3 Highlight all search pattern  I want to replace all underscore from the first column of CSV file. It should do anything with the other column of csv file. I only able to find this: sed 's/_/./g' file.txt. But I want to replace all underscore with dot only in first column of text file. How do I pass first column to sed command? Thanks

Comments
  • I'm finding it difficult to deal with this question properly. You're asking for a fix of a detail in your approach, but the approach is fundamentally flawed. You can do this all with just sed, no need for any shell looping over lines. Look at this simplified example: sed 's/.*/<message>&<\/message>/' filename which gets you half way there.
  • This Q&A is excellent to see why looping over a file is bad practice: unix.stackexchange.com/questions/169716/…
  • Generating XML with sed has a number of inconvenient corner cases. It looks like you are trying to generate a translation resource of some sort; are you sure there isn't an existing tool for this? (Quick googling gets me itstool.org/documentation but it goes in the other direction.)
  • You should mention which sed versions that'd work in (I suspect it's GNU-only but idk) and why since it's not POSIX.
  • @EdMorton: Sorry I was out for lunch, I have added the explanations and I also explicitly explained that it is with GNU sed (if I remember correctly the \L,\U,\E operator are available only in GNU sed)
  • Thanks Allan. The \n for a newline in the replacement text is non-POSIX too (you need an escaped literal newline character for portability there). In some seds (e.g. BSD/OSX) a \n there will just print the letter n.
  • @Allan I saw all the edits you made. Good eye. I'm not sure if you need me to do anything but I appreciate them. Cheers.
  • @needtoknow: no this is fine now ;-) Thanks!