Problem using egrep: spaces and newline

Hello:
I am working in bash and am a newbie.
I want to eliminate spaces from strings. Since this is a basic operation, I searched online and implemented the suggestions; however, I am facing a problem here.

I have an input file which looks like this:

abc defghi
jklmno pqrs
tuvw xyzabcd

My desired output is:

abc^defghi
jklmno^pqrs
tuw^xyzabcd

I used the following code:

NEW_FILE=$(egrep ' ' $OLD_FILE|sed -e 's/ /\^/g')
echo $NEW_FILE>>$OUT_FILE

My aim is to eliminate the spaces while maintaining the newline character.

However, the output I got was:

abc^defghi jklmno^pqrs tuw^xyzabcd

Can anyone please help me on this?

-Andy

tr -s ' ' '^' < myfile >myNewFile

Thanks, I am working in bash shell scripting - sorry, I should have mentioned it earlier.

So I tried various formats of the suggestion above

OUT_FILE=$(echo ${OLD_FILE} | tr -s ' ' '^')
OUT_FILE=`$(echo ${OLD_FILE} | tr -s ' ' '^')`
OUT_FILE=$(egrep ' ' $OLD_FILE | tr -s ' ' '^')

These are a few examples of what I tried. But it did not work.

Thanks in advance.

-Andy

See if this works.

sed 's/ /\^/g'  $OLD_FILE >$NEW_FILE

Try:

sed 's/ /^/g' file

This works for me.

This is NOT what I've suggested - reread the post!

Hi vgersh99:

Sorry about that. I tried using

tr -s ' ' '^' < OLD_FILE >NEW_FILE

and I got the error message: No such file or directory

I also tried using $OLD_FILE and $NEW_FILE. That gives me an error: ambiguous redirect

Thanks,
Andy

If you want to make it work, try this...

cat OLD_FILE  | tr -s ' ' '^'  >NEW_FILE 

if you have your variable ''$OLD_FILE' and '$NEW_FILE' defined in the script, the above should work as desired.
Check your variable definitions FIRST.

---------- Post updated at 09:20 AM ---------- Previous update was at 09:19 AM ----------

'cat' is useless - there's no need for that.