![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to read record by record from a file in unix | raoscb | UNIX for Dummies Questions & Answers | 1 | 05-16-2008 07:30 AM |
| Adding spaces to record | nvenkat010 | Shell Programming and Scripting | 3 | 01-28-2008 01:24 PM |
| appending spaces to first line based on second record. | ammu | Shell Programming and Scripting | 2 | 11-16-2007 03:35 AM |
| splitting a record and adding a record to a file | rsolap | Shell Programming and Scripting | 1 | 08-13-2007 02:58 PM |
| Strip leading and trailing spaces only in a shell variable with embedded spaces | jerardfjay | Shell Programming and Scripting | 6 | 03-07-2005 02:24 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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!!!! |
|
||||
|
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;
}
|
|
||||
|
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"
|
|
||||
|
Code:
$ sed "s/\(.\{3\}\)\(.\{6\}\)/\1 \2 /" file
607 010060 00000030380000000000000030380000400000000000
610 010060 00000008220000000000000008220000100000000000
612 010060 00000030150000000000000030150001000000000000
$
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|