0

I'm newer with bash scripting. I have a regular txt file with three columns of data. I want to make this into csv format.

hagabe   3322   7/28/14
uenebh   3878   7/26/14
jheheb   5569   7/27/14

Making it look like:

hagabe,3322,7/28/14

I know I could take each column and make a file then use paste to create the csv but that seems like over kill. There has to be an easier way. Would it be logical to simply remove the spaces and add the comas? Any suggestions are appreciated!

1
  • 4
    Assuming the data is tab delimited: tr '\t' ',' Commented Jul 28, 2014 at 17:54

4 Answers 4

3

Just use awk with OFS set to ,:

awk -v OFS=, '{$1=$1}1' file

Output:

hagabe,3322,7/28/14
uenebh,3878,7/26/14
jheheb,5569,7/27/14
Sign up to request clarification or add additional context in comments.

1 Comment

This is the idiomatic awk answer. The odd-looking $1=$1 is there to force awk to re-format the line using the output field separator. The NF condition is not strictly necessary.
0

Through awk,

$ awk '{gsub(/ +/,",")}1' file
hagabe,3322,7/28/14
uenebh,3878,7/26/14
jheheb,5569,7/27/14

Comments

0
$ cat in
hagabe   3322   7/28/14
uenebh   3878   7/26/14
jheheb   5569   7/27/14

$ while read -ra line; do IFS=, eval echo \"\${line[*]}\"; done <in
hagabe,3322,7/28/14
uenebh,3878,7/26/14
jheheb,5569,7/27/14

Comments

0

Pipe your file though sed:

cat <file> | sed 's/[ ]\+/,/g'

1 Comment

cat <file> | sed 's/[ ]\+/,/g' or shorter sed 's/[ ]\+/,/g' <file>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.