From list to csv


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting From list to csv
# 1  
Old 07-15-2011
From list to csv

Hello,
i have a file with this row:

item1
attribute1 item1
attribute2 item1
attribute3 item1
item2
attribute1 item2
attribute2 item2
attribute3 item2
.....
itemn
attribute1 itemn
attribute2 itemn
attribute3 itemn

I would change in a new file so:
item1,attribute1 item1,attribute2 item1,attribute3 item1
...
itemn,attribute1 itemn,attribute2 itemn,attribute3 itemn

i tried with s/\n/,/g to insert comma, but how can i group item in same row?
Thanks
Bye
# 2  
Old 07-15-2011
Always 3 attributes?

---------- Post updated at 11:47 AM ---------- Previous update was at 11:40 AM ----------

Code:
[mute@sunny ~]$ awk '{ l[++c] = $0 } c==4 { for (i=1; i<4; i++) printf("%s,", l[i]); printf("%s\n", l[4]); c=0 }' attr
item1,attribute1 item1,attribute2 item1,attribute3 item1
item2,attribute1 item2,attribute2 item2,attribute3 item2
itemn,attribute1 itemn,attribute2 itemn,attribute3 itemn

# 3  
Old 07-15-2011
Or:
Code:
awk '{printf NR==1?$0:NR%4==1?"\n" $0:"," $0}END{print ""}' file

# 4  
Old 07-15-2011
OK! Array's are not needed. This is more readable Smilie

Code:
[mute@sunny ~]$ nawk 'NR%4 {printf $0",";next} 1' attr
item1,attribute1 item1,attribute2 item1,attribute3 item1
item2,attribute1 item2,attribute2 item2,attribute3 item2
itemn,attribute1 itemn,attribute2 itemn,attribute3 itemn

Edit: My first example works in old awk though. Also, it can be done with sed, you have to read in next line as well. I'm not a sed-user though this works

Code:
sed 'N;N;N;s/\n/,/g' file


Last edited by neutronscott; 07-15-2011 at 04:08 PM..
This User Gave Thanks to neutronscott For This Post:
# 5  
Old 07-16-2011
paste can do the job
Code:
# paste -d\, - - - - < file
item1,attribute1 item1,attribute2 item1,attribute3 item1
item2,attribute1 item2,attribute2 item2,attribute3 item2

Or use awk
Code:
awk '{printf ((NF==1)?((NR==1)?x:RS):",")$0}' file


Last edited by danmero; 07-16-2011 at 12:02 PM..
This User Gave Thanks to danmero For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to convert IP range csv into a list of single IP's

Hi All, I am looking for some help to convert a csv with IP ranges in.. in the format e.g. 1.1.1.2, 1.1.1.5 2.1.1.10, 2.1.1.20 and would be looking to output as follows: 1.1.1.2 1.1.1.3 1.1.1.4 1.1.1.5 2.1.1.10 2.1.1.11 etc etc up to 2.1.1.20 I have tried a few google... (4 Replies)
Discussion started by: zippyzip
4 Replies

2. 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

3. Shell Programming and Scripting

Sort and list values from CSV

I have a CSV with below values name,city,2,country name,city,15,country abc,wq,10,afdfd, qeqe,ewqre,1,wqew I need to sort them in ascending order based on the value of column 3 and then , pick the rows with values less than 10 and be able to display the columns in that row. Can anyone... (11 Replies)
Discussion started by: Nevergivup
11 Replies

4. Shell Programming and Scripting

Convert a two-column list into a csv

Hi experts, I have a very large (1.5M lines), sorted but unstructured list that looks like this: process_nameA valueA process_nameA valueA ... process_nameB valueB process_nameB valueB ... process_nameN valueN I'd like to turn this into a csv. The values are all... (4 Replies)
Discussion started by: abercrom
4 Replies

5. Shell Programming and Scripting

Perl search csv fileA where two strings exist on another csv fileB

Hi I have two csv files, with the following formats: FileA.log: Application, This occured blah Application, That occured blah Application, Also this AnotherLog, Bob did this AnotherLog, Dave did that FileB.log: Uk, London, Application, datetime, LaterDateTime, Today it had'nt... (8 Replies)
Discussion started by: PerlNewbRP
8 Replies

6. Shell Programming and Scripting

List unique values and count instances in .csv file

I need to take the second column of a .csv file and count the number of instances of each unique value in that same second column. I'd like the output to be value,count sorted by most instances. Thanks for any guidance! Data example: 317476,317756,0 816063,318861,0 313123,319091,0... (4 Replies)
Discussion started by: batcho
4 Replies

7. Shell Programming and Scripting

Converting a list to X columns of csv (& wrapping a command around it)

Hi All, Random question, how would you convert a data file from a list like so: 12345 12346 12347 12348 12349 12350 ... <snip 100+ lines> ... to comma separated X columns across: 12345,12346,12347 12348,12349,12350 Why would you want to do this? The background to this is a... (2 Replies)
Discussion started by: craigp84
2 Replies

8. Shell Programming and Scripting

CSV to SQL insert: Awk for strings with multiple lines in csv

Hi Fellows, I have been struggling to fix an issue in csv records to compose sql statements and have been really losing sleep over it. Here is the problem: I have csv files in the following pipe-delimited format: Column1|Column2|Column3|Column4|NEWLINE Address Type|some descriptive... (4 Replies)
Discussion started by: khayal
4 Replies

9. Shell Programming and Scripting

list of csv

Hi, I create on script for controlM, what need to call the csv file. But I have 100 files. I would like to have one txt file how call all file. So I create one scrip a.sh for the controlM. When I called the file xxx.csv, it work fine. but when I created one b.txt with xxx.csv yyy.csv... (1 Reply)
Discussion started by: santana
1 Replies

10. Shell Programming and Scripting

return a list of unique values of a column from csv format file

Hi all, I have a huge csv file with the following format of data, Num SNPs, 549997 Total SNPs,555352 Num Samples, 157 SNP, SampleID, Allele1, Allele2 A001,AB1,A,A A002,AB1,A,A A003,AB1,A,A ... ... ... I would like to write out a list of unique SNP (column 1). Could you... (3 Replies)
Discussion started by: phoeberunner
3 Replies
Login or Register to Ask a Question