How to transpose pieces of data in a column to multiple rows?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to transpose pieces of data in a column to multiple rows?
# 1  
Old 10-09-2019
How to transpose pieces of data in a column to multiple rows?

Hello Everyone,
I am very new to the world of regular expressions. I am trying to use grep/sed for the following:

Input file is something like this and there are multiple such files:
Code:
abc
1
2
3
4
5
***END***
abc
6
7
8
9
***END***
abc
10
11
12
***END***

I need to be able to edit the text in all of these files so that all the files look like this:
Code:
abc
1,2,3,4,5
***END***
abc
6,7,8,9
***END***
abc
10,11,12
***END***

Kindly help me. Your help is much appreciated!
Moderator's Comments:
Mod Comment Kindly do wrap your samples into CODE TAGS, as per forum rules.

Last edited by RavinderSingh13; 10-09-2019 at 11:31 PM..
# 2  
Old 10-09-2019
Hello shellnewuser,

Could you please try following.

Code:
awk 'BEGIN{OFS=","} /\*\*\*END\*\*\*/{if(val){print val};print;val="";next} /^abc/{print;next} {val=(val?val OFS:"")$0}'  Input_file

Thanks,
R. Singh
# 3  
Old 10-10-2019
RE: How to transpose pieces of data in a column to multiple rows?

Thank you very much RavinderSingh13 for such a prompt response. The code did work but I have following issues:
1. The code did throw the expected output on the terminal but what I need is that the Input file itself is modified to have the data between "abc" and "***END***" transposed in rows as shown on the terminal.
2. Also, in some files the number of "*" around the word "END" varies, is there a way to generalize the number of "*" so I don't have to worry about it?
3. There is some additional data before "abc" which does not need to be transposed, how do I keep that data as it is along with transposing the data between "abc" and "***END***" like you did.

If its not too much to ask, could you kindly mention what each piece of your code does so I can learn as well.

Once again thank you for help!

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux - Transpose rows into column

hello, I have a server that collect some performance statistics of 4 servers in the following input file : $ cat inputfile Time,A,Server1,KPI1,data1 Time,A,Server1,KPI2,data2 Time,A,Server1,KPI3,data3 Time,A,Server1,KPI4,data4 Time,A,Server1,KPI5,data5 Time,A,Server2,KPI1,data6... (9 Replies)
Discussion started by: capitain25
9 Replies

2. Programming

To transpose rows to column in hadoop

Hi, i am having an HDFS file which is comma seperated, i need to transpose from rows to column only the header columns text.csv cnt,name,place 1,hi,nz 2,hello,aus I need cnt, name, place while using below command in hadoop getting the error hadoop fs -fmt -1 text.csv (0 Replies)
Discussion started by: rohit_shinez
0 Replies

3. Shell Programming and Scripting

Peel syntax for transpose rows into column

Dear all, Plz let me know syntax for transposing rows into column in perl, I am having 30 csv files which are merged into a single xls sheet. but i want to transpose each row into column in excel sheet in each tab (1 CSV = 1tab in xls sheet) example is as below ... (0 Replies)
Discussion started by: sagar_1986
0 Replies

4. Shell Programming and Scripting

Transpose data as rows using awk

Hi I have below requirement, need help One file contains the meta data information and other file would have the data, match the column from file1 and with file2 and extract corresponding column value and display in another file File1: CUSTTYPECD COSTCENTER FNAME LNAME SERVICELVL ... (1 Reply)
Discussion started by: ravlapo
1 Replies

5. Shell Programming and Scripting

Transpose multiple rows (with a mix of space and enter) to a single column

How to change the uploaded weekly file data to the following format? New Well_Id,Old Well_Id,District,Thana,Date,Data,R.L,WellType,Lati.,Longi. BAG001,PT006,BARGUNA,AMTALI,1/2/1978,1.81,2.29,Piezometer,220825,901430 BAG001,PT006,BARGUNA,AMTALI,1/9/1978,1.87,2.29,Piezometer,220825,901430... (3 Replies)
Discussion started by: sara.nowreen
3 Replies

6. Shell Programming and Scripting

Transpose Column of Data to Rows

I can no longer find my commands, but I use to be able to transpose data with common fields from a single column to rows using a command line. My data is separated as follows: NAME=BOB ADDRESS=COLORADO PET=CAT NAME=SUSAN ADDRESS=TEXAS PET=BIRD NAME=TOM ADDRESS=UTAH PET=DOG I would... (7 Replies)
Discussion started by: docdave78
7 Replies

7. Shell Programming and Scripting

Transpose Datefield from rows to column + Print time diff

Hi Experts, Can you please help me in transposing Datefield from rows to column and calculate the time difference for each of the Jobids: Input File: 08/23/2012 12:36:09,JOB_5340 08/23/2012 12:36:14,JOB_5340 08/23/2012 12:36:22,JOB_5350 08/23/2012 12:36:26,JOB_5350 Required Output:... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

8. Shell Programming and Scripting

Transpose Data from Columns to rows

Hello. very new to shell scripting and would like to know if anyone could help me. I have data thats being pulled into a txt file and currently have to manually transpose the data which is taking a long time to do. here is what the data looks like. Server1 -- Date -- Other -- value... (7 Replies)
Discussion started by: Mikes88
7 Replies

9. Shell Programming and Scripting

awk transpose rows to column

Need to transpose in awk rows to column like this: input: A1,6,5,4 3,2,1, A2,8,7,9,10,11,12,13,14 A3,1,2,3,5,7,8,9 A4,9,4,8,1,5,3, output: A1,1 A1,2 A1,4 ... A2,7 A2,8 ... A3,1 A3,2 ... A4,1 A4,3 (5 Replies)
Discussion started by: sdf
5 Replies

10. Shell Programming and Scripting

Transpose columns to Rows : Big data

Hi, I did read a few posts on the subjects, tried out a few solutions, but did not solve my problem. https://www.unix.com/302121568-post11.html https://www.unix.com/shell-programming-scripting/137953-large-file-columns-into-rows-etc-4.html Please help. Problem very similar to the second link... (15 Replies)
Discussion started by: genehunter
15 Replies
Login or Register to Ask a Question