Formatting a text file based on newline and delimiter characters

Hi Everybody,

I need some help on formatting the files coming into unix box on the fly.
I get a file some thing like this in a single line.

ISA^M00^M          ^M00^M          ^M14^M006929681900   ^M01^M095449419      ^M061130^M2240^MU^M00401^M000003791^M0^MP^M>~GS^MPO^M006929681900^M095449419^M20061130^M2240^M41^MX^M004010~ST^M850^M000000059~BEG^M00^MNE^M11302346F2600^M^M20061130~REF^MIA^M0028910^MTANKO COMPANY~FOB^MPP^MDE^M00300DO~ITD^M^M^M^M^M^M^M^M^M^M^M^M 02%  60  DAYS~DTM^M010^M20061214~TD5^M^M^M^M^MAUTHORIZED CARRIER~

Now all I want is :
1) To identify my delimiter character at 4th position(which is ^M[single character] for this file and it could be varying for other files) 
2) Change the delimiter character whatever it is to *(asterisk)
3) To identify my newline character at 105 position(which is a tilde(~) in this file and it could be varying for other files)
4) Break into new line when ever there is a newline character which cud be ~ or any thing else.
5)Finally changed file should look like below :
ISA*00*          *00*          *14*006929681900   *01*095449419      *061130*2240*U*00401*000003791*0*P*>
GS*PO*006929681900*095449419*20061130*2240*41*X*004010
ST*850*000000059
BEG*00*NE*11302346F2600**20061130
REF*IA*0028910*TANKO COMPANY
FOB*PP*DE*00300DO
ITD************ 02%  60  DAYS
DTM*010*20061214
TD5*****AUTHORIZED CARRIER
6)Also when the script is run on already formatted file like above it should not disturb the file since it is already in perfect shape.

I am new to the shell script and my part of struggle I did by looking threads in this forum but my requirement did not match any :mad:
Please help me with the script.

Thanks in advance,
Raj.

sed -e 's/^M/*/g' -e 's/~/\n/g' file

Thanks for your reply.
But I want to dynamically pick the characters from 4th and 106th position from the file.

See if this would help you.
Place these two lines following before the sed command.
Note the double quotes instead of single quotes.

Char4=`head -1 input_file | cut -c4`
Char106=`head -1 input_file | cut -c106`
sed -e "s/$Char4/*/g" -e "s/$Char106/\n/g" input_file

Thanks for the reply but thats not working.
Its not doing any thing on the file.

Any help ?

There I go.
Here is the solution for my problem.
I hope it helps someone who is new to Shell Scripting like me :stuck_out_tongue:

#!/usr/bin/ksh
set -x
#======================================================#
# char_count = Number of characters in the first line of file                  
# sed is used to trim the leading blank spaces in the char_count variable     
#======================================================#
char_count=$(head -1 $1 | wc -c)
char_count=`echo "${char_count}" | sed 's/^ *//'`
# If char_count <> 106 (which means whole file content is in 1 line then format
if [[ $char_count -ne 106 ]]
then
#======================================================#
# Finds the delimiter character from 4th position in file and replaces the
# delimiter with '*' using 'tr'. Stores into temp1.txt
#======================================================#
field_delimiter=$(cut -c 4 $1)
tr "$field_delimiter" '*' < $1 > temp1.txt
#======================================================#
#Finds the newline character at 106th position
# Replaces all newline characters in temp1.txt to '\n' and stores as temp2.txt
#======================================================#
newline_char=$(cut -c 106 $1)
tr "$newline_char" '\n' < temp1.txt > temp2.txt
#======================================================#
# Deletes all the blank lines from the file                                    
#======================================================#
sed '/^$/d' temp2.txt > $1
#======================================================#
# Deletes temp1.txt and temp2.txt files                                        
#======================================================#
rm -f temp1.txt temp2.txt
#======================================================#
fi