arrange files into columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting arrange files into columns
# 1  
Old 03-12-2011
arrange files into columns

Hi may i ask how to arrange this file in into columns like example below

Problem:
#cat data.txt
Code:
1
2
3
4
5
a
b
c
d
9
8
7
6

desired output

Code:
1     a    9
2     b    8
3     c    7    
4     d    6

Also does this allowed in sed the variable inside the ' '
example
Code:
sed '{print $var}' file

or any substitution like this one the allowed variable for the purpose of multiple iteration

Thanks in advance

Last edited by radoulov; 03-12-2011 at 01:33 PM.. Reason: Code tags, please!
# 2  
Old 03-12-2011
Code:
# cat file
1
2
3
4
a
b
c
d
9
8
7
6
# awk '{_=(_==4)?1:_+1;x[_]=x[_]?x[_] t$0:$0}END{for(y=z;y++<_;){print x[y]}}' t=\\t file
1       a       9
2       b       8
3       c       7
4       d       6


Last edited by danmero; 03-12-2011 at 01:41 PM.. Reason: Add tab
This User Gave Thanks to danmero For This Post:
# 3  
Old 03-12-2011
Is this a typo or you really want to skip number 5?

---------- Post updated at 06:46 PM ---------- Previous update was at 06:36 PM ----------

This should handle 0 values in certain positions correctly Smilie

Code:
awk 'END {
  for (i = 0; ++i < m;) print _[i]  
  print _[0]  
  }
{ 
  _[NR % m] = _[NR % m] x ? _[NR % m] OFS $1 : $1
  }' m=4 OFS=\\t  infile


Last edited by radoulov; 03-12-2011 at 02:07 PM..
# 4  
Old 03-12-2011
Quote:
Originally Posted by radoulov
Is this a typo or you really want to skip number 5?

---------- Post updated at 06:46 PM ---------- Previous update was at 06:36 PM ----------

This should handle 0 values in certain positions correctly Smilie

Code:
awk 'END {
  for (i = 0; ++i < m;) print _[i]  
  print _[0]  
  }
{ 
  _[NR % m] = _[NR % m] x ? _[NR % m] OFS $1 : $1
  }' m=4 OFS=\\t  infile


Sorry its my mistake theres no number 5.
Sorry it just a ty

---------- Post updated at 04:32 AM ---------- Previous update was at 04:06 AM ----------

Quote:
Originally Posted by radoulov
Is this a typo or you really want to skip number 5?

---------- Post updated at 06:46 PM ---------- Previous update was at 06:36 PM ----------

This should handle 0 values in certain positions correctly Smilie

Code:
awk 'END {
  for (i = 0; ++i < m;) print _[i]  
  print _[0]  
  }
{ 
  _[NR % m] = _[NR % m] x ? _[NR % m] OFS $1 : $1
  }' m=4 OFS=\\t  infile

Mays is ask:
I got an error when i execute
First if arrange as in order above got and error: Unmatched '.

Second if i arrange in one line got an error got this error i think it point out on the print _[0]
Code:
awk: END {  for (i = 0; ++i < m;) print _[i]    print _[0]    }{   _[NR % m] = _[NR % m] x ? _[NR % m] OFS $1 : $1  }
awk:                                            ^ syntax error

Thanks for the reply..

Last edited by radoulov; 03-12-2011 at 04:33 PM.. Reason: Code tags, please!
# 5  
Old 03-12-2011
Hi.

Is the important part to have 3 columns or to split the input into groups of 4? ... cheers, drl
# 6  
Old 03-12-2011
Quote:
Originally Posted by jao_madn
[...]
Second if i arrange in one line got an error got this error i think it point out on the print _[0]
Code:
awk: END {  for (i = 0; ++i < m;) print _[i]    print _[0]    }{   _[NR % m] = _[NR % m] x ? _[NR % m] OFS $1 : $1  }
awk:                                            ^ syntax error

Thanks for the reply..
I suppose you didn't just copy/paste my code, but you tried to rewrite it manually.

If you want it on one line, it would be something like this:

