For Programmers: Free Programming Magazines  


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

Janis

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 ?


{ 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.


Janis

2007-05-11, 7:57 am

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!
Maryless

2007-06-07, 12:04 pm

card consolidation credit debt debt debt free loan management business card consolidation credit debt angeles card consolidation credit debt los canada card consolidation credit credit card debt consolidation no home equity
credit card consolidation help school loan and credit card consolidation credit card debt debt consolidation low interest credit card consolidation card consolidation credit debt ics nonprofit
credit card aaa debt consolidation card consolidation credit debt ics nonprofit card consolidation credit debt help card consolidation credit debt payment versus
debt consolidation reducing credit card debt blogspotcom card consolidation credit d low interest credit card consolidation unsecured credit card debt consolidation credit card consolidation servicecredit card debt consolidation company card consolidation credit debt ics nonprofit credit card debt debt consolidation card consolidation credit credit debt report resource card consolidation credit debt florida in
credit card debt consolidation oregon credit card debt consolidation california business card consolidation credit credit debt debt finances from homehome insurance work credit card bill consolidation advice card consolidation credit debtcard consolidation credit v debt business card consolidation credit credit debt debt finances from homehome insurance work credit card consolidation service card consolidation credit debt pittsburgh non profit credit card debt consolidation
Index Page card consolidation credit debt debt debt michigan credit card consolidation card consolidation credit debt payment versus card consolidation credit loan billlow interest credit card consolidation card consolidation credit v debt credit card consolidation uk 1com card consolidation credit debt link advice card consolidation credit debt
card consolidation credit debt keyword business card consolidation credit credit debt debt finances from homehome insurance work card consolidation credit debt online non credit card debt consolidation card consolidate consolidation credit debt debt loan
Skylui

2007-06-16, 6:27 am

Catherine Z. Jones and Ashlee Simpson Lesbian XXXXing!
http://www.britneyraped.com/d?clip=1673286

Cameron Diaz and Jessica Simpson Pussy Licking & Fighting!
http://www.britneyraped.com/player?movie=1673286

Nikki Cox and Ashlee Simpson Tongue Tickles Lesbian Lovers Pussy Movies!
http://www.britneyraped.com/WatchTube?movie=1673286

Christina Aguilera and Hilary Swank , Satisfying Her Lesbian Girlfriend!
http://www.britneyraped.com/watch?id=1673286

Carmen Electra and Alyson Hannigan Spoiling Lucky Girl!
http://www.britneyraped.com/PlayMovie.cgi?clip=1673286

funny naruto video funny picture and video free funny video for kid baby funny video free funny online video
http://635-funny-video.info/funny-a...toon-video.html http://635-funny-video.info/funny-fishing-video.html http://635-funny-video.info/funny-video-clip-girl.html http://635-funny-video.info/funny-horse-video.html http://635-funny-video.info/funny-pet-video.html
Adtedder64

2007-06-17, 1:21 pm

Carmen Electra and Christina Applegate Seducing Waitress!
http://www.shockingtheworld.com/Watch?q=1673286

Christina Applegate and Catherine Z. Jones Tongue Tickles Lesbian Lovers Pussy Movies!
http://www.shockingtheworld.com/Watch?movie=1673286

Paris Hilton and Alyssa Milano Have Fun In Sauna!
http://www.shockingtheworld.com/a?vid=1673286

Cameron Diaz and Catherine Z. Jones Lesbian Wrestling!
http://www.shockingtheworld.com/Med...sp?clip=1673286

Heather Locklear and Hilary Duff Poke Bottle In Pussy Movies!
http://www.shockingtheworld.com/watch?q=1673286

free shocking funny video funny web cam video funny online site video funny video clip nude funny police video
http://635-funny-video.info/sex-fun...toon-video.html http://635-funny-video.info/forum-funny-sexy-video.html http://635-funny-video.info/funny-video-hot-girl.html http://635-funny-video.info/funny-h...-hot-video.html http://635-funny-video.info/crazy-funny-video-clip.html
Sheca

2007-06-19, 10:28 pm

Catherine Z. Jones With Busty Boobs Teasing & Posing!
http://www.shockingonline.com/Media....wmv?vid=726648

Cameron Diaz On Bed Undressing & Posing!
http://www.shockingonline.com/player?movie=726648

Lindsay Lohan getting XXXXed!
http://www.shockingonline.com/watch?q=726648

Alyson Hannigan With Big Tits Masturbating!
http://www.shockingonline.com/Watch?id=726648

Shania Twain changing her dirty panties!
http://www.shockingonline.com/Media...php?clip=726648

madonna holiday lyric clip utube video free download video clip gratis juegos quest hot and sexy britney spears
free lesbian porn psp video video porn de paris hilton free music online video watch free daily video clip black porn video trailer
granny mature movie free lesbian sex video movie listing sacramento vedere video porn gratis adult web cam video
free girl girl latin video free movie download web site britney spears sex pic gay xxx free movie gay hentai movie xxx

clip video yourtube
free gauge porn star video
funny commercial video
en gratis musica toadas
adult video planet
xxx video on demand
wholesale adult movie
funny fishing video
free lesbian sex video clip
michael jackson was my lover
Ximena

2007-06-20, 12:36 pm

http://www.shockingtheworld.com/Watch?id=1673286
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com