anarchy
anarchy

Reputation: 5184

Using gawk to add a value separated by a comma

I have a txt file that looks like this.

this-is-name-1
this-is-name-2
...

I am trying to add a ,0 at the end of a certain line using gawk, gawk -i inplace -v n=',0' -v s='this-is-name-1' '$1 == s { $2 = n } 1' file But as you can see there is a space in-between.

this-is-name-1 ,0
this-is-name-2
...

Whats the right gawk syntax so there is no space, so it like this instead

this-is-name-1,0
this-is-name-2
...

Upvotes: 1

Views: 56

Answers (2)

glenn jackman
glenn jackman

Reputation: 246807

Use comma as the output field separator:

gawk -i inplace -v OFS=',' -v n='0' -v s='this-is-name-1' '$1 == s { $2 = n } 1' file
# ..............^^^^^^^^^^.......^ (no comma here)

Upvotes: 2

Ed Morton
Ed Morton

Reputation: 203522

Change $2 = n to $0 = $0 n. Your current code is adding a 2nd field so awk has to add a separator between the fields.

Upvotes: 1

Related Questions