Code:
awk 'END { for (i = 0; ++i < m;) print _[i];print _[0] }{ _[NR % m] = _[NR % m] x ? _[NR % m] OFS $1 : $1}' m=4 OFS=\\t  infile

# 7  
Old 03-12-2011
Quote:
Originally Posted by drl
Hi.

Is the important part to have 3 columns or to split the input into groups of 4? ... cheers, drl
I want to split to my desired numbers of lines into columns like example above

every 4 lines until it reaches the last line and will split it or arrange it into columns of 4 rows..

---------- Post updated at 05:38 AM ---------- Previous update was at 05:32 AM ----------

Quote:
Originally Posted by danmero
Code:
# cat file
1
2
3
4
a
b
c
d
9
8
7
6
# awk '{_=(_==4)?1:_+1;x[_]=x[_]?x[_] t$0:$0}END{for(y=z;y++<_;){print x[y]}}' t=\\t file
1       a       9
2       b       8
3       c       7
4       d       6

I accomplished my objective by using this one and replacing number 4 to muy desired numbers of lines..By the way thanks to everyone who replies.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Shell scripting - need to arrange the columns from multiple file into a single file

Hi friends please help me on below, i have 5 files like below file1 is x 10 y 20 z 15 file2 is x 100 z 245 file3 is y 78 z 23 file4 is x 100 (3 Replies)
Discussion started by: siva kumar
3 Replies

3. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

4. Shell Programming and Scripting

how to arrange datas in columns in shell script.

Hello All, I have datas in the below format. Mark 45 Steve 60 Johnson 79 Elizabeth 90 My requirement is to arrange the data in the order as shown in the attachment. My OS is Solaris. ... (6 Replies)
Discussion started by: vashece
6 Replies

5. Shell Programming and Scripting

Arrange output based on rows into columns

Hi All, I would like to ask help on how can i achieve below output. Inputfile: Oct11,apa1-daily,01:25:01 Oct11,apa2-daily,01:45:23 Oct12,apa1-daily,02:30:11 Oct12,apa2-daily,01:55:01 Oct13,apa1-off,01:43:34 Oct13,apa2-off,01:22:04 Desired output: Clients ... (3 Replies)
Discussion started by: mars101
3 Replies

6. UNIX for Dummies Questions & Answers

How to arrange the files according to time

hi friends, m new to unix environment.. i want to know that "how could we arrange the various files in our unix system according to time at which they are accessed, and are there are ways that we can arrange them in ascending or descending manner..." please help me out with full details...... (4 Replies)
Discussion started by: adityamitra
4 Replies

7. Shell Programming and Scripting

Arrange log files with AWK

Hello friends, I have too many log files to arrange. I use a simple script to create log files with below format and i forgot to create daily directory for them at the beginning. Because of this i should move all daily logs into a directory that i need to create. a part of "ls -l" output:... (1 Reply)
Discussion started by: EAGL€
1 Replies

8. Shell Programming and Scripting

Shell script to arrange files into several folders

Hello this is the script Im working on I have a picture collection that I rescued from a hard drive and there are thousands of pictures saved in one folder. What I need is to create several folders and put lets say around 200 pictures in each folder. At the end instead of having one huge... (8 Replies)
Discussion started by: kizofilax
8 Replies

9. Shell Programming and Scripting

gawk - reading two files & re arrange the columns

Hi, I am trying to read 2 files and writing to the 3rd file if I find the same elements in 2 files. my first file is 1 0 kb12124819 766409 1.586e-01 1 0 kb17160939 773886 8.674e-01 1 0 kb4475691 836671 8.142e-01 1 0 ... (2 Replies)
Discussion started by: ezhil01
2 Replies

10. Shell Programming and Scripting

re arrange the columns

Hi - can any one let me know how to re-arrange the columns of a comma seperated file. The problem here is that the colums some times have new lines and when the columns has new lines or extra comma's then it is enclosed in double quotes("). Can any one tell me how to re-arrange the columns now. ... (0 Replies)
Discussion started by: ahmedwaseem2000
0 Replies
Login or Register to Ask a Question