Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trim leading and trailing spaces from a string in awk

Tags:

shell

unix

awk

I'm trying to remove leading and trailing space in 2nd column of the below input.txt:

Name, Order  
Trim, working
cat,cat1

I have used the below awk to remove leading and trailing space in 2nd column but it is not working. What am I missing?

awk -F, '{$2=$2};1' input.txt 

This gives the output as:

Name, Order  
Trim, working
cat,cat1

Leading and trailing spaces are not removed.

like image 673
Marjer Avatar asked Dec 15 '13 22:12

Marjer


People also ask

How do you trim leading and trailing spaces in Unix?

`sed` command is another option to remove leading and trailing space or character from the string data. The following commands will remove the spaces from the variable, $myVar using `sed` command. Use sed 's/^ *//g', to remove the leading white spaces. There is another way to remove whitespaces using `sed` command.

What is awk '{ print $3 }'?

txt. If you notice awk 'print $1' prints first word of each line. If you use $3, it will print 3rd word of each line.

Can I use cut with awk?

You can cut in awk using awk's function split . You can also filter records using a regex condition within awk, making grep and cut superfluous.

What is GSUB in awk?

gsub stands for global substitution. It replaces every occurrence of regex with the given string (sub). The third parameter is optional. If it is omitted, then $0 is used.


2 Answers

If you want to trim all spaces, only in lines that have a comma, and use awk, then the following will work for you:

awk -F, '/,/{gsub(/ /, "", $0); print} ' input.txt 

If you only want to remove spaces in the second column, change the expression to

awk -F, '/,/{gsub(/ /, "", $2); print$1","$2} ' input.txt 

Note that gsub substitutes the character in // with the second expression, in the variable that is the third parameter - and does so in-place - in other words, when it's done, the $0 (or $2) has been modified.

Full explanation:

-F,            use comma as field separator                 (so the thing before the first comma is $1, etc) /,/            operate only on lines with a comma                 (this means empty lines are skipped) gsub(a,b,c)    match the regular expression a, replace it with b,                 and do all this with the contents of c print$1","$2   print the contents of field 1, a comma, then field 2 input.txt      use input.txt as the source of lines to process 

EDIT I want to point out that @BMW's solution is better, as it actually trims only leading and trailing spaces with two successive gsub commands. Whilst giving credit I will give an explanation of how it works.

gsub(/^[ \t]+/,"",$2);    - starting at the beginning (^) replace all (+ = zero or more, greedy)                              consecutive tabs and spaces with an empty string gsub(/[ \t]+$/,"",$2)}    - do the same, but now for all space up to the end of string ($) 1                         - ="true". Shorthand for "use default action", which is print $0                           - that is, print the entire (modified) line 
like image 69
Floris Avatar answered Nov 09 '22 14:11

Floris


remove leading and trailing white space in 2nd column

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

another way by one gsub:

awk 'BEGIN{FS=OFS=","} {gsub(/^[ \t]+|[ \t]+$/, "", $2)}1' infile 
like image 31
BMW Avatar answered Nov 09 '22 14:11

BMW