Converting Multiline Files to Flat Files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting Multiline Files to Flat Files?
# 1  
Old 04-13-2016
Converting Multiline Files to Flat Files?

How to convert this:
F1-R1 F1-R2 F1-R3 into a flat file for bash?? Each record
Code:
F2-R1 F2-R2 F2-R3
F3-R1 F3-R2 F3-R3
F4-R1 F4-R2 F4-R3

is on one line with all fields for that record, put into an output file. The output file should look like this when converted:
Code:
F1-R1,F2-R1,F3-R1,F4-R1
F1-R2,F2-R2,F3-R2,F4-R2
F1-R3,F2-R3,F3-R3,F4-R3

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

Last edited by RudiC; 04-13-2016 at 12:24 PM.. Reason: Tried my best to add (i)code tags without destroying the post.
# 2  
Old 04-13-2016
An awk solution:-
Code:
awk '
        {
                for ( i = 1; i <= NF; i++ )
                        A[NR FS i] = $i
        }
        END {
                for ( i = 1; i <= NF; i++ )
                {
                        for ( j = 1; j <= NR; j++ )
                        {
                                if ( j == NR )
                                        printf "%s\n", A[j FS i]
                                else
                                        printf "%s,", A[j FS i]
                        }
                }
        }
' OFS=, file

# 3  
Old 04-13-2016
Hello bud1738,

Could you please try following and let me know if this helps.
Code:
xargs -n1 < Input_file | pr -4 -t

Output will be as follows.
Code:
F1-R1             F2-R1             F3-R1             F4-R1
F1-R2             F2-R2             F3-R2             F4-R2
F1-R3             F2-R3             F3-R3             F4-R3

Thanks,
R. Singh
# 4  
Old 04-13-2016
try also:
Code:
xargs -n1 < infile | sort -t"-" -k2 | paste -d, - - - -


Last edited by rdrtx1; 04-13-2016 at 12:59 PM.. Reason: forgot an important step
# 5  
Old 04-13-2016
Quote:
Originally Posted by rdrtx1
try also:
Code:
xargs -n1 < infile | paste -d, - - - -

Hello rdrtx1,

I think above code doesn't meet OP's output, because OP wants to transpose the columns to rows. Other had above code will only change number of columns to 4.

Thanks,
R. Singh
# 6  
Old 04-13-2016
Yes that works! What would I do though if say something kept reoccurring, for example

Code:
a1 a2 a3
b1 b2 b3
c1 c2 c3
d1 d2 d3
a1 a2 a3
b1 b2 b3
c1 c2 c3
d1 d2 d3
etc...

and convert it into

Code:
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a1 b1 c1 d1
a2 b2 c2 d2
etc....


Last edited by Scrutinizer; 04-13-2016 at 04:45 PM.. Reason: code tags
# 7  
Old 04-13-2016
Hello bud1738,

Please use code tags as per forum rules for commands/codes/Inputs which you are using into your posts, could you please try following and let me know
if this could help(I haven't tested though).
Code:
awk '{!A[$1];!B[$2];!C[$3];}END{for(i in A){Q=Q?Q OFS i:i};for(j in B){U=U?U OFS j:j};for(k in C){V=V?V OFS k:k};print Q ORS U ORS V}'  Input_file

Output will be as follows.
Code:
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3

Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with converting XML to Flat file

Hi Friends, I want to convert a XML file to flat file. Sample I/p: <?xml version='1.0' encoding='UTF-8' ?> <DataFile xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' contactCount='4999' date='2012-04-14' time='22:00:14' xsi:noNamespaceSchemaLocation='gen .xsd'> <Contact... (3 Replies)
Discussion started by: karumudi7
3 Replies

2. UNIX for Advanced & Expert Users

Converting the date format in a flat file

Hi All, I am new to this forum, could any one help me out in resolving the below issue. Input of the flat file contains several lines of text for example find below: 5022090,2,4,7154,88,,,,,4/1/2011 0:00,Z,L,2 5022090,3,1,6648,88,,,,,4/1/2011 0:00,Z,,1... (0 Replies)
Discussion started by: av_sagar
0 Replies

3. Programming

Scripting using flat files

I need some help to create a script that can do the following: I have two flat files with one column that should link the two files' information into one record: 1st Flat File - 3 columns, multiple rows: orderid, Jobnumber, Ordernumber 2nd Flat File - 2 columns, multiple rows: ... (4 Replies)
Discussion started by: Lavelle
4 Replies

4. Shell Programming and Scripting

Converting a flat file in XML

Hello Friends, I am new to UNIX shell scripting. Using bash....Could you please help me in converting a flat file into an XML style output file. Flat file: (Input File entries looks like this) John Miller: 617-569-7996:15 Bunting lane, staten Island, NY: 10/21/79: 60600 The... (4 Replies)
Discussion started by: humkhn
4 Replies

5. Shell Programming and Scripting

Compare 2 flat files

Hi Frnds, I have a flat file with millions of records. . Now I on this. (I prefer for AWK as its gives good performance.) Old_file.txt ------------------ 1 gopi ase .... 2 arun pl ... 3 jack sutha .. 4 peter pm .. ... New_file.txt --------------- 4 peter pm .. .. ... (12 Replies)
Discussion started by: Gopal_Engg
12 Replies

6. Shell Programming and Scripting

Converting Column to Rows in a Flat file

Hi, Request To guide me in writing a shell program for the following requirement: Example:if the Input File contains the follwing data Input File Data: 80723240029,12,323,443,88,98,7,98,67,87 80723240030,12,56,6,,,3,12,56,6,7,2,3,12,56,6,7,2,3,88,98,7,98,67,87... (5 Replies)
Discussion started by: srinikal
5 Replies

7. Shell Programming and Scripting

Awk Compare Files w/Multiline Records

I'm trying to compare the first column values in two different files that use a numerical value as the key and output the more meaningful value found in the second column of file1 in front of the matching line(s) in file2. My problem is that file2 has multiple records. For example given: FILE1... (4 Replies)
Discussion started by: RacerX
4 Replies

8. Shell Programming and Scripting

Compare 2 flat files

Hi Gurus, I searched the forum but didnt get much info. I want to compare 2 files. 1)Newfile comes today with 2)Old file of previous day. The files are same ,just the new files might have new records sometimes. So I want to capture these new records in another file. Can anyone help... (5 Replies)
Discussion started by: ganesh123
5 Replies

9. Shell Programming and Scripting

Help with Flat Files Please!! BASH (New User)

Hello All, I am brand new to the UNIX world and so far and very intrigued and enjoy scripting. This is just a new language for me. I would really like assistance with the below request. Any help would be greatly appreciated! I want to create a flat file in Vi that has a header field and... (0 Replies)
Discussion started by: cyberjax21
0 Replies

10. Shell Programming and Scripting

Flat Files

I have a flat file like this 0001 THER ULT HEAD & NECK VES 0002 THER ULTRASOUND OF HEART 0003 THER ULT PERIPHERAL VES 0009 OTHER THERAPEUTIC ULTSND 0010 IMPLANT CHEMOTHERA AGENT 0011 INFUS DROTRECOGIN ALFA 0012 ADM INHAL NITRIC OXIDE I need to conver this to a comma delimited flat file... (2 Replies)
Discussion started by: thumsup9
2 Replies
Login or Register to Ask a Question