The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-31-2007
lyoncc lyoncc is offline
Registered User
  
 

Join Date: May 2007
Posts: 21
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 (permalink)  
Old 05-31-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
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 (permalink)  
Old 05-31-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,538
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 (permalink)  
Old 06-01-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
$ sed "s/\(.\{3\}\)\(.\{6\}\)/\1 \2 /" file
607 010060 00000030380000000000000030380000400000000000
610 010060 00000008220000000000000008220000100000000000
612 010060 00000030150000000000000030150001000000000000
$
  #5 (permalink)  
Old 06-01-2007
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,409
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 (permalink)  
Old 06-01-2007
lyoncc lyoncc is offline
Registered User
  
 

Join Date: May 2007
Posts: 21
Thanks

Thanks, you guys are sick.

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

Thanks again!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:28 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0