Transposing lines in a csv file with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Transposing lines in a csv file with sed
# 1  
Old 12-04-2012
Transposing lines in a csv file with sed

Hi
I have a large csv file with lines like below

Code:
Date,Status,orig,dest,in,out,when,where
2012-01-01 00:30:37,I,48,56,23,98,83,34
2012-06-17 15:00:38,I,72,43,12,65,34,12

I will really appreciate if someone can help with a sed script to transpose this to

Code:
2012-01-01 00:30:37,I,orig,48
2012-01-01 00:30:37,I,dest,56
2012-01-01 00:30:37,I,in,23,
2012-01-01 00:30:37,I,out,98
2012-01-01 00:30:37,I,when,83
2012-01-01 00:30:37,I,where,34
2012-06-17 15:00:38,I,orig,72
2012-06-17 15:00:38,I,dest,43
2012-06-17 15:00:38,I,in,12
2012-06-17 15:00:38,I,out,65
2012-06-17 15:00:38,I,when,34
2012-06-17 15:00:38,I,where,12

Thanks in advance and i will really appreciate help Smilie

Last edited by vgersh99; 12-04-2012 at 05:22 PM.. Reason: thanks for using the code tags in your FIRST post!
This User Gave Thanks to kaf3773 For This Post:
# 2  
Old 12-04-2012
Code:
$ awk -F"," 'NR==1 { for(L=3; L<=NF; L++) O[L]=$L; next } { for(N=3; N<L; N++) print $1, $2, O[N], $N }' OFS="," data

2012-01-01 00:30:37,I,orig,48
2012-01-01 00:30:37,I,dest,56
2012-01-01 00:30:37,I,in,23
2012-01-01 00:30:37,I,out,98
2012-01-01 00:30:37,I,when,83
2012-01-01 00:30:37,I,where,34
2012-06-17 15:00:38,I,orig,72
2012-06-17 15:00:38,I,dest,43
2012-06-17 15:00:38,I,in,12
2012-06-17 15:00:38,I,out,65
2012-06-17 15:00:38,I,when,34
2012-06-17 15:00:38,I,where,12

$

# 3  
Old 12-04-2012
why does it have to bed done with 'sed'?
Code:
awk -F, 'FNR==1{split($0,h,FS);next}{for(i=3;i<=NF;i++) print $1,$2, h[i],$i}' OFS=, myFile

# 4  
Old 12-04-2012
Quote:
Originally Posted by vgersh99
why does it have to bed done with 'sed'?
Code:
awk -F, 'FNR==1{split($0,h,FS);next}{for(i=3;i<=NF;i++) print $1,$2, h[i],$i}' OFS=, myFile

I just tried this and it worked thanks a lot.
I mentioned sed because i have been trying to use sed to do that so i understand a little bit of sed.

If you can kindly explain the code to me a little i will really appreciate.
# 5  
Old 12-04-2012
Code:
awk -F, '
# for first line/header (FNR==1), split the record/line ($0) by FieldSeparator (FS)
# and put the result into an array 'h' index-ed by field numbers - go the next record/line (next)
FNR==1{split($0,h,FS);next}
# other records/lines
{
# iterate through the fields of the current record/line starting at the 3-rd position
# output/print the first 2 fields followed by the corresponding field in array h (h[i]) AND
# the value of the iterated field ($i)
for(i=3;i<=NF;i++) 
   print $1,$2, h[i],$i
}' OFS=, myFile

# 6  
Old 12-04-2012
Quote:
Originally Posted by vgersh99
Code:
awk -F, '
# for first line/header (FNR==1), split the record/line ($0) by FieldSeparator (FS)
# and put the result into an array 'h' index-ed by field numbers - go the next record/line (next)
FNR==1{split($0,h,FS);next}
# other records/lines
{
# iterate through the fields of the current record/line starting at the 3-rd position
# output/print the first 2 fields followed by the corresponding field in array h (h[i]) AND
# the value of the iterated field ($i)
for(i=3;i<=NF;i++) 
   print $1,$2, h[i],$i
}' OFS=, myFile

Awesome thanks very much i really appreciate. Thanks to Corona688 as well for your code.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert a horizontal lines to vertical lines in a csv file

