Converting line output to column based output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting line output to column based output
# 1  
Old 03-22-2010
Converting line output to column based output

Hi Guys,

I am trying to convert a file which has a row based output to a column based output. My original file looks like this:

Code:
1
2
3
4
5
6
1
2
3
 
 
 
1
2
3
4
 
 
1
2
3
4
5
6

I want output of:

Code:
1 1 1 1
2 2 2 2
3 3 3 3
4    4 4
5       5
6       6

I am using the following statement:

Code:
awk '{a[NR%6]=a[NR%6]" "$0} END{for (i=1;i<=6;i++) {print o= (i!=6)? a[i] : a[0] } }' test.txt > test1.txt

For some reason, it is not seeing the blank lines.

What may be wrong with this statement?

Thanks.
# 2  
Old 03-22-2010
What do you mean the awk script doesn't see the blank lines ? What does it output ?
It works fine with me (using nawk vs awk on Solaris).
Code:
$ cat test.txt
1
2
3
4
5
6
1
2
3
 
 
 
1
2
3
4
 
 
1
2
3
4
5
6
$ nawk '{a[NR%6]=a[NR%6]" "$0} END{for (i=1;i<=6;i++) {print o= (i!=6)? a[i] : a[0] } }' test.txt > test1.txt 
$ cat test1.txt
 1 1 1 1
 2 2 2 2
 3 3 3 3
 4   4 4
 5     5
 6     6

# 3  
Old 03-23-2010
Ok can you try the attached file and send me the output file?

The delimiter is 53. The third group of data does not have all 53 points.

When I try this file, the third group appears in the last column in the output file. It should appear as the third column.

Here is what I am using:

Code:
awk '{a[NR%53]=a[NR%53]" "$0} END{for (i=1;i<=53;i++) {print o= (i!=53)? a[i] : a[0] } }' test.txt  >  test1.txt

# 4  
Old 03-23-2010
Works fine with me. I got 53 lines with 10 columns minus some empty fields. Perhaps are those empty fields an issue ?
# 5  
Old 03-23-2010
I got the attached file.

The empty fields is not the problem. The one column which has the empty fields is the last column. That's the problem. That last column should be the third one.

Can you upload the output file you get?

Thanks.
# 6  
Old 03-23-2010
Ok I found out what was happening. The original text file had a (space) on every blank line that was messing up the order. I removed the spaces and it worked just fine.

Thanks.
# 7  
Old 03-24-2010
Code:
my $ref;
while(<DATA>){
	chomp;
	my $tmp = $.%6;
	my $first=($tmp==0)?6:$tmp;
	my $second=($tmp==0)?int($./6):int($./6)+1;
	$ref->[$first]->[$second]=$_;
}
my @tmp=@{$ref};
for(my $i=1;$i<=6;$i++){
	my @t= @{$tmp[$i]};
	for(my $j=1;$j<=$#t+1;$j++){
		print $t[$j]," ";
	}
	print "\n";
}
__DATA__
1
2
3
4
5
6
1
2
3
 
 
 
1
2
3
4
 
 
1
2
3
4
5
6

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a multi-line shell command output and execute logic based on result

The following is a multi-line shell command example: $cargo build Compiling prawn v0.1.0 (/Users/ag/rust/prawn) error: failed to resolve: could not find `setup_panix` in `human_panic` --> src/main.rs:14:22 | 14 | human_panic::setup_panix!(); | ... (2 Replies)
Discussion started by: yogi
2 Replies

2. Ubuntu

Converting Impress output to DVD video

I have a need to convert 'Impress' presentations to DVD video format whilst retaining the original presentation in situ. Copying the individual slides and modifying size etc. in Gimp and then importing them into 'Openshot' is both long winded and the loss of quality is significant. Can anybody... (14 Replies)
Discussion started by: Royalist
14 Replies

3. Shell Programming and Scripting

Read in 2-column CSV, output many files based on field

Is there a way to read in a two-columned CSV file, and based on the fields in 1st column, output many different files? The input/output looks something like: input.csv: call Call Mom. call Call T-Mobile. go Go home. go Go to school. go Go to gas station. play Play music. play Play... (4 Replies)
Discussion started by: pxalpine
4 Replies

4. Shell Programming and Scripting

Need help converting df output to gigabytes

I need some help converting the disk space values in kilobytes to gigabytes. I can't use df -h because the report has to be in megabytes for some disk space tracking software the customer is using. I have been playing around with trying to assign variables I can use outside of awk so I can do... (1 Reply)
Discussion started by: kuliksco
1 Replies

5. Shell Programming and Scripting

Find in first column and replace the line with Awk, and output new file

Find in first column and replace the line with Awk, and output new file File1.txt"2011-11-02","Georgia","Atlanta","x","","" "2011-11-03","California","Los Angeles","x","","" "2011-11-04","Georgia","Atlanta","x","x","x" "2011-11-05","Georgia","Atlanta","x","x","" ... (4 Replies)
Discussion started by: charles33
4 Replies

6. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

7. Shell Programming and Scripting

top output for six processes with the same name, output changed from column to row

Hi, I have a system under test, and I use a script that does a ps. The output, is in the following format, it's basically the timestamp, followed by the rss and vsize. 09:03:57 68404 183656 68312 181944 69860 217360 67536 182564 69072 183172 69032 199276 09:04:27 68752 183292 70000 189020... (5 Replies)
Discussion started by: Bloke
5 Replies

8. Shell Programming and Scripting

Converting to columnar output

Hi All, I need some help in script or awk to create a textfile. I have a directory where two subdirectories exists say A and B. Now I need to write a ".txt" file with well arranged(space wraped) three columns in it with the data as follows: 1st column:Serial number(which will increment... (4 Replies)
Discussion started by: Sreejith_VK
4 Replies

9. Shell Programming and Scripting

how to make a line BLINKING in output and also how to increase font size in output

how to make a line BLINKING in output and also how to increase font size in output suppose in run a.sh script inside echo "hello world " i want that this should blink in the output and also the font size of hello world should be big .. could you please help me out in this (3 Replies)
Discussion started by: mail2sant
3 Replies

10. UNIX for Dummies Questions & Answers

Converting gettimeoftheday output to string

Hi .. is there a way i can retrieve the output of gettimeoftheday in a string (1 Reply)
Discussion started by: p_aishwarya
1 Replies
Login or Register to Ask a Question