Skip to content
Advertisement

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.

I have a before.txt file that looks like this:

JavaScript

and I need it to look like this:

JavaScript

This is what I have so far …

JavaScript

But it fails

JavaScript

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

Output:

JavaScript

Explanations:

For reading purpose let me split the command on several lines:

JavaScript
  • Test is done with GNU sed 4.2.2
  • -n option to deactivate the autoprint mode
  • h 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 lowercase
  • s/ /_/g is used to transform the spaces in underscores
  • s/(.*)/<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 message
  • p is used to print it
  • x is used to exchange the two buffers, after doing this operation the pattern buffer contains the line as-is
  • s/(.*)/t<translation>1</translation>n</message>/p is used to add the <translation> tags and ending </message> tag before printing the result via p
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement