Celald
October 29, 2010, 5:10am
1
Hi,
I have a large flat file from host without delimiter. I'm transforming this file to a csv file using statements like
# Row 03: Customer / field position 3059 +20
WOFABNAM=substr( $0, 3059, 20 );
and deleting the trailing whitespaces before and after with that
sub( /^ +/, "", WOFABNAM );
sub( / +$/, "", WOFABNAM );
Is here a better way to do that like pseudo
WOFABNAM=substr( $0, 3059, 20 ) | sed 's/ //g' ;
thanks in advance
Celal
birei
October 29, 2010, 5:25am
2
Hi,
Removing leading and trailing spaces?
Try:
WOFABNAM=substr( $0, 3059, 20 ) | sed 's/^ *// ; s/ *$//' ;
Regards,
Birei
gsub(/ /, "", WOFABNAM)
The above will remove any space in the string (not only leading/trailing).
Is that OK?
Celald
October 29, 2010, 5:40am
4
Thanks, that works.
I wonder (and that was the reason for my post) that the same gsub command I tried before didn't work.
It's sad that following doesn't work:
WOFABNAM=gsub(/ /, "", substr( $0, 3059, 20 ) );
nawk: syntax error at source line 18
context is
OFABNAM=gsub(/ /, "", >>> substr <<< ( $0, 3059, 20 ) );
nawk: illegal statement at source line 18
thanks again,
celal
In your case it doesn't work because gsub third parameter is not a changeable object.
However, this won't work with a static string either, because sub and gsub do not return the modified string, they rather return the number of substitutions made.
The GNU awk's gensub returns the modified string.