First year learning with Linux a gameshell made by the teacher on a terminal. We are asked to remove spaces in between words horizontally ( besides \n ), change the letters from upper case to lower case and then transfer the result into a new file.

I'm pretty sure this line of command is the good one. Once I display it using cat newfile.

This is my line of command :

sed -i 's/.*/\U&/' old file | tee newfile

However, it doesn't accept it. Why doesn't it work? Can someone explain to me what I'm doing wrong?

| improve this question | | | | |

There are at least 3 things that I can see:

  1. The \U modifier converts to upper case, whereas you have been asked to convert from upper to lower case

  2. Your expression doesn't appear to remove spaces at all - this is probably easiest to do as a separate expression e.g. s/ //g or (to replace horizontal whitespace more generally) s/[[:blank:]]//g

  3. Using -i (or --in-place) doesn't make sense when you want to redirect or pipe the command's output to another file or process.

So putting all these together, you could use

sed -e 's/[[:blank:]]//g' -e 's/.*/\L&/' oldfile > newfile

If you want to modify oldfile in place first, and then transfer the content to newfile using redirection, you could do that at least in GNU sed using:

sed -i.bak -e 's/[[:blank:]]//g' -e 's/.*/\L&/' -e 'w /dev/stdout' oldfile | tee newfile

Note that you need to write to /dev/stdout explicitly since the modified file is not written to standard output by default when the -i option is used.

Alternatively (and more simply), move or copy the modified file:

sed -i.bak -e 's/[[:blank:]]//g' -e 's/.*/\L&/' oldfile && cp oldfile newfile
| improve this answer | | | | |
  • The reason why I was using -i is to modify the content in the oldfile and then transfer the content to newfile. – ForzaTekk Oct 16 '19 at 2:46
  • So what does sed -e do exactly ? – ForzaTekk Oct 16 '19 at 2:48
  • -e tells sed that the next argument is a script. You can drop it if there is only one script argument: sed 's/ //g;s/.*/\L&/' oldfile – Philippos Oct 16 '19 at 3:57
  • @forzatek I have added some options for in-place processing – steeldriver Oct 16 '19 at 13:19
  • @Philippos thanks for your help ! – ForzaTekk Oct 16 '19 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.