Alex Kinman
Alex Kinman

Reputation: 2615

How to replace commas with white spaces in a csv, but inserting a different number of space after each column?

I have a file in the following format:

s1,23,789  
s2,25,689

and I would like to transform it into a file in the following format:

s1      23  789  
s2      25  689

i.e 6 white spaces between the 1st and 2nd column and only 3 white spaces between the 2nd and 3rd column?

Is there a quick way of pulling this off using sed or awk?

Upvotes: 1

Views: 570

Answers (3)

The_Non-Great
The_Non-Great

Reputation: 1

just for future seekers, I used the answer from Naumann and modified it to for my own use. My purpose is to remove commas between two columns and put a white space there. The following worked for me:

awk -F, '{ print $1,$2 }' n64215_my_file_name.txt > log_new_file_name.txt

Upvotes: 0

Jose Ricardo Bustos M.
Jose Ricardo Bustos M.

Reputation: 8164

you can try with sed

sed -r 's/(\S+),(\S+),(\S+)/\1      \2   \3/g' file

or awk using Modifiers for printf Formats

awk -vFS="," '{printf "%2s%8s%5s\n",$1,$2,$3}' file

you get,

s1      23   789  
s2      25   689

Upvotes: 4

Naumann
Naumann

Reputation: 161

using awk it's as simple as

awk -F, '{ print $1,"      ",$2,"   ",$3 }'

Upvotes: 2

Related Questions