Program to insert Delimiters at fixed locations in a file, Can you please Debug it for me??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Program to insert Delimiters at fixed locations in a file, Can you please Debug it for me??
# 1  
Old 06-16-2010
Program to insert Delimiters at fixed locations in a file, Can you please Debug it for me??

Can someone please help?I have a file - fixed.txt----------------------------AABBBBCCCCCCDDDEEFFFFGGGGGGHHHIIJJJJKKKKKKLLL----------------------------To insert delimiters at fixed lengths of 2, 4, 6, 3, I created a file text1.txt as-------------------2463----------------------and trying to execute below program--------#!/bin/kshfixedwidthfile=$1collengthfile=$2if [ -s delimitedfile.txt ]; then rm delimitedfile.txtficat $fixedwidthfile | while read linedoi=1newline=""cat $collengthfile | while read line1doj=`expr $i + $line1 - 1`col=`echo $line|cut -c ${i}-${j}`newline=`expr ${newline},$col`i=`expr $i + $line1`doneecho $newline|cut -c 2- >> delimitedfile.txtdone---------------------------But it is not giving the desired resultPlease Help!
# 2  
Old 06-16-2010
Hi.

Your post is near impossible to read. Can you please edit it, using code tags, to make it presentable?

Thanks.
# 3  
Old 06-16-2010
Can someone please help?
I have a file - fixed.txt
Code:
AABBBBCCCCCC
DDDEEFFFFGGGGGG
HHHIIJJJJKKKKKKLLL

To insert delimiters at fixed lengths of 2, 4, 6, 3,
I created a file text1.txt as
Code:
2
4
6
3

and trying to execute below program
Code:
#!/bin/ksh
fixedwidthfile=$1
collengthfile=$2
if [ -s delimitedfile.txt ]; then 
  rm delimitedfile.txt
fi

cat $fixedwidthfile | while read line
do
  i=1
  newline=""
  cat $collengthfile | while read line1
  do
    j=`expr $i + $line1 - 1`
    col=`echo $line|cut -c ${i}-${j}`
    newline=`expr ${newline},$col`
    i=`expr $i + $line1`
  done

  echo $newline|cut -c 2- >> delimitedfile.txt
done

But it is not giving the desired resultPlease Help!

Last edited by Scott; 06-16-2010 at 04:39 PM.. Reason: Added code tags, and indentation
# 4  
Old 06-16-2010
You notice the break with your own protocol when 'DDD' wraps around onto the next line? Or was this intended...?
# 5  
Old 06-16-2010
Yes It was intended.
# 6  
Old 06-16-2010
okay.

I'm sure there are quicker approaches, but this would probably suffice in a quick and dirty approach; it also shows exactly what it's doing...and is scalable in terms of additional field (should they arise...):

Code:
-> #  Where Edit12 is your file... 
-> for i in $(tr -d "\n" <Edit12 |fold -b -15) ;do \
      fld1=$(echo $i |cut -b 1-2) \
   && fld2=$(echo $i |cut -b 3-6) \
   && fld3=$(echo $i |cut -b 7-12) \
   && fld4=$(echo $i |cut -b 13-16) ;print $fld1 $fld2 $fld3 $fld4 ;done
AA BBBB CCCCCC DDD
EE FFFF GGGGGG HHH
II JJJJ KKKKKK LLL

You can adjust to provide your own delimiters such as follows:

Code:
-> #  Where Edit12 is your file... 
-> for i in $(tr -d "\n" <Edit12 |fold -b -15) ;do \
      fld1=$(echo $i |cut -b 1-2) \
   && fld2=$(echo $i |cut -b 3-6) \
   && fld3=$(echo $i |cut -b 7-12) \
   && fld4=$(echo $i |cut -b 13-16) ;print "$fld1${FS:- }$fld2${FS:- }$fld3${FS:- }$fld4" ;done
AA BBBB CCCCCC DDD
EE FFFF GGGGGG HHH
II JJJJ KKKKKK LLL