Hi.. I need some help in converting the below horizontal lines to vertical lines format. can anyone help me on this. input file Hour,1,2,3,4,5 90RT,106,111,111,112,111 output file Hour,90RT 1,106 2,111 3,111 4,112 5,111 (3 Replies)
Discussion started by: Raghuram717
3 Replies

2. Shell Programming and Scripting

Transposing rows to columns with multiple similar lines

Hi, I am trying to transpose rows to columns for thousands of records. The problem is there are records that have the same lines that need to be separated. the input file as below:- ID 1A02_HUMAN AC P01892; O19619; P06338; P10313; P30444; P30445; P30446; P30514; AC Q29680; Q29837;... (2 Replies)
Discussion started by: redse171
2 Replies

3. Shell Programming and Scripting

Awk/sed script for transposing any number of rows with header row

Greetings! I have been trying to find out a way to take a CSV file with a large number of rows, and a very large number of columns (in the thousands) and convert the rows to a single column of data, where the first row is a header representing the attribute name and the subsequent series of... (3 Replies)
Discussion started by: tntelle
3 Replies

4. Shell Programming and Scripting

Filtering out lines in a .csv file

Hi Guys, Would need your expert help with the following situation.. I have a comma seperated .csv file, with a header row and data as follows H1,H2,H3,H4,H5..... (header row) 0,0,0,0,0,1,2.... (data rows follow) 0,0,0,0,0,0,1 ......... ......... i need a code... (10 Replies)
Discussion started by: dev.devil.1983
10 Replies

5. Shell Programming and Scripting

transposing lines to columns

Okay folks, here's a question. I tried searching but couldn't find exactly what I needed. I have a text file (excerpt below). This text file is an extract I did from several hundred pages of datasheets using grep so I could look only at the site history for each site. The problem is that... (2 Replies)
Discussion started by: jbrandt1979
2 Replies

6. Shell Programming and Scripting

Transposing X and Y axis of CSV data

Hello list, I have a source CSV data file as follows: PC_NAME,MS11-040,MS11-039,MS11-038,MS11-035 abc123,Not Applicable,Not Applicable,Not Applicable,Not Applicable abc987,Not Applicable,Not Applicable,Not Applicable,Not Applicable tnt999,Not Applicable,Not Applicable,Applicable,Not... (2 Replies)
Discussion started by: landossa
2 Replies

7. Shell Programming and Scripting

Removing lines of a .csv file

Hello, Does anyone have a one-liner to remove lines of a csv file if the value in a specific column is zero? For example, I have this file, 12345,COM,5,0,N,29.95,Y 12345,MOM,1,0,N,29.95,Y 12345,COM,4,0,N,9.99,Y 12345,MOM,0,2,N,9.99,Y 12345,REN,0,1,N,9.99,Y and I want to remove lines... (4 Replies)
Discussion started by: palex
4 Replies

8. Shell Programming and Scripting

Read Two lines in a CSV File and Compare

Hi , I have a CSV file ( file.csv) with some data as below: A,1,abc,x,y,z,,xyz,20100101,99991231 A,1,abc,x,y,z,234,xyz,20100101,99991231 I have to delete the duplicate line based on unique identifiers which are values in the fields- 2,3,4,8.These coulmns in both the rows have same... (6 Replies)
Discussion started by: Sheel
6 Replies

9. Homework & Coursework Questions

Displaying specific lines from a CSV file

1. The problem statement, all variables and given/known data: Display from a csv file, birthdays that occur today. If there are no birthdays today, the next one in the year. 2. Relevant commands, code, scripts, algorithms: The csv file is ordered from older to younger (ie. the most recent... (8 Replies)
Discussion started by: Adzi
8 Replies

10. Shell Programming and Scripting

Deleting Lines from .csv file

Hello All, I have a .csv file and I have to delete the selcted records stored in a vairable e.g echo $lname 7 88 91 94 97 100 103 106 I dont know how to pass the variable name to "sed" for deleting the $lname from a file can any one help as this is very urgent. $lname is changing the... (3 Replies)
Discussion started by: 009satya
3 Replies
Login or Register to Ask a Question