Inserting spaces in a record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting spaces in a record
# 1  
Old 05-31-2007
Inserting spaces in a record

Hi all.

I am using /bin/sh on an HPUX system.

I have a file, with records as such:

60701006000000030380000000000000030380000400000000000
61001006000000008220000000000000008220000100000000000
61201006000000030150000000000000030150001000000000000

I know the character counts which define the fields, ie
field1 = 1-3 field2 = 4-7, etc

How can I stream thru the entire file and put spaces between the fields, effectively delimiting the fields with spaces, ie

607 01006 000000 03038 0000000000000 03038 000 040 0000000000

Thanks in advance!!!!
# 2  
Old 05-31-2007
This should compile with K&R C so the compiler shipped with HPUX should cope.

Basically list a load of zero based offsets on the command line and pipe the file through it.

Code:
#include <stdio.h>
#include <stdlib.h>

int main(argc,argv)
int argc;
char **argv;
{
	while (1)
	{
		long i=1;
		long index=atol(argv[i++]);
		long cpos=0;

		while (1)
		{
			int c=getchar();

			if (c==EOF) return 0;

			if ((c=='\n')||(c=='\r')) 
			{
				printf("%c",c);
				break;
			}
			
			if (cpos==index)
			{
				printf(" ");

				if (i < argc) index=atol(argv[i++]);
			}

			printf("%c",c);

			cpos++;
		}
	}

	return 0;
}

# 3  
Old 05-31-2007
Code:
awk 'BEGIN { 
           arr[1]="1,3"
	   arr[2]="4,5"
	   arr[3]="9,6"
	   arr[4]="15,5"
	   arr[5]="20,13"
	   arr[6]="33,5"
	   arr[7]="38,3"
	   arr[8]="41,3"
	   arr[9]="44,10"
     }
     { 
           for ( i=1;i<=9;i++ ) {
	     n=split(arr[i],coords,",")
	     printf substr($0,coords[1],coords[2])" "
	   }
	   print ""
     }
' "file"

# 4  
Old 06-01-2007
Code:
$ sed "s/\(.\{3\}\)\(.\{6\}\)/\1 \2 /" file
607 010060 00000030380000000000000030380000400000000000
610 010060 00000008220000000000000008220000100000000000
612 010060 00000030150000000000000030150001000000000000
$

# 5  
Old 06-01-2007
Or...
Code:
awk -v s="3,8,14,19,32,37,40,43" '
    BEGIN{
       n = split(s,b,",")
       for(i=1;i<=n;i++){
           t=b[i]
           a[t]=1
       }
    }
    {
       for(i=1;i<=length;i++)
           printf substr($0,i,1) (i in a?OFS:"")
       printf ORS
    }
    ' file1 > file2

...gives...
Code:
607 01006 000000 03038 0000000000000 03038 000 040 000000000
610 01006 000000 00822 0000000000000 00822 000 010 000000000
612 01006 000000 03015 0000000000000 03015 000 100 000000000

# 6  
Old 06-01-2007
Thanks

Thanks, you guys are sick.

I used the sed example, as I'm lazy and that was 1 line.

Thanks again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

2. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

3. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

4. Shell Programming and Scripting

Removing spaces from record

HI i have record as shown below 402665,4X75,754X_FERNIE BC,12F2,008708,FERNIE BC,1,UTC ,UTC ,250 402665,4X75,754X_FERNIE BC,F212,008708,FERNIE BC,1,UTC ,UTC ,250 402665,4Y75,754Y_FERNIE BC,22F2,008708,FERNIE BC,1,UTC ,UTC ,250 here i want to remove multiple spaces into no... (3 Replies)
Discussion started by: raghavendra.cse
3 Replies

5. Shell Programming and Scripting

how to add blank spaces at the end of every record in a file.

hi, Does anyone has any idea in adding few blank spaces at the end of every record in a file. Eg: file.txt Baby Boy Kim 1234 Baby Boy Vik 1334 Desired output:- output.txt Baby Boy Kim 1234 Baby Boy Vik 1334 I want to add 10 blank spaces at the end every record in file.txt (3 Replies)
Discussion started by: techmoris
3 Replies

6. Shell Programming and Scripting

Inserting spaces after a value in UNIX

Hi All, I need to prepare a shell script in UNIX to insert 400 spaces after a variable in a file "file.txt". Eg. if n=T12345 echo $n >> file.txt cat file.txt should give: T12345<400 spaces> Please... (5 Replies)
Discussion started by: learning_skills
5 Replies

7. UNIX for Advanced & Expert Users

Inserting new record after lookup

I have a requirement: I got a perfect example in this forum and looks like it would be quite similar but I have an additional task to do. I would explain. I have a file x.csv client_id,client_nbr ABC,1250 CDE,1520 EFG,1000 PQR,1800 I have a file y.csv client_id,client_nbr ABC,1000... (0 Replies)
Discussion started by: RubinPat
0 Replies

8. Shell Programming and Scripting

Inserting into first record using perl

hi all... i got some requirment where i need to insert some values into first record of flat file. i am using perl for that job. var1=456 var2=789 echo `perl -p -i -e "s/U/\${var1}U${var2}/g;" myFile.txt` but it is writing into all records which has U.... can anyone help me out in this... (7 Replies)
Discussion started by: shreekrishnagd
7 Replies

9. Shell Programming and Scripting

Adding spaces to record

Hi, I want to print spaces in a trailer record which is a single command. namely the unix command which i already have recs=`wc -l $TargetFileDir/myfile.txt|cut -c1-9`;export recs;echo 'PCPC.DXDINPT.FC0.INPUTFLE.PASS'`date +%Y%m%d``printf '%015d\n' $recs` >> $TargetFileDir/myfile1.txt I... (3 Replies)
Discussion started by: nvenkat010
3 Replies

10. Shell Programming and Scripting

appending spaces to first line based on second record.

Hi, I have a situation to append spaces to end of first record (header)and last record (footer) based on second record length. The first record length is always 20.The second record will be different for different files.I have to append spaces for the first line based on second record... (2 Replies)
Discussion started by: ammu
2 Replies
Login or Register to Ask a Question