Hoa
Hoa

Reputation: 20448

How can I trim white space from a variable in awk?

Suppose $2 is my variable. I have tried going from

awk -F\, '{print $2 ":"}'

to

awk -F\, '{print gsub(/[ \t]+$/, "", $2) ":"}'

But it goes from printing something to printing nothing at all.

Upvotes: 43

Views: 114616

Answers (6)

JGC
JGC

Reputation: 602

To trim whitespace at the end of $2 use:

awk -F\, '{gsub(/[ \t]+$/, "", $2); print $2 ":"}'

To trim whitespace at the beginning of $2 use:

awk -F\, '{gsub(/^[ \t]+/, "", $2); print $2 ":"}'

And for both end and beginning:

awk -F\, '{gsub(/^[ \t]+/, "", $2); gsub(/[ \t]+$/, "", $2); print $2 ":"}'

Upvotes: 25

A more simple option:

You have to define the trim function, the use gsub for a general replacement. Then find with a regex if starts or ends with space(one or more times), returns the field.

   awk -F'|' 'function trim(field){
   gsub(/^ +| +$/,"", field); 
   return field
   }{
   OFS=",";
   print trim($2),trim($3)}' stations > lista.csv

With this code is enougth

And the file stations is with this structure:

     1 | UTAR | U de Tarapacá                   
     3 | VALN | Valparaíso                      
     4 | JRGN | Junta Gorgino                   
     6 | TRPD | Torpederas  

Upvotes: 0

VIPIN KUMAR
VIPIN KUMAR

Reputation: 3147

ltrim and rtrim in Unix

awk 'BEGIN{FS=OFS="|"} {gsub(/^[ \t]+|[ \t]+$/, "", $2)}1' filename.txt

Upvotes: 3

Ben
Ben

Reputation: 108

A one liner for gawk:

gawk -F\, '{$2 = gensub(/^[ \t]*|[ \t]*$/,"","g",$2)}'

Upvotes: 8

Jong Bor Lee
Jong Bor Lee

Reputation: 3855

These functions come in handy to improve readability. They also return the trimmed result:

function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }
BEGIN {
# whatever
}
{
# whatever
}
END {
# whatever
} 

Source: https://gist.github.com/andrewrcollins/1592991

Upvotes: 59

je4d
je4d

Reputation: 7848

You're printing the result of the gsub, but gsub does an in-place modify of $2 instead of returning a modified copy. Call gsub, then print:

awk -F\, '{gsub(/[ \t]+$/, "", $2); print $2 ":"}'

Upvotes: 63

Related Questions