Transpose the 2nd coloumn in every fourth row three times....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Transpose the 2nd coloumn in every fourth row three times....
# 1  
Old 01-24-2019
Transpose the 2nd coloumn in every fourth row three times....

I have several csv files, each hundreds of rows with the following layout

Code:
"","SEQ ID No.  1 Subject # 20"
"BPM neg.:",68.83
"BPM normal:",68.48
"SEQ ID No.  1 Subject # 20",79.33
"","SEQ ID No.  10 Subject # 20"
"BPM neg.:",68.05
"BPM normal:",68.58
"SEQ ID No.  10 Subject # 20",81.63
"","SEQ ID No.  17 Subject # 20"
"BPM neg.:",71.06
"BPM normal:",71.19
"SEQ ID No.  17 Subject # 20",81.99

I would need the first and fifth and ninth and.... row be transposed into a third coloumn three times giving me this output:


Code:
"BPM neg.:",68.83"","SEQ ID No.  1 Subject # 20"
"BPM normal:",68.48"","SEQ ID No.  1 Subject # 20"
"SEQ ID No.  1 Subject # 20",79.33"","SEQ ID No.  1 Subject # 20"
"BPM neg.:",68.05"","SEQ ID No.  10 Subject # 20"
"BPM normal:",68.58"","SEQ ID No.  10 Subject # 20"
"SEQ ID No.  10 Subject # 20",81.63"","SEQ ID No.  10 Subject # 20"
"BPM neg.:",71.06"","SEQ ID No.  17 Subject # 20"
"BPM normal:",71.19"","SEQ ID No.  17 Subject # 20"
"SEQ ID No.  17 Subject # 20",81.99"","SEQ ID No.  17 Subject # 20"

Many thanks,....

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 01-24-2019 at 05:56 PM.. Reason: Added CODE tags.
# 2  
Old 01-24-2019
Welcome to the forum.


Any attempts / ideas / thoughts from your side?
# 3  
Old 01-24-2019
OK, calling it a day, now. Before leaving - how about this:


Code:
$ awk '!((NR-1)%4) {TMP = $0; next} {print $0 TMP}'  file
"BPM neg.:",68.83"","SEQ ID No.  1 Subject # 20"
"BPM normal:",68.48"","SEQ ID No.  1 Subject # 20"
"SEQ ID No.  1 Subject # 20",79.33"","SEQ ID No.  1 Subject # 20"
"BPM neg.:",68.05"","SEQ ID No.  10 Subject # 20"
"BPM normal:",68.58"","SEQ ID No.  10 Subject # 20"
"SEQ ID No.  10 Subject # 20",81.63"","SEQ ID No.  10 Subject # 20"
"BPM neg.:",71.06"","SEQ ID No.  17 Subject # 20"
"BPM normal:",71.19"","SEQ ID No.  17 Subject # 20"
"SEQ ID No.  17 Subject # 20",81.99"","SEQ ID No.  17 Subject # 20"

These 3 Users Gave Thanks to RudiC For This Post:
# 4  
Old 01-25-2019
Many thanks... and yes I tried a little sed, since I cannot seem to get the hang of awk...I will look for a tutorial though, sinc eI know that it is rude to just demand an answer without making an effort...
# 5  
Old 01-25-2019
Here is in sed
Code:
sed -n '1~4 {h;n};G;s/\n/ /p' file

These 3 Users Gave Thanks to nezabudka For This Post:
# 6  
Old 01-25-2019
I would prefer awk here.
Of course it is doable in sed.
The N command appends the next input line (separated by a \n character) to the input buffer.
Do that 3 times, then do a big substitution:
Code:
sed 'N;N;N;s#\([^\n]*\)\n\([^\n]*\)\(\n[^\n]*\)\(\n[^\n]*\)#\2\1\3\1\4\1#' file

The first \( \) captures line1 and is the \1 in the substitution string
The second \( \) captures line2 and is the \2 in the substitution string
The next two \( \) capture line3 and line4 including their preceding \n character (that prints as a newline), they are \3 and \4 in the substitution string.
This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 01-25-2019
The 1~4, every 4 lines, is specific to GNU sed. It is very useful here, because sed normally has no means of "counting" things.
If by this you recognize the line to save, then you can use the hold buffer, and append it in all the following lines.
If there is a special pattern, then it also works with a standard sed (and Unix sed). For example "", at the begining of the line:
Code:
sed '/^"",/ {h;d;};G;s/\n//' file

These 3 Users Gave Thanks to MadeInGermany For This Post:
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

Transpose columns to row

Gents Using the attached file and using this code. awk '{print substr($0,4,2)}' input.txt | sort -k1n | awk '{a++}END{for(i in a) print i,a}' | sort -k1 > output i got the this output. 00 739 01 807 02 840 03 735 04 782 05 850 06 754 07 295 08 388 09 670 10 669 11 762 (8 Replies)
Discussion started by: jiam912
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. UNIX for Dummies Questions & Answers

Select 2 columns and transpose row by row

Hi, I have a tab-delimited file as follows: 1 1 2 2 3 3 4 4 a a b b c c d d 5 5 6 6 7 7 8 8 e e f f g g h h 9 9 10 10 11 11 12 12 i i j j k k l l 13 13 14 14 15 15 16 16 m m n n o o p p The output I need is: 1 1 a a 5 5 e e 9 9 i i 13... (5 Replies)
Discussion started by: mvaishnav
5 Replies

8. UNIX for Dummies Questions & Answers

transponse row to coloumn output

dears i have row data as below: ======================================================================================== Session Count: 24580 Session Count: 13426 Session Count: 22533 Session Count: 0 Session Count: 0 Session Count: 15036 Session Count: 11625 Session Count: 6942... (3 Replies)
Discussion started by: thehero
3 Replies

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

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