0

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?

0

1 Answer 1

2

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 
6
  • The reason why I was using -i is to modify the content in the oldfile and then transfer the content to newfile. Commented Oct 16, 2019 at 2:46
  • So what does sed -e do exactly ? Commented Oct 16, 2019 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 Commented Oct 16, 2019 at 3:57
  • @forzatek I have added some options for in-place processing Commented Oct 16, 2019 at 13:19
  • @steeldriver is it possible to use mv instead of cp ? Commented Oct 16, 2019 at 22:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.