- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- how to get rid of trailing white spaces using awk
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-16-2005 08:12 PM
тАО03-16-2005 08:12 PM
how to get rid of trailing 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-16-2005 08:18 PM
тАО03-16-2005 08:18 PM
Re: how to get rid of trailing white spaces using 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-16-2005 08:49 PM
тАО03-16-2005 08:49 PM
Re: how to get rid of trailing white spaces using awk
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-16-2005 09:16 PM
тАО03-16-2005 09:16 PM
Re: how to get rid of trailing white spaces using awk
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-16-2005 09:27 PM
тАО03-16-2005 09:27 PM
Re: how to get rid of trailing white spaces using awk
use the gsub function in awk.
gsub(" ","",$MFILE); print $MFILE
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-16-2005 09:34 PM
тАО03-16-2005 09:34 PM
Re: how to get rid of trailing white spaces using awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-17-2005 12:08 AM
тАО03-17-2005 12:08 AM
Re: how to get rid of trailing white spaces using awk
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-17-2005 12:57 AM
тАО03-17-2005 12:57 AM
Re: how to get rid of trailing white spaces using awk
mode=$(awk -F "," '/Test Mode/ {print substr($2, 1, match($2, "[ \t]*$")-1)}' $MFILE)
-Amit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-17-2005 07:33 PM
тАО03-17-2005 07:33 PM
Re: how to get rid of trailing white spaces using awk
awk -F "," '/Test Mode/{print $2}' $MFILE|awk -F " " '{print $1}'
..but still I would use perl...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-17-2005 08:46 PM
тАО03-17-2005 08:46 PM
Re: how to get rid of trailing white spaces using awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-17-2005 09:10 PM
тАО03-17-2005 09:10 PM
Re: how to get rid of trailing white spaces using awk
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-18-2005 07:07 AM
тАО03-18-2005 07:07 AM
Re: how to get rid of trailing white spaces using awk
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-18-2005 09:59 AM
тАО03-18-2005 09:59 AM
Re: how to get rid of trailing white spaces using awk
sed -e "s/^[ \t]*//; s/[ \t]*$//;"
Regards,
Alex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-20-2005 07:14 PM
тАО03-20-2005 07:14 PM
Re: how to get rid of trailing white spaces using awk
it was a great help!
many thanks!