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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to trim the leading spaces of a field?

Status
Not open for further replies.

kobewins

Programmer
Dec 1, 2005
57
US
I have a file with some records, each of which has 13 fields seperated by "|":

B|dbl|F0245|PDCS 123|DS0000002| 99||dbl||A||||

How can I trim the leading spaces of Field 6, before '99'? I just need 99: such as

B|dbl|F0245|PDCS 123|DS0000002|99||dbl||A||||

Thank you
David
 
man awk

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Something like...
Code:
sed 's/^ *//g;s/| */|/g' file.orig > file.fixed
 
If you're column is numeric, the easiest way is to add 0 to the field forcing the column to numeric:

Code:
awk ' BEGIN { FS="|"; OFS="|" }
{ $6=$6+0
print $0 } ' data.file

If you want a generic solution for any field type: (Note the use of nawk cause I'm using solaris):

Code:
nawk ' BEGIN { FS="|"; OFS="|" }
{ gsub("^[ \t]*", "", $6)
print $0 } ' data.file
 
Thanks for all the inputs and thoughts.

David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top