Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command to trim spaces

Status
Not open for further replies.

new2unix

Programmer
Feb 5, 2001
143
0
0
US
Hi,

For the following command line output (without quotation mark) :

"to be sent: 50982"

I piped the output to "| grep "to be sent:" | cut -d : -f 2" to extract the number portion. How do I get rid of the spaces before the number "50982" before I assign it to a variable? Or is there another command that I can pipe to after the "cut" command to remove the spaces?

Thanks

Mike

 
You could use the tr command.

... | grep "to be sent:" | tr -s " " | cut -d" " -f2

Regards,

Greg.
 
You can also do this with awk. Assumming the number is in field 4.

... | grep "to be sent:" | awk '{print $4}'

If the number is always the last field, you could use the folowing:

... | grep "to be sent:" | awk '{print $NF}'


 
Plus the advantage of awk would be you could calculate a total as well. If the data is originally stored in file, then you could use:
Code:
awk '
    /to be sent/ { total += $NF; count++; print $NF }
    END {
        print "Total:", total
        print "Average:", (total/count)
    }
' file
Cheers, NEIL
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top