Jose Campos
Jose Campos

Reputation: 23

Put a comma in a specific column

I would like to know how to put a comma in one column (space). For example.

a b c d e

And I would like this.

a b c d, e

A comma in the 4th space.

I tried with this command.

awk -F '{print $4}' < file.txt | cut -d"," -f4-

Upvotes: 0

Views: 650

Answers (3)

Claes Wikner
Claes Wikner

Reputation: 1517

echo a b c d e| awk '{$0=gensub(/ /,", ",4)}1'
a b c d, e

Upvotes: 0

RavinderSingh13
RavinderSingh13

Reputation: 133428

If you have only 5 fields(or in case you have more fields in your Input_file and you want to perform this for second last field) in your Input_file then following may also help you in same.

awk '{$(NF-1)=$(NF-1)","} 1'  Input_file

Or with sed simply replace 4th space with comma as follows.

sed 's/ /, /4'   Input_file

Upvotes: 3

Ed Morton
Ed Morton

Reputation: 203209

$ awk '{$4=$4","}1' file
a b c d, e

Upvotes: 3

Related Questions