Last edited by curleb; 06-18-2010 at 04:09 PM.. Reason: line continuation brings sanity...
# 7  
Old 06-16-2010
I will have to generalise this logic for large data files from database received as flat files. using this program I am planning to insert delimiter after fixed lengths(lengths of the columns from database) and then can export the flat file into excel as table with ease.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert data between comma delimiters-large file

Having a huge file in the following format. 2,3,1,,,4 1,2,3,,,,,5, 8,7,3,4,,,, Output needed is: 2,3,1,0.0,0.0,4 1,2,3,0.0,0.0,0.0,0.0,5, 8,7,3,4,0.0,0.0,0.0, I have tried reading the file each line, using AWK to parse to find out ",," and then insert 0.0 . It works but very slow. Need... (8 Replies)
Discussion started by: wincrazy
8 Replies

2. Shell Programming and Scripting

Insert a complete file into another file after certain fixed line

Hi Friends, I am writing a shell script where it is required to insert a file say file1 into file2 after certain number of fixed lines in file2.. Please help me how this could be done.. Please suggest me any useful links i need to go through to achieve this.... Thanks in advance .. (2 Replies)
Discussion started by: amr89
2 Replies

3. Shell Programming and Scripting

Insert a variable to a text file after fixed number of lines

Hi, I am new to unix. I need to insert a variable which contains some lines of text into a text file after fixed number of lines.. Please help me on this.. Thanks in Advance, Amrutha (3 Replies)
Discussion started by: amr89
3 Replies

4. Shell Programming and Scripting

running C program to output in multiple locations

Hi Guys, What I am looking at doing is to run a C program in my home directory, but output files in multiple directories BUT not at the same instance. For e.g. 1st instance: Run program.c and output results in path /aaa/bbb/ccc/ 2nd instance: Run program.c again and output results... (9 Replies)
Discussion started by: Jatsui
9 Replies

5. Shell Programming and Scripting

Getting a string without fixed delimiters

I have a line of text for example aaaa bbbb cccc dddd eeee ffffff I would need to get the cccc however bbbb could be there or not. So whether bbbb is in the line or not I need cccc. I was looking at either awk or sed....and trying to start at c and end until the next space. Also... (11 Replies)
Discussion started by: bombcan1
11 Replies

6. UNIX for Advanced & Expert Users

Insert Delimiter at fixed locations in a flat file

Hi Can somebody help me with solution for this PLEASE? I have a flat file and need to insert delimiters at fixed positions in all the lines so that I can easily convert into EXCEL with columns defined as per their width. For Example Here is the file { kkjhdhal sdfewss sdtereetyw... (7 Replies)
Discussion started by: jd_mca
7 Replies

7. Shell Programming and Scripting

Fetch the rows with match string on a fixed lenth text file - NO delimiters

Hi I am trying to fetch the rows with match string "0000001234" Input file looks like below: 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1 09 0 XXX 0000001234 Z 1... (6 Replies)
Discussion started by: nareshk
6 Replies

8. Shell Programming and Scripting

string deletion, variable contents, fixed delimiters

Hi, I need to mass delete the following string(s) from my files weight=100, However the '100' is variable e.g Current: ---------------- moretext=bar, weight=100, moreinfo=blah extrastuff=hi, weight=9999, extrainfo=foo Desired: ------------------ moretext=bar, moreinfo=blah... (2 Replies)
Discussion started by: rebelbuttmunch
2 Replies

9. Shell Programming and Scripting

Insert lines between delimiters

I'm working with a file like: somestuff somemorestuff ... someadditionalstuff STARTTAG ENDTAG someotherstuff somecoolstuff ... somefinalstuffI've got some text (either in a file or piped) to put between STARTTAG and ENDTAG. I was thinking something like grepping for the line number of... (2 Replies)
Discussion started by: BMDan
2 Replies

10. UNIX for Dummies Questions & Answers

How do I insert commas/delimiters in File

Hi, Newbie here. Need to convert a txt file to .csv format. There's no character to replace so not sure if I can use sed :confused: . The comma is to be inserted after every certain number of characters in each line... Help! Thanks. (4 Replies)
Discussion started by: mbelen
4 Replies
Login or Register to Ask a Question