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.
`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.
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.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With