Home > Archive > AWK > January 2005 > awk script question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
awk script question
|
|
| Blind Squirrel 2005-01-20, 3:55 am |
| I am have very little experiece in awk scripting but I have been asked
to debug a problem. I have narrowed the problem to this part of the
code. Could someone please tell me what the following part of an .awk
script does?
{if (substr($0,43,14) ~ /^Sales Order :$/) {
print "FLID0300~" substr($0,62,8);
sub (substr($0,43,25)," ");
print "\\left 0.0";
print "\\space lines 19";
print "\\font courier, 9.75";
print $0;
next;
}
Thanks in advance,
Rob
| |
| Patrick TJ McPhee 2005-01-20, 3:55 am |
| In article <41ed2dab@news.012.net.il>,
Aharon Robbins <arnold@skeeve.com> wrote:
% best done with something like:
%
% $0 = substr($0, 1, 42) " " substr($0, 68)
%
% I.e., pull out the leading and trailing substrings, then rebuild the
% record using concatenation.
It's probably best not to assign the result to $0, though.
--
Patrick TJ McPhee
North York Canada
ptjm@interlog.com
| |
| Ed Morton 2005-01-20, 3:56 pm |
|
Blind Squirrel wrote:
> I am have very little experiece in awk scripting but I have been asked
> to debug a problem. I have narrowed the problem to this part of the
> code. Could someone please tell me what the following part of an .awk
> script does?
>
if (the 14 characters starting at character 43 of the current input
record are "Sales Order :") then
> {if (substr($0,43,14) ~ /^Sales Order :$/) {
print "FLID0300=" followed by the 8 characters starting at
character 62 of the current input record
> print "FLID0300~" substr($0,62,8);
replace the 25 characters starting at character 43 of the current
input record with blanks
> sub (substr($0,43,25)," ");
print the text within the double quotes
> print "\\left 0.0";
> print "\\space lines 19";
> print "\\font courier, 9.75";
print the current input record
> print $0;
go on to the next record
> next;
endif
> }
>
> Thanks in advance,
You're welcome, but it would've taken on the briefest of glances at the
awk manual page or users guide
(http://www.gnu.org/software/gawk/manual/gawk.html) for you to figure
that out yourself.
Ed.
> Rob
>
| |
| Aharon Robbins 2005-01-20, 3:56 pm |
| In article <354mguF4ip0l9U1@individual.net>,
Jürgen Kahrs <Juergen.KahrsDELETETHIS@vr-web.de> wrote:
>Ed Morton wrote:
>
Not quite, see below.
[color=darkred]
>Are you sure about this ?
>
>There is a blank character (0x20) between the "sub"
>and the "(". This is not a valid invokation of the
>function "sub". If I remember correctly, some AWK
>interpreters don't like this.
That is only a problem for invoking user-defined functions, not built-in
ones.
The sub() call has a different problem, which is that if whatever text
as defined by the substr() call occurs earlier in the string, it is that
occurrence that will be replaced with blanks. Such things are usually
best done with something like:
$0 = substr($0, 1, 42) " " substr($0, 68)
I.e., pull out the leading and trailing substrings, then rebuild the
record using concatenation.
HTH.
--
Aharon (Arnold) Robbins --- Pioneer Consulting Ltd. arnold AT skeeve DOT com
P.O. Box 354 Home Phone: +972 8 979-0381 Fax: +1 206 350 8765
Nof Ayalon Cell Phone: +972 50 729-7545
D.N. Shimshon 99785 ISRAEL
| |
| William James 2005-01-20, 3:56 pm |
|
Blind Squirrel wrote:
> {if (substr($0,43,14) ~ /^Sales Order :$/) {
> print "FLID0300~" substr($0,62,8);
> sub (substr($0,43,25)," ");
> print "\\left 0.0";
> print "\\space lines 19";
> print "\\font courier, 9.75";
> print $0;
> next;
> }
{
if ( "Sales Order :" == substr($0,43,14) )
{
print "FLID0300~" substr($0,62,8)
$0 = replace( $0, 43, 25, copies(" ",25))
print "\\left 0.0"
print "\\space lines 19"
print "\\font courier, 9.75"
print $0
next
}
}
function replace( s, start, len, new )
{ return substr(s,1,start-1) new substr(s,start+len)
}
function copies( c, n )
{ while ( length(c) < n )
c = c c
return substr(c,1,n)
}
|
|
|
|
|