Formatting a text file based on newline and delimiter characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Formatting a text file based on newline and delimiter characters
# 1  
Old 05-10-2007
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.

Code:
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 Smilie
Please help me with the script.

Thanks in advance,
Raj.
# 2  
Old 05-10-2007
Code:
sed -e 's/^M/*/g' -e 's/~/\n/g' file

# 3  
Old 05-11-2007
Thanks for your reply.
But I want to dynamically pick the characters from 4th and 106th position from the file.
# 4  
Old 05-11-2007
See if this would help you.
Place these two lines following before the sed command.
Note the double quotes instead of single quotes.
Code:
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

# 5  
Old 05-11-2007
Thanks for the reply but thats not working.
Its not doing any thing on the file.

Any help ?
# 6  
Old 05-11-2007
There I go.
Here is the solution for my problem.
I hope it helps someone who is new to Shell Scripting like me Smilie

Code:
#!/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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to loop file data based on delimiter

My file has data that looks like below: more data.txt I wish to display each string seperated by a delimiter : Expected output: I tried the below but I m not getting every split string on a new line. #!/bin/bash for i in `sed 's/:/\\n/g' data.txt`; do echo -n... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Replace comma delimiter by newline

The input file is as below AR,age,marks,roll,section,evin,25,80,456,A,atch,23,56,789,B,eena,24 ,78H245,C,Ps,ot,ecessary,hat,ame comes first then age and rest AR AZ,kevin,25,80,456,A,Satch,23,56,789,Satch,23,56,789,B,Meena,24,78,H245,C,AZ ................ ................ I am writting... (8 Replies)
Discussion started by: millan
8 Replies

3. Shell Programming and Scripting

Splitting records in a text file based on delimiter

A text file has 2 fields (Data, Filename) delimited by # as below, Data,Filename Row1 -> abc#Test1.xml Row2 -> xyz#Test2.xml Row3 -> ghi#Test3.xml The content in first field has to be written into a file where filename should be considered from second field. So from... (4 Replies)
Discussion started by: jayakkannan
4 Replies

4. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

5. Shell Programming and Scripting

Newline characters in fields of a file

My source file is pipe delimeted file with 53 fields.In 33 rd column i am getting mutlple new line characters,dule to that record is breaking into multiple records. Note : here record delimter also \n sample Source file with 6 fields : 1234|abc| \nabcd \n bvd \n cde \n |678|890|900\n ... (6 Replies)
Discussion started by: lakshmi001
6 Replies

6. Shell Programming and Scripting

How can I add a newline In a text file using Shell Script

Good day ... Well i do have this project in school, in our Principles Of Operating System Class We are using Cygwin.... And our project goes like this... Create a dictionary using cygwin. Display the following menu at the start of execution 1-add a word in the dictionary # specify the... (1 Reply)
Discussion started by: kpopfreakghecky
1 Replies

7. UNIX for Dummies Questions & Answers

How can I add a newline In a text file using Shell Script

Good day ... Well i do have this project in school, in our Principles Of Operating System Class We are using Cygwin.... And our project goes like this... Create a dictionary using cygwin. Display the following menu at the start of execution 1-add a word in the dictionary # specify... (1 Reply)
Discussion started by: kpopfreakghecky
1 Replies

8. Shell Programming and Scripting

How to avoid Newline character in generated text file?

Hi All, Just need small help in resolving the special new line character in generated output file. In one of my shell script I am using following lines to get the spool file (i.e. sfile.txt) and AAA_XXXX_p111_n222.txt AAA_YYYY_p111_n222.txt Here assuming v_pnum="p111" v_nid="n222" ... (1 Reply)
Discussion started by: shekharjchandra
1 Replies

9. UNIX for Dummies Questions & Answers

formatting data to remove newline

Hi All, I have raw data in the format :- -------------------------------------------------------------------- NUT070 3 ./opc.sh SQLSCRIPT &SID sysdate.sql 20120105 NUW004 3 ./opc.sh SQLSCRIPT &SID sab_supp.sql UNUW032 3 ./opc.sh SQLSCRIPT &SID sab_unsupp.sql... (3 Replies)
Discussion started by: subhotech
3 Replies

10. Shell Programming and Scripting

Substring based on delimiter, finding last delimiter

Hi, I have a string like ABC.123.XYZ-A1-B2-P1-C4. I want to delimit the string based on "-" and then get result as only two strings. One with string till last hyphen and other with value after last hyphen... For this case, it would be something like first string as "ABC.123.XYZ-A1-B2-P1" and... (6 Replies)
Discussion started by: gupt_ash
6 Replies
Login or Register to Ask a Question