Operating System - HP-UX
1753814 Members
7889 Online
108805 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.