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!

Supressing spaces in pipe-delimited field 1

Status
Not open for further replies.

rufflocks

Technical User
Jun 29, 2001
26
US
I wish to suppress leading and trailing spaces in a pipe-delimited field so that:
| 123| will become 00123, and
|123 | will become 00123
 
use printf or sprintf on it. e.g.

{
printf( "%05d\n", $1)
}

Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Thanks. That works if I want to print the field without leading and trailing spaces.
How about if I want to do some work with the non-space value digit/characters of this field?
 
What work d'you wanna do with the digits/characters ?????

Dickie Bird (:)-)))
 
if field is numerics (all 5 digits)
write field to file1
write field to file2.
if field is alpha-numeric (some alpha, some numeric, or all apha, not spaces)
write field to file1
write "1111" to file2.
if field has leading/trailing spaces (remaining vales are numerics)
write with leading zero to file1
write "1111" to file2.

I'm not looking for the code for the above, only how to get the field without the leading/trailing spaces so it can be manipulated.
 
nawk -f spaces.awk file.txt

#------------------ spaces.awk

BEGIN {
FS="\\|"
}

{
for( i=1; i <= NF; i++)
if (match($i, &quot;(^[ ][ ]*)|([ ][ ]*$)&quot;))
print &quot;spaces [&quot; $i &quot;]&quot;
else
print &quot;no spaces [&quot; $i &quot;]&quot;
}


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
or better yet:

#----------------- spaces.awk
BEGIN {
FS=&quot;\\|&quot;
}

{
for( i=1; i <= NF; i++)
if (match($i, /(^[ ])|([ ]$)/))
print &quot;spaces [&quot; $i &quot;]&quot;
else
print &quot;no spaces [&quot; $i &quot;]&quot;
}


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top