How can I trim white space from a variable in awk?
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 ":"}'
Trim leading and trailing spaces from a string in awk
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
Trim extra spaces in AWK
For the current example, another option might be to recalculate the text of the input record by first setting the value of line to the input record and then use $1=$1
awk -v line=" foo bar " 'END {$0=line; $1=$1; print}' somefile.txt
Output (the quotes are only for clarity that there are no leading or trailing spaces)
"foo bar"
The inner workings how the spaces are removed are described in the comments by Ed Morton:
Setting $0=line
or any other change to $0
would trigger the fields being recalculated.
Using $1=$1
triggers the record to be recalculated in as much as it'll be rebuilt from the existing fields thereby stripping leading/trailing white space and replacing every other chain of contiguous white space with a single blank char (assuming the default FS and OFS are used).
removing a space from a string by awk
gsub
is your friend. The following command basically does a global substitution of a regular expression (a single space in this case), replacing it with an empty string, on the target $0
(the whole line).
pax> echo "steve john" | awk '{ gsub (" ", "", $0); print}'
stevejohn
You can use any target, including one input by a user:
pax> awk 'BEGIN {getline xyzzy ; gsub(" ","", xyzzy) ; print xyzzy}'
hello there my name is pax
hellotheremynameispax
Bash: How to trim whitespace before using cut
Pipe your command to awk, print the 3rd column, a space and the 4th column like this
<command> | awk '{print $3, $4}'
This results in:
12345 KB
or
<command> | awk '{print $3}'
if you want the naked number.
How to awk print a subtring and trim trailing and ending white space of the same subtring in bash
You can use sub()
to substitute the white space away, and awk
can do the pattern match for you, too:
xinput -list --short | awk '
/keyboard.*slave/ {
s = substr($0, 7, 41);
sub(/ *$/, "", s);
print s;
exit;
}'
That would stop after the first matching line, which I suppose you wanted, since the original only acted on the first record read by awk
.
So,
$ cat keyb
mouse mouse something
keyboard slave foo
keyboard slave bar
$ awk '/keyboard.*slave/ { s = substr($0, 7, 41); sub(/ *$/, "", s); print s; exit; }' < keyb
rd slave foo
Related Topics
What Is the Linux Equivalent to Dos Pause
Faster Forking of Large Processes on Linux
Docker-Compose Up and User Inputs on Stdin
How to Automatically Add User Account and Password with a Bash Script
Best Way to Find Os Name and Version in Unix/Linux Platform
Term Environment Variable Not Set
Redirecting Stdout with Find -Exec and Without Creating New Shell
Gem5 Syscall Emulation Arm C Hello World Fails with "Fatal: Syscall Gettid (#224) Unimplemented"
Linux: Compute a Single Hash for a Given Folder & Contents
Bash: Inserting a Line in a File at a Specific Location
How to Increase Ble Advertisement Frequency in Bluez
Running 32 Bit Assembly Code on a 64 Bit Linux & 64 Bit Processor:Explain the Anomaly
Docker Command Can't Connect to Docker Daemon
How to Compile/Install Node.Js(Could Not Configure a Cxx Compiler!) (Ubuntu)
Replace Whitespace with a Comma in a Text File in Linux