Removing trailing spaces from a field using awk 
Author Message
 Removing trailing spaces from a field using awk

Quote:

> You probably only need to do the following in your program:

> sub(/[ ][ ]*$/,"",$3)

The awk regular expression constant to match "one or more space
characters" is simply / +/. It's never necessary to specify a
single-character pattern as a regular expression character class.
Also, the plus quantifier ("+") is a valid regular expression
metacharacter in all versions of awk.

--
Jim Monty

Tempe, Arizona USA



Sat, 21 Apr 2001 03:00:00 GMT  
 Removing trailing spaces from a field using awk

Quote:

> Hi, I have a file say with 4 fields. The field no. 2 and 3 contains some
> trailing spaces. I want to remove trailing spaces only for field 3 and not
> for field 2. How it can be done. The field seperator is ",". As I understand
> I can use sub function. But sub works on full line (source string) as the
> input and not the field as the source string input.

     BEGIN { FS = OFS = "," }

     { sub(/ +/, "", $3); print }

     # or sub(/[ \t]+/, "", $3)
     # or sub(/[[:space:]]+/, "", $3) (POSIX awk)
     # or $3 = rtrim($3), where rtrim() is function rtrim(s) {
     #                                         sub(/[ \t]+/, "", s)
     #                                         return s
     #                                     }

Your choice of regular expression depends on your data and on the
version of awk you're using.

--
Jim Monty

Tempe, Arizona USA



Sat, 21 Apr 2001 03:00:00 GMT  
 Removing trailing spaces from a field using awk


Quote:

>> Hi, I have a file say with 4 fields. The field no. 2 and 3 contains some
>> trailing spaces. I want to remove trailing spaces only for field 3 and not
>> for field 2. How it can be done. The field seperator is ",". As I understand
>> I can use sub function. But sub works on full line (source string) as the
>> input and not the field as the source string input.

>     BEGIN { FS = OFS = "," }

>     { sub(/ +/, "", $3); print }

>     # or sub(/[ \t]+/, "", $3)
>     # or sub(/[[:space:]]+/, "", $3) (POSIX awk)
>     # or $3 = rtrim($3), where rtrim() is function rtrim(s) {
>     #                                         sub(/[ \t]+/, "", s)
>     #                                         return s
>     #                                     }

>Your choice of regular expression depends on your data and on the
>version of awk you're using.

You should use $ to terminate the regular expression you're using
if you want to specify trailing whitespace.  Something like this:

     { sub(/ +$/, "", $3); print }

What was specified (without the $) would eliminate leading spaces,
interior, or trailing spaces in that order depending upon what
existed.  Remember, with a comma as the field separator, there can be
spaces in many places, so the regular expression should be precise.

Chuck Demas
Needham, Mass.
[posted and emailed]
--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sun, 22 Apr 2001 03:00:00 GMT  
 Removing trailing spaces from a field using awk


Quote:

>> Hi, I have a file say with 4 fields. The field no. 2 and 3 contains some
>> trailing spaces. I want to remove trailing spaces only for field 3 and not
>> for field 2. How it can be done. The field seperator is ",". As I understand
>> I can use sub function. But sub works on full line (source string) as the
>> input and not the field as the source string input.

>     BEGIN { FS = OFS = "," }

>     { sub(/ +/, "", $3); print }

>     # or sub(/[ \t]+/, "", $3)
>     # or sub(/[[:space:]]+/, "", $3) (POSIX awk)
>     # or $3 = rtrim($3), where rtrim() is function rtrim(s) {
>     #                                         sub(/[ \t]+/, "", s)
>     #                                         return s
>     #                                     }

>Your choice of regular expression depends on your data and on the
>version of awk you're using.

You should use $ to terminate the regular expression you're using
if you want to specify trailing whitespace.  Something like this:

     { sub(/ +$/, "", $3); print }

What was specified (without the $) would eliminate leading spaces,
interior, or trailing spaces in that order depending upon what
existed.  Remember, with a comma as the field separator, there can be
spaces in many places, so the regular expression should be precise.

Chuck Demas
Needham, Mass.
[posted and emailed]
--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sun, 22 Apr 2001 03:00:00 GMT  
 Removing trailing spaces from a field using awk

Quote:


> >     { sub(/ +/, "", $3); print }

> You should use $ to terminate the regular expression you're using
> if you want to specify trailing whitespace. Something like this:

>      { sub(/ +$/, "", $3); print }

> ... [T]he regular expression should be precise.

Hey, I'm too busy correcting other people's wrong answers! I don't have
time to check my own comp.lang.awk posts for errors! Besides, I was only
missing one metacharacter!

Seriously though, folks. I apologize for the mistake. Thanks, Chuck, for
catching it.

--
Jim Monty

Tempe, Arizona USA                                   ESCHEW OBFUSCATION!



Sun, 22 Apr 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. removing trailing spaces from string variables

2. C4-Removing extra spaces from memo field???

3. SMASHING A FIELD (remove spaces; left justify)

4. Removing spaces from fields

5. how do i remove the last field with awk, and save it

6. using awk to remove header and footer lines

7. Question: How to remove END OF LINE using AWK or SED

8. Adding and removing fields when using a Mclistbox

9. Modifying existing script to remove trailing newlines

10. Remove trailing newlines from file

11. Removing trailing blanks

12. how to remove trailing blanks of a string?

 

 
Powered by phpBB® Forum Software