Splitting & reformating a single file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting & reformating a single file
# 1  
Old 06-27-2011
Splitting & reformating a single file

I have a bif text file with the following format:
Code:
d1_03  fr:23
d1_03 fr:56
d1_03 fr:67
d1_03 fr:78
d1_01 fr:35
d1_01 fr:29
d1_01 fr:45
d2_09 fr:34
d2_09 fr:78
d3_98 fr:90
d3_98 fr:104
d3_98 fr:360

I have like thousands of such lines

I want to reformat this file based on column 1 such as:
Code:
d1_03 fr:23 fr:56 fr:67  fr:78
d1_01 fr:35 fr:29 fr:45
d2_09 fr:34 fr:78 
d3_98 fr:90 fr:104 fr:360

Please let me know the best way to do it using awk or sed
LA
# 2  
Old 06-27-2011
Code:
[mute@geek ~/test]$ awk '{ a[$1] = (a[$1] ? (a[$1] FS) : "") $2 } END { for (i in a) print i " " a[i] }' bif

d3_98 fr:90 fr:104 fr:360
d2_09 fr:34 fr:78
d1_01 fr:35 fr:29 fr:45
d1_03 fr:23 fr:56 fr:67 fr:78

Last edited by Neo; 06-27-2011 at 03:01 PM.. Reason: code tag
# 3  
Old 06-27-2011
Hi,

Using 'perl':
Code:
$ cat script.pl
use strict;
use warnings;

my ($old_d, %h);

while ( <> ) {
        chomp;
        my ($d, $fr) = split;
        if ( $. > 1 && !exists $h{ $d } ) {
                print qq($old_d ), join( " ", @{$h{ $old_d }} ), qq(\n);
                undef %h;
                undef $old_d;
        }
        push @{$h{ $d }}, $fr;
        $old_d ||= $d;

        print qq($old_d ), join( " ", @{$h{ $old_d }} ), qq(\n) if eof;

}
$ perl script.pl infile
d1_03 fr:23 fr:56 fr:67 fr:78
d1_01 fr:35 fr:29 fr:45
d2_09 fr:34 fr:78
d3_98 fr:90 fr:104 fr:360

Regards,
Birei
# 4  
Old 06-28-2011
Alternate awk..
Code:
awk 'NR==FNR{a[$1]=$2" "a[$1];next} END{for(b in a)print b,a[b]} inputfile inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting a single xml file into multiple xml files

Hi, I'm having a xml file with multiple xml header. so i want to split the file into multiple files. Sample.xml consists multiple headers so how can we split these multiple headers into multiple files in unix. eg : <?xml version="1.0" encoding="UTF-8"?> <ml:individual... (3 Replies)
Discussion started by: Narendra921631
3 Replies

2. Shell Programming and Scripting

Execution of loop :Splitting a single file into multiple .dat file

hdr=$(cut -c1 $path$file|head -1)#extract header”H” trl=$(cut -c|path$file|tail -1)#extract trailer “T” SplitFile=$(cut -c 50-250 $path 1$newfile |sed'$/ *$//' head -1')# to trim white space and extract table name If; then # start loop if it is a header While read I #read file Do... (4 Replies)
Discussion started by: SwagatikaP1
4 Replies

3. Shell Programming and Scripting

Splitting a single file to multiple files

Hi Friends , Please guide me with the code to extract multiple files from one file . The File Looks like ( Suppose a file has 2 tables list ,column length may vary ) H..- > File Header.... H....- >Table 1 Header.... D....- > Table 1 Data.... T....- >Table 1 Trailer.... H..-> Table 2... (1 Reply)
Discussion started by: AspiringD
1 Replies

4. Shell Programming and Scripting

Help me pls : splitting single file in unix into different files based on data

I have a file in unix with sample data as follows : -------------------------------------------------------------- -------------------------------------------------------------- {30001002|XXparameter|Layout|$ I want this file to be splitted into different files and corresponding to the sample... (54 Replies)
Discussion started by: Ravindra Swan
54 Replies

5. Shell Programming and Scripting

Need help splitting huge single record file

I was given a data file that I need to split into multiple lines/records based on a key word. The problem is that it is 2.5GB or bigger and everything I try in perl or sed causes a Segmentation fault. Can someone give me some other ideas. The data is of the form:... (5 Replies)
Discussion started by: leolson
5 Replies

6. Shell Programming and Scripting

Splitting single file into n files

Hi all, I am new to scripting and I have a requirement we have source file as HEADER 01.10.2010 14:32:37 NAYA TA0022 TA0000 20000001;20060612;99991231;K4;02;3 20000008;20080624;99991231;K4;02;3 20000026;19840724;99991231;KK;01;3 20000027;19840724;99991231;KK;01;3... (6 Replies)
Discussion started by: srk409
6 Replies

7. UNIX for Advanced & Expert Users

Splitting the single csv file

Hi, I have a requiement where in i will get a single file but there will be mutiple headers. Suppose say for eg: Header1 Data... Data... Header2 Data.. Data.. Header3 Data.. Data.. I want to split each with the corresponding data into a single file. Please let me know how... (1 Reply)
Discussion started by: weknowd
1 Replies

8. Shell Programming and Scripting

Reformating ascii file with awk

Hello, I've a lot of ascii files that I would like to reformat : One of files's column (for exemple $5) contains increasing numbers (see exemple) : $5= 1 1 1 1 1 2 2 2 2 3 3 (2 Replies)
Discussion started by: Caribou
2 Replies

9. UNIX for Advanced & Expert Users

Selectively Reformating a file using AWK

Dear users, I am new to AWK and have been battling with this one for close to a week now. Some of you did offer some help last week but I think I may not have explained myself very well. So I am trying again. I have a dataset that has the following format where the datasets repeat every... (5 Replies)
Discussion started by: sda_rr
5 Replies

10. Shell Programming and Scripting

Help on awk.. reformating a file

Hello, I am having a trouble with awk attempting to reformat a two columns file , such as below: 201 84 201 370 201 544 201 600 213 99 213 250 213 431 220 65 220 129 220 338 220 408 220 501 220 550 231 101 231 350 What I need to do is is to add a third column containing a... (4 Replies)
Discussion started by: Martian
4 Replies
Login or Register to Ask a Question