Hi,
Please help me...
Problem1:
I am held up in checking an array. I want to check up some invalid
codes while printing the output. We have some 200 hundred codes. In
this only 100 codes are valid left all invalid.
So i have created an array of invalid 100 codes like following.
awk '{
blah
blah
codes=substr($0,1,3)
invalid = "111,222,333,444,555,666,777,.........and so on"
split(invalid,notvalid,",")
if(!(codes in notvalid){
print $0
}
}' input > output
but it is printing all the lines including invalid codes.
I have solved the problem with the following line
if(codes!=111 && codes!=222...........so one){print $0}
but i feel it is lengthy...
Is there any other way?
Problem 2:
I wrote a function header() to print it at every beginning of the new
page starting.
function header(){
print head1
print dd()
}
function dd(){for (i=1; i<=80; i++); printf("=")}
If I call the function in the program like the following.
awk '{
line++
print $0
if(line==60){print header(); line=0}
}
it is printing an empty line after every header like.
head 1
================
------------> this
is the empty line
line1
line2
line3
...
...
line60
to get rid of the empty line i am doing at the output as
}' input | awk 'NF>0' > output
|
|
0
|
|
|
|
Reply
|
visitnag (50)
|
2/28/2010 9:23:44 AM |
|
On 2/28/2010 3:23 AM, nag wrote:
> Hi,
> Please help me...
>
> Problem1:
>
> I am held up in checking an array. I want to check up some invalid
> codes while printing the output. We have some 200 hundred codes. In
> this only 100 codes are valid left all invalid.
>
> So i have created an array of invalid 100 codes like following.
>
> awk '{
>
> blah
> blah
> codes=substr($0,1,3)
>
> invalid = "111,222,333,444,555,666,777,.........and so on"
> split(invalid,notvalid,",")
The above will create an array indexed by the numbers 1 through 100 containing
elements 111, 222, etc. You want an array indexed by 111, 222, etc. so you can
use the "in" operator below. That'd be:
invalid = "111,222,333,444,555,666,777,.........and so on"
c=split(invalid,tmpArr,",")
for (i=1;i<=c;i++)
notvalid[tmpArr[i]]++
delete tmpArr
>
> if(!(codes in notvalid){
> print $0
> }
> }' input> output
>
> but it is printing all the lines including invalid codes.
>
> I have solved the problem with the following line
> if(codes!=111&& codes!=222...........so one){print $0}
>
> but i feel it is lengthy...
>
> Is there any other way?
>
>
>
> Problem 2:
>
> I wrote a function header() to print it at every beginning of the new
> page starting.
>
> function header(){
> print head1
> print dd()
> }
>
> function dd(){for (i=1; i<=80; i++); printf("=")}
>
>
> If I call the function in the program like the following.
>
> awk '{
>
> line++
> print $0
> if(line==60){print header(); line=0}
You don't need an extra "line" variable, just use "!(NR%60)" do do something
every 60 lines.
> }
>
> it is printing an empty line after every header like.
You have a function dd() that prints a bunch of "=" signs. You then call it AND
print whatever it returns by doing "print dd()". I guess it must be returning
something that looks like a newline followed by a blank line. Instead of:
function dd(){for (i=1; i<=80; i++); printf("=")}
....
print dd()
do this:
function dd(){for (i=1; i<=80; i++); printf "="; print ""}
....
dd()
or better:
BEGIN{dd=sprintf("%80s","");gsub(/ /,"=",dd)}
....
print dd
Note in the last version above dd is just a string, not a function.
>
>
> head 1
> ================
> ------------> this
> is the empty line
> line1
> line2
> line3
> ..
> ..
> line60
>
>
> to get rid of the empty line i am doing at the output as
>
>
> }' input | awk 'NF>0'> output
Not necessary. The scripts you posted above had a non-awkish style and some
inefficiencies so if you'd like to post what you end up with after fixing the
above problems we could probably suggest some other improvements.
Ed.
|
|
0
|
|
|
|
Reply
|
Ed
|
2/28/2010 10:02:46 AM
|
|
On Feb 28, 3:02=A0pm, Ed Morton <mortons...@gmail.com> wrote:
> On 2/28/2010 3:23 AM, nag wrote:
>
>
>
>
>
> > Hi,
> > Please =A0help me...
>
> > Problem1:
>
> > I am held up in checking an array. I want to check up some invalid
> > codes while printing the output. We have some 200 hundred codes. In
> > this only 100 codes are valid left all invalid.
>
> > So i have created an array of invalid 100 codes like following.
>
> > awk '{
>
> > blah
> > blah
> > codes=3Dsubstr($0,1,3)
>
> > invalid =3D "111,222,333,444,555,666,777,.........and so on"
> > split(invalid,notvalid,",")
>
> The above will create an array indexed by the numbers 1 through 100 conta=
ining
> elements 111, 222, etc. You want an array indexed by 111, 222, etc. so yo=
u can
> use the "in" operator below. That'd be:
>
> invalid =3D "111,222,333,444,555,666,777,.........and so on"
> c=3Dsplit(invalid,tmpArr,",")
> for (i=3D1;i<=3Dc;i++)
> =A0 =A0 notvalid[tmpArr[i]]++
> delete tmpArr
>
>
>
>
>
>
>
> > if(!(codes in notvalid){
> > print $0
> > }
> > }' input> =A0output
>
> > but it is printing all the lines including invalid codes.
>
> > I have solved the problem with the following line
> > if(codes!=3D111&& =A0codes!=3D222...........so one){print $0}
>
> > but i feel it is lengthy...
>
> > Is there any other way?
>
> > Problem 2:
>
> > I wrote a function header() to print it at every beginning of the new
> > page starting.
>
> > function header(){
> > print head1
> > print dd()
> > }
>
> > function dd(){for (i=3D1; i<=3D80; i++); printf("=3D")}
>
> > If I call the function in the program like the following.
>
> > awk '{
>
> > line++
> > print $0
> > if(line=3D=3D60){print header(); line=3D0}
>
> You don't need an extra "line" variable, just use "!(NR%60)" do do someth=
ing
> every 60 lines.
>
> > }
>
> > it is printing an empty line after every header like.
>
> You have a function dd() that prints a bunch of "=3D" signs. You then cal=
l it AND
> print whatever it returns by doing "print dd()". I guess it must be retur=
ning
> something that looks like a newline followed by a blank line. Instead of:
>
> function dd(){for (i=3D1; i<=3D80; i++); printf("=3D")}
> ...
> print dd()
>
> do this:
>
> function dd(){for (i=3D1; i<=3D80; i++); printf "=3D"; print ""}
> ...
> dd()
>
> or better:
>
> BEGIN{dd=3Dsprintf("%80s","");gsub(/ /,"=3D",dd)}
> ...
> print dd
>
> Note in the last version above dd is just a string, not a function.
>
>
>
>
>
>
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 head 1
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0------------> =A0this
> > is the empty line
> > line1
> > line2
> > line3
> > ..
> > ..
> > line60
>
> > to get rid of the empty line i am doing at the output as
>
> > }' input | awk 'NF>0'> =A0output
>
> Not necessary. The scripts you posted above had a non-awkish style and so=
me
> inefficiencies so if you'd like to post what you end up with after fixing=
the
> above problems we could probably suggest some other improvements.
>
> =A0 =A0 =A0Ed.
Thank you sir. Indexed by my required elements gave the answer. Now i
could understand array in array help (which i asked long back in this
forum). Thank you lot.
problem 2:
calling the function simply as dd() instead of print dd() is giving
the correct result.
But the sprintf(....) is also giving the same blank line. but it
helped me in other way.
I have prepared a bill printing prog. In this the name of the person
only printed and the tail is left blank...(which may lead to name
tampering) with the help of sprintf(....) and gsub(...) i replaced the
blanks followed by the name with a special character like "*"
earlier bill print was like this...
date: 12/31/2009
Pay to McDonald
now
date: 12/31/2009
Pay to McDonald****************************************
Thank you.
|
|
0
|
|
|
|
Reply
|
nag
|
2/28/2010 11:52:10 AM
|
|
|
2 Replies
116 Views
(page loaded in 0.063 seconds)
Similiar Articles: how to check if operation returns an error - comp.lang.ruby ...Now I want to check if the array of > integers is a date or not (i.e. if someone enters '99' in the 'hour' > slot I want the program to know it's not correct.) Is there a better way to do this? - comp.lang.java.helpI thought that rather than do some sort of fancy checking before adding a picked random number into an array that I would just keep picking 6 numbers until they are all ... what is the highest subscript in the array?!? - comp.lang.idl ...FFT Algoritm written in Basic or VB6 - comp.dsp what is the highest subscript in the array?!? - comp.lang.idl ... How to check the FFT results of a sine wave? - comp ... chaning a part of cell arrays - comp.soft-sys.matlabConverting a cell array into a dataset - comp ... checking/setting part of an array. - comp.soft-sys ... Can't change part of an array - ExcelBanter Can't change ... Add values to an array - comp.soft-sys.matlab... do this for N number of times, so how can i store ALL the values for x and y in an array, not just the LAST value of x and y? This is important because i want to check ... Convolution with non-constant Kernel? - comp.lang.idl-pvwave ...Hi, I wonder if there is an easy way to perform convolution on an array with non ... Just checking it here beforehand to avoid re-inventing wheels. Thanks! uitable - 'logical' - checkboxes - comp.soft-sys.matlabI used the table editor to insert checkboxes on my uitable. I have a data cell array behind the table. But it does not allow me to check and uncheck t... Find the median value of an array. - comp.lang.fortranHello, Given an array with 10,000 real values, I want to find the median. ... to do things that work all the time so that I don't need to worry about checking when ... [need help]: how to automatically detect which number is mostly ...Dear experts, I have an array of numbers (from 0 ~ unknown) which is needed to be ... > Check out the function mode, perhaps. http://www.mathworks.com/access/helpdesk/help ... find value in partly empty cell array - comp.soft-sys.matlab ...Can mat2cell return a cell array of columns? - comp.soft-sys ... find value in partly empty cell array - comp.soft-sys.matlab ..... so now i want to check, if there is a ... Check If an variable is an array or not in javascript | GuyFromChennaiThis js function will check if a variable / Object is an array or not and returns true or false Bounds checking - Wikipedia, the free encyclopediaIndex checking means that, in all expressions indexing an array, first check the index value against the bounds of the array which were established when the array was ... 7/25/2012 4:21:39 PM
|