41

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.

Hoa
  • 19,858
  • 28
  • 78
  • 107

6 Answers6

60

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 ":"}'
je4d
  • 7,628
  • 32
  • 46
55

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

Jong Bor Lee
  • 3,805
  • 1
  • 24
  • 27
23

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 ":"}'
gunr2171
  • 16,104
  • 25
  • 61
  • 88
JGC
  • 505
  • 1
  • 4
  • 12
  • 4
    Looks the third line can be abbreviated as `awk -F\, '{gsub(/^[ \t]+|[ \t]+$)/, "", $2); print $2 ":"}'`. Mind that powerful pipe sign! :) – syntaxerror Jul 24 '15 at 06:35
8

A one liner for gawk:

gawk -F\, '{$2 = gensub(/^[ \t]*|[ \t]*$/,"","g",$2)}'
Ben
  • 108
  • 1
  • 4
3

ltrim and rtrim in Unix

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

VIPIN KUMAR
  • 3,019
  • 1
  • 23
  • 34
0

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