Replacing File values

Currently I am using the tr command in 3 scenarios for a ksh script.

1) Replacing any spaces in the file with a ~

tr ' ' '~' <$orignalFile> $newFile

2) After certain processing is done at the end of the scirpt i convert the Tilde back to spaces

tr ' ' '~' <$newFile> $newFile2

3) Last I need to remove any carriage return from the file.

tr -d '\r' <$newFile2> $newFile3

Currently the script works however I am left with 2 extra files that I delete within the script. Is there a more effective way of acheiving what I listed above without having to create 3 new files, I guess I'm suprised when issuing the TR command it doesn't allow you to copy to the same file which i have tried mutlpie times without success ie..

tr ' ' '~' <$orignalFile> $originalFile

Thanks

if you have higher version of sed it comes with an option
sed -i (edit files in place) you can use that to replace space , "~" or anything else without making any temp files

You can't use the same file for input and output as redirection . You will always need to use a temp file and then move it later.

tr ' ' '~' <newfile > new.tmp ;; mv new.tmp newfile

For your first question, it depends on what processing you will do it to the file after converting spaces to a ~ and then when/why do you revert back.

-Devaraj Takhellambam

I am working with a csv. which I am building a specified table for another applicaiton.

data1,This has a space
data2, This has a space
data3, This has a space

I am using AWK to split on the space to seperate the two columns however I noticed when I was looping through csv file the loop was seperating each value instead of keeping data1 and This has a space as two elements. I noticed if I replaced the space with a ~ i would then keep the two value relationship while looping. ie.

data1
This~has~a~space

instead of
data1
This
has
a
space

I tried changing the IFS to IFS=, however that was causing an issue for me for the back end processing of the table of keeping track via a counter of when to add a specified # for the table format between every 2 values and then no # at the end of the file. The other issue i noticed was a carriage return at the end of each 2nd value in the csv value which is where I used the TR -d to remove it, which worked.

I am achieving the end result however I'm always looking for a more efficient way. I then would strip the ~ back out and return spaces.

data1
This has a space
#
data2
This has a space
#
data3
This has a space
<no # at end of file>

I am curious with the SED command is that a per line edit comand or does it operate in the same fashion of the tr command of replacing/removing everything in the file at once?

awk -F, '{sub(/^ /,"",$2);printf("%s\n%s\n",$1,$2)}' input
data1
This has a space
data2
This has a space
data3
This has a space

-Devaraj Takhellambam