how to get rid of trailing white spaces using awk (140 Views)
Reply
Regular Advisor
Pando
Posts: 201
Registered: ‎04-15-2004
Message 1 of 14 (140 Views)

how to get rid of trailing white spaces using awk

how can i remove of the traing white spaces using awk.

I have used the command below to get the value but i also get the white spaces with it.

mode=$(awk -F "," '/Test Mode/ {print $2}' $MFILE)

Maximum points for al correct replies.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,189
Registered: ‎10-13-1997
Message 2 of 14 (140 Views)

Re: how to get rid of trailing white spaces using awk

does it /have/ to be awk?

# perl -ple's/\s+$//' file

And to strip trailing whitespace in-place (the file itself will actually be changed):

# perl -pli -e's/\s+$//' file

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Regular Advisor
Gordon  Morrison_1
Posts: 83
Registered: ‎07-28-2004
Message 3 of 14 (140 Views)

Re: how to get rid of trailing white spaces using awk

Why be AWKward? ksh can do it.
If it is a single "word" with training spaces, declare the variable that will hold it with:
typeset -L varname
or
shortline=${line%%[ ]*}
(Put a tab and a space inside the []'s

If it's a line with more than one word on it, pass the line variable to a function, then extract each word with $1 $2 $3 etc. within that function (Don't pass the variable inside quotes, or it will take the whole thing to be $1)
What does this button do?
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 4 of 14 (140 Views)

Re: how to get rid of trailing white spaces using awk

Fernando,
with sed:
to delete whitespaces from end of line
sed 's/[ \t]*$//'
to delete whitespaces from start and end of line
sed 's/^[ \t]*//;s/[ \t]*$//'

\t represents the tab key!

Regards
Honored Contributor
Leif Halvarsson_2
Posts: 6,682
Registered: ‎01-09-2002
Message 5 of 14 (140 Views)

Re: how to get rid of trailing white spaces using awk

Hi,
use the gsub function in awk.

gsub(" ","",$MFILE); print $MFILE
Frequent Advisor
Noel Miranda
Posts: 64
Registered: ‎09-01-2000
Message 6 of 14 (140 Views)

Re: how to get rid of trailing white spaces using awk

You can also pipe the output to awk again, this time printing the first field, with separator being the default, i.e white space.
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 7 of 14 (140 Views)

Re: how to get rid of trailing white spaces using awk

untested:

mode=$(awk -F "," '/Test Mode/ { sub(/ *$/,"",$2); print $2}' $MFILE)


The ere specifies any number of spaces at the end of $2 to be replaced with nothing.

Hein.

Trusted Contributor
Amit Agarwal_1
Posts: 153
Registered: ‎06-02-2004
Message 8 of 14 (140 Views)

Re: how to get rid of trailing white spaces using awk

you can try combination of match and substr.

mode=$(awk -F "," '/Test Mode/ {print substr($2, 1, match($2, "[ \t]*$")-1)}' $MFILE)


-Amit
Super Advisor
Ionut Grigorescu_2
Posts: 280
Registered: ‎05-26-2002
Message 9 of 14 (140 Views)

Re: how to get rid of trailing white spaces using awk

Fernando, you should give some points to Noel, so why not pipe it to awk again, like:
awk -F "," '/Test Mode/{print $2}' $MFILE|awk -F " " '{print $1}'
..but still I would use perl...
If it weren't for STRESS I'd have no energy at all
Trusted Contributor
Amit Agarwal_1
Posts: 153
Registered: ‎06-02-2004
Message 10 of 14 (140 Views)

Re: how to get rid of trailing white spaces using awk

Fernando is using "," as FS, so I tend to believe that each field can be in form of multiple words. *IF* it is so, then Noel's solution won't work.
Honored Contributor
Kent Ostby
Posts: 1,836
Registered: ‎09-26-2000
Message 11 of 14 (140 Views)

Re: how to get rid of trailing white spaces using awk

Fernando -- I'm making the assumption that you only want to get rid of white space at the end of the line.

sample data input (add three spaces after last word):

Fernando Fulgar Fernando

To test:
wc indata
1 3 30
{NOTE: one character is a carriage return)

Create a file, tw.awk, with this script in it:

{dalen=length($0);daflag=0;
for (idx1=dalen;daflag==0;idx1--)
{ if (substr($0,idx1,1)!=" ")
{print substr($0,1,idx1);exit}
}
}

awk -f tw.awk < indata > outdata

wc -l outdata
1 3 27 outdata

best regards,

Kent M. Ostby


"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 12 of 14 (140 Views)

Re: how to get rid of trailing white spaces using awk

I like perl a lot, but in this case there is a perfectly reasonable, compact, readable awkward solution as requested.

As I replied earlier, but I guess is got overlooked, just use the SUBstitute function using "any number of spaces at end of variable" as ere (extended regular expression) and nothing ("") as replacement string on the variable of choice.
This case $2, and often you can just work on $0.

Solution:

sub(/ *$/,"",$2)


Cheers,
Hein.
Advisor
Alexandre Dumont_1
Posts: 17
Registered: ‎01-12-2003
Message 13 of 14 (140 Views)

Re: how to get rid of trailing white spaces using awk

If possible, i would rather use sed to get rid of heading and trailing spaces:

sed -e "s/^[ \t]*//; s/[ \t]*$//;"

Regards,
Alex
Regular Advisor
Pando
Posts: 201
Registered: ‎04-15-2004
Message 14 of 14 (140 Views)

Re: how to get rid of trailing white spaces using awk

dear all,

it was a great help!
many thanks!
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.