Convert from CSV to space padded columns (.ksh)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert from CSV to space padded columns (.ksh)
# 1  
Old 10-21-2008
Convert from CSV to space padded columns (.ksh)

Hello,

Could someone please help me to convert a string(s) of comma separated values into space padded columns in .ksh?

ex.

10-21-2008,someword,blah,127.0.0.1,8,3
10-21-2008,randomword,ick,128.0.111.128,1,0

converted to

10-21-2008 someword blah 127.0.0.1 8 3
10-21-2008 randomword ick 128.0.111.128 1 0

Any help would be greatly appreciated,
Thanks.
# 2  
Old 10-21-2008
string substitution is very common and easy task and appears alot in this forum. what have you tried?
# 3  
Old 10-21-2008
Bug Convert those commas to spaces

you can use sed or tr or any other string replacement type function. The best thing is to try it yourself at least to come up with something then others can guide you in the right direction.
# 4  
Old 10-22-2008
Here is what I have tried:

printf "%-30s\n" ${arr[$i]} |sed 's/,/ /g'
which gives me no padding, just a space in place of commas

printf "%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n", ${arr[$i]} |sed 's/,/ /g'
which does the same as above, only now it is double spaced for some reason.

So am I close?
# 5  
Old 10-22-2008
Hammer & Screwdriver Since you are saying space-padded

rather than a tr command to translate characters, what about the following?

Code:
> echo "10-21-2008,someword,blah,127.0.0.1,8,3" | awk -F"," '{printf("%20s %20s %20s %20s %5s %5s \n", $1,$2,$3,$4,$5,$6)}'

          10-21-2008             someword                 blah            127.0.0.1     8     3 

>

Beware of spaces in the command, since the following will provide a different output layout.
Code:
> echo "10-21-2008,someword,blah,127.0.0.1,8,3" | awk -F"," '{printf("%20s%20s%20s%20s%5s%5s\n", $1,$2,$3,$4,$5,$6)}'

          10-21-2008            someword                blah           127.0.0.1    8    3

>

# 6  
Old 10-22-2008
What about using a simple sed command?

Code:
echo "10-21-2008,someword,blah,127.0.0.1,8,3" | sed 's/\,/ /g'

# 7  
Old 10-22-2008
Got It

echo "10-21-2008,someword,blah,127.0.0.1,8,3" | awk -F"," '{printf("%20s %20s %20s %20s %5s %5s \n", $1,$2,$3,$4,$5,$6)}'
-> Worked perfectly.

echo "10-21-2008,someword,blah,127.0.0.1,8,3" | sed 's/\,/ /g'
-this worked great for switching out the commas with spaces, but it doesn't format the output be in neat looking columns with the space padding.

Thank you both for your suggestions.

Last edited by WhotheWhat; 10-22-2008 at 04:20 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Replace columns in .csv using other .csv columns

Hi, I have 2 csv files with 15 000 lines, which looks like this: Daily.csv "CODE","BRAND","DESIGNER","SIZE","TYPE","GENDER","SET","DESCRIPTION","IMAGE","COST","WEIGHT","MSRP","UPC" "M-1001","212","Caroline Her","1.7 oz","EDT... (4 Replies)
Discussion started by: olivieraz
4 Replies

2. Shell Programming and Scripting

Import 2 columns from 8 .csv files into pandas df (side by side) and write a new csv

I have 8 .csv files with 16 columns and "n" rows with no Header. I want to parse each of these .csv and get column and put the data into a new.csv. Once this is done, the new.csv should have 16 columns (2 from each input.csv) and "n" rows. Now, I want to just take the average of Column from... (3 Replies)
Discussion started by: Zam_1234
3 Replies

3. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

4. Shell Programming and Scripting

How to convert space&tab delimited file to CSV?

Hello, I have a text file with space and tab (mixed) delimited file and need to convert into CSV. # cat test.txt /dev/rmt/tsmmt32 HP Ultrium 6-SCSI J3LZ 50:03:08:c0:02:72:c0:b5 F00272C0B5 0/0/6/1/1.145.17.255.0.0.0 /dev/rmt/c102t0d0BEST /dev/rmt/tsmmt37 ... (6 Replies)
Discussion started by: prvnrk
6 Replies

5. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

6. UNIX for Dummies Questions & Answers

How convert space separated list to matched columns?

Hi I have been racking my (limited) brains to get this to work without success I have a file output which is a list of lists - ie a single column of data that is separated by space into sub lists below - I need to both split this so that each list is in a separate column (eg tab or semicolon... (8 Replies)
Discussion started by: Manchesterpaul
8 Replies

7. Shell Programming and Scripting

Compare 2 csv files in ksh and o/p the difference in a new csv file

(say) I have 2 csv files - file1.csv & file2.csv as mentioned below: file1.csv ID,version,cost 1000,1,30 2000,2,40 3000,3,50 4000,4,60 file2.csv ID,version,cost 1000,1,30 2000,2,45 3000,4,55 6000,5,70 The... (7 Replies)
Discussion started by: Naresh101
7 Replies

8. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

9. UNIX for Advanced & Expert Users

Help in Deleting columns and Renaming Mutliple columns in a .Csv File

Hi All, i have a .Csv file in the below format startTime, endTime, delta, gName, rName, rNumber, m2239max, m2239min, m2239avg, m100016509avg, m100019240max, metric3min, m100019240avg, propValues 11-Mar-2012 00:00:00, 11-Mar-2012 00:05:00, 300.0, vma3550a, a-1_CPU Index<1>, 200237463, 0.0,... (9 Replies)
Discussion started by: mahi_mayu069
9 Replies

10. UNIX for Advanced & Expert Users

Unix Bash: substitute columns in .csv using other .csv columns

Hi All, I have two .csv's input.csv having values as (7 columns) ABC,A19907103,ABC DEV YUNG,2.17,1000,2157,07/07/2006 XYZ,H00213850,MM TRUP HILL,38.38,580,23308,31/08/2010 output.csv having (25 columns) A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y... (4 Replies)
Discussion started by: abhivyas
4 Replies
Login or Register to Ask a Question