Wonder where shall i put it in my code. basically my awk script looks like below:
BEGIN {
some codes here
}
#so it search for some Trailer with T as indicator
/^T/{
var1=substr($0,100,9); <-- this part grep the '00000-123'
print var1;
}
END
Could you please try changing var1=substr($0,100,9); to var1=substr($0,106,4); , off course not tested it as lack of complete details and sample Input_file.
Currently (ignore the position). Let say i have a few records with 15 character long (fixed length).
Input
T00001000000234
T0000200000-123 <-- this should become -123
T00003000000234
T00004000000234
T00005000-11234 <-- this should become -11234
T00006000000234
T00007000000234
the var1 should be something like: var1=substr($0,7,9)
and record 2 and 5 are having '-' sign but in different position. Hope this is enough sample.
Thanks Ravinder. I've tested your solutions and it works if i use a single line awk command. But my scripts are in an awk script file. so my scripts is calling my myscript.awk file.
if i add that part, it gives me error: no space. my example is a snapshot of what my awk file have. and those numbers without the '-' sign needs to be converted too but that has no issue. only the negative part is.
It isn't really clear to me what you're trying to do in cases where there is no <hyphen> character in your output from awk . Is a return value like 000000234 to remain unchanged or do you want to remove leading zeros?
If you always want to remove leading zeros (and never have a string that is all zeros and don't want to change your awk script, you don't need to use bc to perform your arithmetic; it can all be done with variable expansions:
Unfortunately, the code I suggested in post #15 only works if all zeros in the string are leading zeros. :o Fortunately, RudiC posted the fix that does what I was trying to do in post #18.