Home > Archive > AWK > June 2007 > stripping left , right blanks
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 |
stripping left , right blanks
|
|
| ehabaziz2001@gmail.com 2007-05-10, 7:57 am |
| How can I strip left , right blanks from a string in awk ?
| |
| Mohsen 2007-05-10, 7:57 am |
| On 10 Mai, 07:59, ehabaziz2...@gmail.com wrote:
> How can I strip left , right blanks from a string in awk ?
Hi,
Normally I use the sub() command with a regular expression. Something
like below:
sub (/[ \t]+$/, "", STRING)
sub (/[ \t]+$/, "", $0)
If the STRING or $0 has some spaces or tabs at the end, it removes all
of them.
Be careful that after this command the STRING or $0 will be changed.
Regards Mohsen
| |
|
| On 10 Mai, 07:59, ehabaziz2...@gmail.com wrote:
> How can I strip left , right blanks from a string in awk ?
{ gsub(/^ *| *$/, "", var) }
{ gsub(/^[ \t]*|[ \t]*$/, "", var) }
The first for blanks the latter for blanks and tabs.
Janis
| |
| Anton Treuenfels 2007-05-11, 7:57 am |
|
"Janis" <janis_papanagnou@hotmail.com> wrote in message
news:1178785490.094101.295890@l77g2000hsb.googlegroups.com...
> On 10 Mai, 07:59, ehabaziz2...@gmail.com wrote:
>
> { gsub(/^ *| *$/, "", var) }
>
> { gsub(/^[ \t]*|[ \t]*$/, "", var) }
>
> The first for blanks the latter for blanks and tabs.
So these will always substitute a null at the beginning and end of 'var'
regardless of whether or not there are blanks and/or tabs? Why not:
{ gsub(/^[ \t]+|[ \t]+$/, "", var) } ?
But this is from someone who doesn't use gsub() much. My own solution is
more convoluted:
if ( match(var, /[^ \t]+([ \t]+[^ \t]+)*/) )
var = substr( var, RSTART, RLENGTH )
IIRC the regex. The idea being to match the first non-blank non-tab to the
last non-blank non-tab, plus catch the case of all blanks and tabs (a
no-no).
- Anton Treuenfels
But I may try your method and see what happens.
| |
|
| On 11 Mai, 05:35, "Anton Treuenfels" <atreuenf...@earthlink.net>
wrote:
> "Janis" <janis_papanag...@hotmail.com> wrote in message
>
> news:1178785490.094101.295890@l77g2000hsb.googlegroups.com...
>
>
>
>
>
> So these will always substitute a null at the beginning and end of 'var'
> regardless of whether or not there are blanks and/or tabs? Why not:
>
> { gsub(/^[ \t]+|[ \t]+$/, "", var) } ?
Sure that's also possible; maybe even preferable in the given context
if there are mostly non-matching lines and you want to save a function
call for the non-matching lines. There are many ways to do something.
One application where the difference matters is, e.g., in this context
awk 'gsub(/^[ \t]*|[ \t]*$/, "", $0)'
awk 'gsub(/^[ \t]+|[ \t]+$/, "", $0)'
Depending on the requirements the former _or_ the latter may be the
right one. The former will substitute matching lines and will print
all lines, so you'll get the complete text with substitutions made.
The latter will just print the lines that have matched and have been
changed, it will remove non-matching lines from output.
Janis
>
> But this is from someone who doesn't use gsub() much. My own solution is
> more convoluted:
>
> if ( match(var, /[^ \t]+([ \t]+[^ \t]+)*/) )
> var = substr( var, RSTART, RLENGTH )
>
> IIRC the regex. The idea being to match the first non-blank non-tab to the
> last non-blank non-tab, plus catch the case of all blanks and tabs (a
> no-no).
>
> - Anton Treuenfels
>
> But I may try your method and see what happens.
| |
| ehabaziz2001@gmail.com 2007-05-11, 7:57 am |
| On May 11, 6:35 am, "Anton Treuenfels" <atreuenf...@earthlink.net>
wrote:
> "Janis" <janis_papanag...@hotmail.com> wrote in message
>
> news:1178785490.094101.295890@l77g2000hsb.googlegroups.com...
>
>
>
>
>
> So these will always substitute a null at the beginning and end of 'var'
> regardless of whether or not there are blanks and/or tabs? Why not:
>
> { gsub(/^[ \t]+|[ \t]+$/, "", var) } ?
>
> But this is from someone who doesn't use gsub() much. My own solution is
> more convoluted:
>
> if ( match(var, /[^ \t]+([ \t]+[^ \t]+)*/) )
> var = substr( var, RSTART, RLENGTH )
>
> IIRC the regex. The idea being to match the first non-blank non-tab to the
> last non-blank non-tab, plus catch the case of all blanks and tabs (a
> no-no).
>
> - Anton Treuenfels
>
> But I may try your method and see what happens.
Can you please aply your solution to my case please , becuase when I
printed adr1 I found numbers not non-blank strings.
Thanks .
adr1=substr($0,66,32)
adr1=gsub(/^ *| *$/, "",adr1)
| |
| Ed Morton 2007-05-11, 7:57 am |
| ehabaziz2001@gmail.com wrote:
<snip>
> Can you please aply your solution to my case please , becuase when I
> printed adr1 I found numbers not non-blank strings.
>
> Thanks .
>
> adr1=substr($0,66,32)
>
> adr1=gsub(/^ *| *$/, "",adr1)
>
gsub modifies the string in-situ and returns a count of the changes it
made. Try this:
adr1=substr($0,66,32)
count=gsub(/^ *| *$/, "",adr1)
print count,adr1
Regards,
Ed.
| |
| Ed Morton 2007-05-11, 7:57 am |
| Anton Treuenfels wrote:
> "Janis" <janis_papanagnou@hotmail.com> wrote in message
> news:1178785490.094101.295890@l77g2000hsb.googlegroups.com...
>
>
>
> So these will always substitute a null at the beginning and end of 'var'
> regardless of whether or not there are blanks and/or tabs? Why not:
>
> { gsub(/^[ \t]+|[ \t]+$/, "", var) } ?
>
> But this is from someone who doesn't use gsub() much. My own solution is
> more convoluted:
>
> if ( match(var, /[^ \t]+([ \t]+[^ \t]+)*/) )
> var = substr( var, RSTART, RLENGTH )
>
> IIRC the regex. The idea being to match the first non-blank non-tab to the
> last non-blank non-tab, plus catch the case of all blanks and tabs (a
> no-no).
I probably wouldn't use your match()+substr() approach in this
particular case given how much more complicated it is than the gsub()
approach, but I do like the idea of describing what you DO want rather
than what you DON'T want in the RE. Why do you say that having all
blanks and tabs is "a no-no", though?
Ed.
| |
| ehabaziz2001@gmail.com 2007-05-12, 7:56 am |
| On May 11, 3:42 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> Anton Treuenfels wrote:
>
>
>
>
>
>
>
>
>
>
>
> I probably wouldn't use your match()+substr() approach in this
> particular case given how much more complicated it is than the gsub()
> approach, but I do like the idea of describing what you DO want rather
> than what you DON'T want in the RE. Why do you say that having all
> blanks and tabs is "a no-no", though?
>
> Ed.- Hide quoted text -
>
> - Show quoted text -
Thanks to all . It works like this :
adr1=substr($0,66,32)
gsub(/^ *| *$/, "",adr1)
| |
| Anton Treuenfels 2007-05-12, 9:57 pm |
|
"Ed Morton" <morton@lsupcaemnt.com> wrote in message
news:uYudnV2g8KwA-dnbnZ2dnUVZ_revnZ2d@comcast.com...
> Anton Treuenfels wrote:
[color=darkred]
> I probably wouldn't use your match()+substr() approach in this
> particular case given how much more complicated it is than the gsub()
> approach, but I do like the idea of describing what you DO want rather
> than what you DON'T want in the RE. Why do you say that having all
> blanks and tabs is "a no-no", though?
>
> Ed.
Oh sorry, that wasn't meant to be a general statement. In the particular
application an all-blank field is defined as an error.
- Anton Treuenfels
| |
| Sugabbe 2007-05-26, 11:48 am |
| HaCkeD aDuLT SiTe :)
Direct access to member zone
http://uniqueadult.com/members/video.php?file=1
username: 218571
password: wanttocome
change the number in the link to get other videos! There are gigs of them! | |
|
|
|
|
|
|
|
|
|
|
|
|
|