user2057932
user2057932

Reputation: 27

Remove spaces from a single column using bash

I was provided with a CSV file, that in a single column, uses spaces to denote a thousands separator (eg. 11 000 instead of 11,000 or 11000). The other columns have useful spaces within them, so I need to only fix this one column.

My data:

Date,Source,Amount
1/1/2013,Ben's Chili Bowl,11 000.90

I need to get:

Date,Source,Amount
1/1/2013,Ben's Chili Bowl,11000.90

I have been trying awk, sed, and cut, but I can't get it to work.

Upvotes: 1

Views: 2857

Answers (3)

glenn jackman
glenn jackman

Reputation: 246754

just with bash

$ echo "Date,Source,Amount
1/1/2013,Ben's Chili Bowl,11 000.90" |
while IFS=, read -r date source amount; do
    echo "$date,$source,${amount// /}"
done
Date,Source,Amount
1/1/2013,Ben's Chili Bowl,11000.90

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753525

One possibility might be:

sed 's/\([0-9]\) \([0-9]\)/\1\2/'

This looks for two digits either side of a blank and keeps just the two digits. For the data shown, it would work fine. You can add a trailing g if you might have to deal with 11 234 567.89.

If you might have other columns with spaces between numbers, or not the first such column, you can use a similar technique/regex in awk with gsub() on the relevant field.

Upvotes: 0

Kent
Kent

Reputation: 195039

dirty and quick:

awk -F, -v OFS="," '{gsub(/ /,"",$NF)}1'

example:

kent$  echo "Date,Source,Amount
1/1/2013,Ben's Chili Bowl,11 000.90"|awk -F, -v OFS="," '{gsub(/ /,"",$NF)}1'
Date,Source,Amount
1/1/2013,Ben's Chili Bowl,11000.90

Upvotes: 7

Related Questions