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.

6 Answers 6

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 ":"}'
0
56

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

0
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 ":"}'
1
  • 4
    Looks the third line can be abbreviated as awk -F\, '{gsub(/^[ \t]+|[ \t]+$)/, "", $2); print $2 ":"}'. Mind that powerful pipe sign! :) Jul 24, 2015 at 6:35
8

A one liner for gawk:

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

ltrim and rtrim in Unix

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

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  

Not the answer you're looking for? Browse other questions tagged or ask your own question.