Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fixed-length file substitution

Status
Not open for further replies.

hciacastaway

Technical User
Feb 4, 2004
2
US
I am working with a fixed-length file that I need to replace the names that are in between the 81-130 positions for all records starting with a "C" at the beginning of the line. If this was a variable-length file, I could state the field number I wish to do the substitutions for, but how do I do that for fixed-length files? I have been going over both awk and sed and I am stumped. Any suggestions? Thanks!
 

Substitute what? Is the stuff in the middle of the records known or is there way to calculate what it should be from the orginal stuff?

An AWK script like this, decide where you need to set newstuff based upon the previous questions.

BEGIN { newstuff = "this is the known new stuff" ; }

/^C/ { a = substr($0,0,81);
b = substr($0,81,50);
c = substr($0,131,999);
newstuff = "some transformation of b ";
printf("%s%-50.50s%s\n",a,newstuff,c);
}

/^[^C]/{ print $0 }

 
Thanks for the tip! I have never seen an AWK script that parses things out like that before... that's pretty cool...
 
The second action can be removed :

BEGIN { newstuff = "this is the known new stuff" ; }

/^C/ { a = substr($0,0,81);
b = substr($0,81,50);
c = substr($0,131,999);
newstuff = "some transformation of b ";
printf("%s%-50.50s%s\n",a,newstuff,c);
next ;
}

{ print $0 }


Jean Pierre.
 
Actually it be written like this as well - if you're a real 'pervert' [wink]:

BEGIN { newstuff = "this is the known new stuff" ; }

/^C/ { a = substr($0,0,81);
b = substr($0,81,50);
c = substr($0,131,999);
newstuff = "some transformation of b ";
printf("%s%-50.50s%s\n",a,newstuff,c);
next ;
}
1

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Anyway, to answer the original post:
/^C/ { a = substr($0,1,80);
b = substr($0,81,50);
c = substr($0,131);
newstuff = &quot;some transformation of b &quot;;
printf(&quot;%s%-50.50s%s\n&quot;,a,newstuff,c);
next ;
}
1

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top