Register
It is currently Wed Jul 23, 2014 5:39 pm

How to create an array of 4 colum from one column


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Fri Jul 26, 2013 9:37 am   

Joined: Mon May 13, 2013 7:59 am
Posts: 4
Hi,

I have a long column and I want to transform it in an array of 4 column with the following rules:

source column
1
2
3
4
5
6
7
8
9
etc

new array
1 2 3 4
5 6 7 8
9 etc

I'm sure there is a very easy way... do someone could help for that?

thank you


Top
 Profile  
 PostPosted: Fri Jul 26, 2013 9:46 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
hi,

where does data come from ?

Code:
$ array=( 1 2 3 4 5 6 7 8 9 etc )
$ unset n
$ for i in "${array[@]}"; do ((++n%4)) && fmt='%s ' || fmt='%s\n'; printf "$fmt" "$i"; done
1 2 3 4
5 6 7 8
9 etc


Top
 Profile  
 PostPosted: Mon Jul 29, 2013 5:31 am   

Joined: Mon May 13, 2013 7:59 am
Posts: 4
Hello Watael,

The data come from a .csv file with text and number string. It is saved with tab separator.

I'm not good in bash language... could you also explain me how to load the csv file as "array" and also then to save the result in a new csv file?

Thank you


Top
 Profile  
 PostPosted: Mon Jul 29, 2013 6:28 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
Could you provide a representative sample of the CSV file, please?

files are better read using a while loop:
Code:
while read line; do echo "$line"; done < file
but there are some other ways: IFS may be adapted in the read environment only to fit the file's separator, and so split line read into varaibles or an array:
Code:
while IFS=$'\t' read var1 var2 rest[...] < file
or
Code:
while IFS=$'\t' read -a array[...] < file


Top
 Profile  
 PostPosted: Tue Jul 30, 2013 3:34 am   

Joined: Mon May 13, 2013 7:59 am
Posts: 4
I realized I wasn't clear enough in my description...

I have to do 5 (not 4...) columns reading the file line by line and then to save it in a new csv or text file.

here is an example
input file:

146
Wajir Bor
1931
204
124.4
160
Aus Manduli _ __ _
1931
357
109.8
184
War Gedud ____ _ __
1934
549
164.7

output file
146 Wajir Bor 1931 204 124.4
160 Aus Manduli _ 1931 357 109.8
184 War Gedud __ 1934 549 164.7


Top
 Profile  
 PostPosted: Tue Jul 30, 2013 5:29 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
hi,

what are «_ __ _» in «Aus Manduli _ __ _» (for example)?
why are there less in the destination file?

Code:
$ cat file
146
Wajir Bor
1931
204
124.4
160
Aus Manduli _ __ _
1931
357
109.8
184
War Gedud ____ _ __
1934
549
164.7
$ while read line; do ar+=("$line"); ((++n <5)) || { (IFS=$'\t'; echo "${ar[*]}"); unset n ar;} done <file >destFile
$ cat destFile
146     Wajir Bor       1931    204     124.4
160     Aus Manduli _ __ _      1931    357     109.8
184     War Gedud ____ _ __     1934    549     164.7


Top
 Profile  
 PostPosted: Fri Aug 02, 2013 5:49 am   

Joined: Mon May 13, 2013 7:59 am
Posts: 4
Wonderful!

It's working.

_ are just symboles from the original file. I don't need them

Thank you for your help


Lucien


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP