Transpose from row to column using timestamp in first column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Transpose from row to column using timestamp in first column
# 8  
Old 08-15-2018
Hi Don

I changed to 6 lines the group, because my input sample changed a little. But always it will fixed to 6 lines the block.

The number of fields a group of lines is a constant. NO. it will be different in some cases.

As i say before the changes can be 6 or maximun 12 when the date does not change after 6 lines.

Sorry if dont aswer exactly your questions.

I figert out the issue using the 2 codes below..

Code:
gawk '
BEGIN{
   FS=OFS=","
}
prev!=$1 && prev{
   for(i=1;i<=count;i++){
     print prev,a[prev,i]
   }
   prev=count=""
}
{
   prev=$1
   sub(/[^,]*,/,"")
   if(count==6){
     count=1
   }
   else{
     count++
   }
   a[prev,count]=a[prev,count]?a[prev,count] OFS $0:$0
}
END{
   if(prev){
     for(i=1;i<=count;i++){
        print prev,a[prev,i]
     }
   }
}'  tmp1.txt > tmp1

gawk -F, '
 func init_block() {ts=$1;delete a;cnt=0;nf0=NF}
 func dump_block() {for(f=2;f<=nf0;f+=1){printf("%s",ts);for(r=1;r<=cnt;r+=1){printf(",%s",a[r,f])};print ""}}
 BEGIN{ts=-1}
 ts<0{init_block()}
 ts!=$1{dump_block();init_block()}
 {cnt+=1;for(f=1; f<=NF; f++) a[cnt,f]=$f}
 END{dump_block()}' < tmp1 > tmp.txt

Using this codes i got exactly the desired output.

I believe you can do the same output using a shot way code.

Thanks for your help.
# 9  
Old 08-16-2018
Since you still haven't answered several of my questions, I hate to make wild guesses at what the format of your input files really is (and the following code absolutely will not work with the sample data you provided in post #1 in this thread), but as long as each set of lines contains 6 lines, every line in each 6 line set contains the same number of fields, and there are no empty input fields or missing lines in a 6 line set (as in the file tmp1.txt that you attached to post #6); the following produces the same output as was in the file tmp2.txt that you attached to that same post and seems to me to be easier to understand:
Code:
awk '
BEGIN {	FS = OFS = ","
}
{	for(i = 2; i <= NF; i++)
		d[NR % 6, i] = $i
}
!(NR % 6) {
	for(i = 2; i <= NF; i++)
		print $1, d[1, i], d[2, i], d[3, i], d[4, i], d[5, i], d[0, i]
}' tmp1.txt

This was tested with awk on macOS High Sierra version 10.13.6, but should also work with a current gawk on a Linux system. It will also work with /usr/xpg4/bin/awk or nawk on Solaris/SunOS systems.
This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 08-17-2018
Hi Don,

Your code works perfectly, kindly can you explain a little how it works.

Many thanks
# 11  
Old 08-17-2018
Quote:
Originally Posted by jiam912
Hi Don,
Your code works perfectly, kindly can you explain a little how it works.
Many thanks
Hello jiam912,

Could you please go through following and let us know if this helps you?
Code:
awk '                    ##Starting awk code from here.
BEGIN {	FS = OFS = ","   ##Starting BEGIN section of awk where setting FS and OFS value to comma for the Input_file.
}
{	for(i = 2; i <= NF; i++)  ##Starting a for loop which starts from i=2 value to till value of NF.
		d[NR % 6, i] = $i ##Creating an array d whose index is NR%6 means will be 0 on every line which is dividable with 6 and value of variable i as second index.
}
!(NR % 6) {                       ##Checking condition here if current line value if fully dividable with 6.
	for(i = 2; i <= NF; i++)  ##Starting a for loop again here from value of i from 2 to till value of NF in line.
		print $1, d[1, i], d[2, i], d[3, i], d[4, i], d[5, i], d[0, i]  ##Printing values of array d with indexes from 1 to 6 and i value as per loop.
}' tmp1.txt                       ##mentioning Input_file name here.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 12  
Old 08-17-2018
Hi jiam912,
I think Ravinder has accurately described how each line in my script works. The slightly higher level overview is that the first loop gathers data from each line in your file as it is read and saves that data into the d[] array.

The second loop runs every time six lines of data have been accumulated into the d[] array and prints the results you want from those six lines while transposing the data it found in those six lines.

Without saving or checking the data in the 1st field on each input line, it makes the wild assumption that the 1st field on each line will have the same value. Therefore, it doesn't need to save that value and can just use the value in the 1st field of the sixth line of each set as the value to be printed in the 1st field of each output line for that set.
This User Gave Thanks to Don Cragun For This Post:
# 13  
Old 08-17-2018
Hi Don and R. Singh

Many thanks for the explanation. Highly Appreciated.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Transpose the Row and column

Hi, I have data in form of A ram B shyam C seeta D geeta A bob B methew C Richad D Mike and i want it in this form. A B C D ram shyam seeta geeta bob methew Richard Mike. please help by providing the scripting for this. (3 Replies)
Discussion started by: ricbha
3 Replies

2. Shell Programming and Scripting

How transpose column in a row?

Hello guys, First of all happy holidays and happy new year. I'm new in bioinformatic and also it is my first time that I write in this forum. Therefore, sorry if I make some mistakes. I'm writing to ask your help to fix a problem: I have a file like this: gene1 GO:0016491|GO:0055114... (8 Replies)
Discussion started by: Salvatore_espos
8 Replies

3. Shell Programming and Scripting

Transpose row to column

I'm using the testawk.awk from the following thread https://www.unix.com/shell-programming-and-scripting/18897-row-column-transpose.htmlI'm getting the following output fieldname1 data1 fieldname2 data2 fieldname3 data3 How can I get like this instead 1 fieldname1 data1 2 fieldname2 data2... (1 Reply)
Discussion started by: makkan
1 Replies

4. Shell Programming and Scripting

To transpose row into column

Hi All, In shell, I have below data coming from some some text file as below: . 351706 5861.8 0.026 0.012 12.584 0.026 0.012 12.582 0.000 0.000 0.000 Now i need the above data to be transposed as below 351706... (16 Replies)
Discussion started by: Anamica
16 Replies

5. Shell Programming and Scripting

Column to row Transpose

Hi Folks, Iam a kinda newbie to unix shell scripting, the scenario is i have a text file containing the following info Charlie chicago 15 Charlie newyork 26 jonny chicago 14 jonny newyork 15 joe chicago 15 joe newyork 18output should be Name chicago ... (3 Replies)
Discussion started by: tech_frk
3 Replies

6. UNIX for Dummies Questions & Answers

Row to column transpose

Hi there, Below is sample three rows which i need transpose into multiple rows. By keeping first 2 fields static and split them into multiple rows depend following date field. Each into seperate rows. Sample code: ... (6 Replies)
Discussion started by: ganeshd
6 Replies

7. Shell Programming and Scripting

Row to column transpose between same pattern.

Hi All, I have been trying to transpose rows to column in an large file (about 15000 lines) between matching pattern. Searched all posts in this forum, but not able find the solution to my problem. Any help appreciated.!! Input /*------XXXXXX-------*/ owner: XXXX location: XXXX... (3 Replies)
Discussion started by: RobP
3 Replies

8. Shell Programming and Scripting

Transpose column to row

Hi i have a file which has values seperated by "," as shown below and I want to transpose for every doc_id in one row. Input: DOC_ID,KEYWORD 105,REGISTROS 105,GEOLOGIA 105,NUCLEOS 105,EXPEDIENTE 105,PROGRAMAS 10025,EXPEDIENTE 10025,LOCALIZACIONES 10025,OFICIOS 10025,PROGRAMAS... (4 Replies)
Discussion started by: juelillo
4 Replies

9. Shell Programming and Scripting

How do I transpose a column of results to a row

Hi, Can anyone advise me what command I could use to display the results of the following command as a row. Thanks Gareth (6 Replies)
Discussion started by: m223464
6 Replies

10. Shell Programming and Scripting

Row to column transpose

Can we transpose rows to columns? Fields within row are separated by a comma. (10 Replies)
Discussion started by: videsh77
10 Replies
Login or Register to Ask a Question