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

Help in removing leading spaces in a field 2

Status
Not open for further replies.

rufflocks

Technical User
Jun 29, 2001
26
US
I have a pipe-delimited file that has 1 or more leading spaces. Do you know how to remove these leading spaces, using AWK, while I'm proceesing the file?

ex: 987| 234| 2| 567
 
Assuming you want to remove all spaces from the line, you can use the gsub function in nawk.

nawk -F"|" '{gsub(" ","",$0);print $1,$2,$3,$4}' filename

Greg.
 
Hi!

You can omit $0 in gsub function. If 3rd parameter of the gsub is not supplied, it defaults to $0.

Please, try the same example as grega's but littlebit different:

awk 'BEGIN { RS = "|" } { gsub(" ",""); print }' inputfile

Bye!

KP.
 
Thx grega. I do not want to delete all spaces, just the leading spaces. Some of the fields will have name (|first <space> last|) and address. So only leading spaces will be deleted.
 
for (i=1; i <= NF; i++)
nsub1=gsub(&quot;^[[:space:]]+|[[:space:]]+$&quot;, &quot;&quot;, $i);
 
Another option is to use sed as follows:

sed &quot;s/\(|\) */\1/&quot; filename

If you also need to remove spaces at the beginning of each line, pipe the output into another sed like so:

sed &quot;s/\(|\) */\1/&quot; filename | sed &quot;s/^ *//&quot;

Hope this is helpful.

CaKiwi
 
But if you insist on using awk, this should work.

awk '{gsub(&quot;\| *&quot;,&quot;|&quot;);gsub(&quot;^ *&quot;,&quot;&quot;);print}' filename

CaKiwi
 
CaKiwi,

sed &quot;s/\(|\) */\1/&quot; filename works for me, Thanks.

Could you please explain it in English. I know that it removes leading spaces that immediately follow a pipe, but what does each piece following sed&quot;s/ mean?

Thanks in advance.
 

The \( \) pair save everything between them (in this case just the vertical bar) in register 1, which is replayed in the substitution by the \1. An asterisk following a character matches any number of that character so the blank asterisk matches any number of blanks.

Hope this helps.

CaKiwi
 
Well,

I tried to solve this problem with original version of awk that is described in this document: gsub() function isn't avaible in original version of awk, so I must write a bunch of code:


BEGIN { FS = &quot;|&quot; }

{
# removing leading spaces
for (i = 1; i <= NF; i ++) { # any field of input line
n = split($i, ch, &quot; &quot;)
for (j = 1; j <= n; j ++) { # any word of the field
if (j == 1)
newFld = ch[j]
else
newFld = newFld &quot; &quot; ch[j]
}
if (newLn == &quot;&quot;)
newLn = newFld
else
newLn = newLn FS newFld
}

# Contens of the variable &quot;newLn&quot; is without leading spaces
# in the fields.
# Here is posible to process the contens
# of the variable &quot;newLn&quot; (instead &quot;print newLn&quot;).
print newLn

newLn = &quot;&quot;
}

Bye!

KP.
 
Although now that I look at it again it would have been easier to write:

sed &quot;s/| */|/&quot; filename

and to also remove leading blanks:

sed &quot;s/| */|/;s/^ //&quot; filename

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top