converting rows into columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting converting rows into columns
# 8  
Old 01-03-2011
use below:

form line to paragraphs:

Code:
perl -wlne '$_ =~ s/\s+/\n/g;print $_,"\n";' infile.txt > outfile.txt


for reverse:
from paragraphs to lines:

Code:
perl -00 -wne 's/\n/\t/g;print $_,"\n"'  infile.txt > outfile.txt


Last edited by ahmad.diab; 01-03-2011 at 02:17 PM..
This User Gave Thanks to ahmad.diab For This Post:
# 9  
Old 01-03-2011
Code:
xargs -n3 < file

This User Gave Thanks to Franklin52 For This Post:
# 10  
Old 01-03-2011
A bit fork/exec intensive, but yes, that is another way to collect N lines into one:
Code:
xargs -n4 echo < file

This User Gave Thanks to DGPickett For This Post:
# 11  
Old 01-04-2011
Quote:
Originally Posted by Scrutinizer
Code:
awk '$1=$1' RS= file


when i try your command, it is giving me the following output.

Code:
PC_1
wf_test1
Test
PC_2
wf_test2
Test
PC_3
wf_test3
Test

but what i want is

Code:
PC_1 wf_test1 Test
PC_2 wf_test2 Test
PC_3 wf_test3 Test

---------- Post updated at 04:40 PM ---------- Previous update was at 04:31 PM ----------

Quote:
Originally Posted by DGPickett
A bit fork/exec intensive, but yes, that is another way to collect N lines into one:
Code:
xargs -n4 echo < file

It gives the following output.

Code:
PC_1 wf_test1 Test PC_2
wf_test2 Test PC_3 wf_test3
Test

But I want to start output in next line after encountering the word 'Test'

ex:

Code:
PC_1 wf_test1 Test 
PC_2 wf_test2 Test 
PC_3 wf_test3 Test

# 12  
Old 01-04-2011
Quote:
Originally Posted by svajhala
when i try your command, it is giving me the following output

Code:
PC_1
wf_test1
Test
PC_2
wf_test2
Test
PC_3
wf_test3
Test

but what i want is

[CODE]
PC_1 wf_test1 Test
PC_2 wf_test2 Test
PC_3 wf_test3 Test
It only works when there are empty lines between the records, as is the case in the sample input file.
This User Gave Thanks to Scrutinizer For This Post:
# 13  
Old 01-04-2011
Back in #1 there were blank lines so 4 was a good number.
This User Gave Thanks to DGPickett For This Post:
# 14  
Old 01-04-2011
Quote:
Originally Posted by svajhala
when i try your command, it is giving me the following output.

Code:
PC_1
wf_test1
Test
PC_2
wf_test2
Test
PC_3
wf_test3
Test

but what i want is

Code:
PC_1 wf_test1 Test
PC_2 wf_test2 Test
PC_3 wf_test3 Test

The supplied code only works with GNU awk for plain awk you might need something like this:
Code:
awk '!$0;$0{printf $0" "}END{print}' infile

Another version with extra space on end of each line removed:
Code:
awk '!$0{print; f=0}$0{printf(f++?" ":"")$0}END{if(f)print}' infile


Last edited by Chubler_XL; 01-04-2011 at 06:48 PM.. Reason: Added version with extra space removed
This User Gave Thanks to Chubler_XL 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

Converting columns of text to rows, with blank lines

I've spent the past hour trying different things and googling for this solution and cannot find the answer. Found variations of this, but not this exact thing. I have the following text, which is the output from our mainframe. Each field is on a separate line, with a blank line between each... (7 Replies)
Discussion started by: lupin..the..3rd
7 Replies

2. Shell Programming and Scripting

Converting rows into columns

hi all I need a help ..I have a script that takes has command and its output is like below.. a b a v v a c I am assigning the above outputs to a variable .. <variable name> = <command output> problem here is when I echo the variable ..it gives me output like " a b... (3 Replies)
Discussion started by: shankarb
3 Replies

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

4. UNIX for Dummies Questions & Answers

Converting txt output to rows and columns and send report via mail.

Hi All, I would like to send below output in a tabular column ( xml or excel ) and send a mail. vinay unix anil sql vamsee java request to suggest a solution. (1 Reply)
Discussion started by: Girish19
1 Replies

5. UNIX for Dummies Questions & Answers

Converting Columns To Rows Sort Of

Hi I'm a UNIX awk and sed novice at best. I'm trying to creat a .csv file so it can be graphed in Excel. Tried various xargs, awk, sed and paste but just can't seem to get the data to line up. Not sure if this is beyond for a question in these forums. Any help would greatly be appreciated. Have... (4 Replies)
Discussion started by: jimmyf
4 Replies

6. Shell Programming and Scripting

Converting rows to columns in csv file

Hi, I have a requirement to convert rows into columns. data looks like: c1,c2,c3,.. r1,r2,r3,.. p1,p2,p3,.. and so on.. output shud be like this: c1,r1,p1,.. c2,r2,p2,.. c3,r3,p3,.. Thanks in advance, (12 Replies)
Discussion started by: Divya1987
12 Replies

7. Shell Programming and Scripting

Converting rows to columns using shell script

I have a script which converts rows to columns. file_name=$1 mailid=$2 #CREATE BACKUP OF ORIGINAL FILE #cp ${file_name}.xlsx ${file_name}_temp.xlsx #tr '\t' '|' < ${file_name}_temp.xlsx > ${file_name}_temp.csv #rm ${file_name}_temp.xlsx pivot_row=`head -1 ${file_name}` sed 1d... (3 Replies)
Discussion started by: andy538
3 Replies

8. UNIX for Dummies Questions & Answers

Converting columns into rows

Is there anyway to convert columns into raws using awk? (or any other command line):eek::eek::eek::eek::eek::eek::eek::eek::eek: (1 Reply)
Discussion started by: cosmologist
1 Replies

9. UNIX for Dummies Questions & Answers

Converting rows into multiple-rows

Hi every one; I have a file with 22 rows and 13 columns which includes floating numbers. I want to parse the file so that every five columns in the row would be a new record (row). For example, the first line in the old file should be converted into three lines with first two lines contain 5... (6 Replies)
Discussion started by: PHL
6 Replies

10. Shell Programming and Scripting

how to converting rows to columns, bash

I have in file these words: @fraza1 = rw @fraza2 = r @fraza3 = r @fraza4 = r @fraza5 = r @fraza1 = r @fraza6 = r @fraza7 = r @fraza2 = r @fraza8 = r @fraza9 = r ... I would like so that: ,rw,@fraza1 ,r,@fraza2 (2 Replies)
Discussion started by: patrykxes
2 Replies
Login or Register to Ask a Question