Operating System - HP-UX
1819684 Members
3530 Online
109605 Solutions
New Discussion юеВ

how to get rid of trailing white spaces using awk

 
Pando
Regular Advisor

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.
13 REPLIES 13
H.Merijn Brand (procura
Honored Contributor

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
Gordon  Morrison_1
Regular Advisor

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?
Peter Godron
Honored Contributor

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
Leif Halvarsson_2
Honored Contributor

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

Hi,
use the gsub function in awk.

gsub(" ","",$MFILE); print $MFILE
Noel Miranda
Frequent Advisor

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.
Hein van den Heuvel
Honored Contributor

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.

Amit Agarwal_1
Trusted Contributor

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
Ionut Grigorescu_2
Super Advisor

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
Amit Agarwal_1
Trusted Contributor

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.
Kent Ostby
Honored Contributor

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"
Hein van den Heuvel
Honored Contributor

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.

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
Pando
Regular Advisor

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

dear all,

it was a great help!
many thanks!