Data formation | Unix Linux Forums | UNIX for Advanced & Expert Users

  Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Data formation

UNIX for Advanced & Expert Users


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-04-2013
srikanth38 srikanth38 is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 8 September 2014, 11:11 AM EDT
Posts: 16
Thanks: 7
Thanked 3 Times in 3 Posts
Data formation

I have a data like as follows, I need to format it as shown in as below. Request you to help me here ?

I/P

Code:
aa|3|1
aa|4|2
bb|3|1
bb|4|1
cc|3|26
cc|4|1

O/P


Code:
aa|3|1|4|2
bb|3|1|4|1
cc|3|26|4|1

Thanks,
Srikanth

Last edited by Scott; 01-04-2013 at 06:50 AM.. Reason: Please use code tags, and less formatting
Sponsored Links
    #2  
Old 01-04-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 October 2014, 5:10 AM EDT
Location: San Jose, CA, USA
Posts: 4,898
Thanks: 182
Thanked 1,646 Times in 1,397 Posts
Please clarify:
  1. Do you want sets of two adjacent lines with the same value in the 1st field combined into a single line?
  2. Do you want sets of two adjacent lines to be combined no matter what is in the 1st field?
  3. Do you want all lines in your input with the same first field combined into a single line (even if they are not adjacent)?
  4. Are there always 3 fields in input lines, or do you want all fields combined no matter how many fields there are?
The Following User Says Thank You to Don Cragun For This Useful Post:
rbatte1 (01-04-2013)
Sponsored Links
    #3  
Old 01-07-2013
srikanth38 srikanth38 is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 8 September 2014, 11:11 AM EDT
Posts: 16
Thanks: 7
Thanked 3 Times in 3 Posts
Please find my answers.

1. Do you want sets of two adjacent lines with the same value in the 1st field combined into a single line? Yes
2. Do you want sets of two adjacent lines to be combined no matter what is in the 1st field? No
3. Do you want all lines in your input with the same first field combined into a single line (even if they are not adjacent)? No
4. Are there always 3 fields in input lines, or do you want all fields combined no matter how many fields there are? Yes
    #4  
Old 01-07-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 October 2014, 5:10 AM EDT
Location: San Jose, CA, USA
Posts: 4,898
Thanks: 182
Thanked 1,646 Times in 1,397 Posts
Quote:
4. Are there always 3 fields in input lines, or do you want all fields combined no matter how many fields there are? Yes
The response Yes doesn't say which of the two specified behaviors you want implemented. The following should work either way but is more complex than is needed if there are always 3 input fields per line.


Code:
awk 'BEGIN {FS = OFS = "|"}
NR % 2 {o = $0          # This is the 1st line in a pair of lines.
        key = $1
        next    
}                       
{       if(key != $1) { # This is the 2nd line in a pair of lines.
            printf("1st field on line %d (%s) != 1st field on line %d (%s)\n",
                NR - 1, key, NR, $1) 
            exit 1  
        }       
        for(i = 2; i <= NF; i++) o = o OFS $i
        print o 
}' input_file

NOTE: On Solaris systems, use /usr/xpg4/bin/awk or nawk instead of awk.

Last edited by Don Cragun; 01-07-2013 at 11:02 AM.. Reason: Removed line of code that had been replaced earlier
Sponsored Links
    #5  
Old 01-07-2013
Fundix Fundix is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 16 January 2014, 5:23 AM EST
Posts: 71
Thanks: 16
Thanked 11 Times in 11 Posts
A Perl script (maybe a little bit too long) but doing the job :

Input file :

Code:
aa|3|1
aa|4|2
bb|3|1
bb|4|1
cc|3|26
cc|4|1
aa|8|5
aa|7|9

Script :

Code:
#!/usr/bin/perl -w
use strict;

my $cur_dir = $ENV{PWD};
my $filename = $cur_dir."/file008";
my ($record,@fields,%hash,$key,$flg);

open(FILE,"<$filename") or die"open: $!";

while( defined( $record = <FILE> ) ) {
  chomp $record;
  @fields=split(/\|/,$record);

  # New key and not the 1st one ?
  if( (defined($key)) && ($key ne $fields[0]) ) {
    $flg=1;
  } else {
    $flg=0;
  }

  # Processing new key or same key
  if( $flg == 1) {
    # new key printing previous one and processing new one
    print "$key$hash{$key}\n";
    delete( $hash{$key} );
    $key=$fields[0];
    $hash{$key}="\|$fields[1]\|$fields[2]";
  } else {
    # processing existing key
    $key=$fields[0];
    $hash{$key}.="\|$fields[1]\|$fields[2]";
  }
}

# Printing last key if needed
if( $flg == 0) {
  print "$key$hash{$key}\n";
}

close(FILE);

Output :

Code:
aa|3|1|4|2
bb|3|1|4|1
cc|3|26|4|1
aa|8|5|7|9

Sponsored Links
    #6  
Old 01-07-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 October 2014, 11:36 AM EDT
Location: Saskatchewan
Posts: 19,682
Thanks: 823
Thanked 3,352 Times in 3,139 Posts

Code:
awk -F"|" '{ T=$1 ; sub(/^[^|]+[|]/, ""); A[$1]=A[$1]"|"$0 } END { for(X in A) print substr(A[X], 2); }' inputfile

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl string formation from array ptappeta Shell Programming and Scripting 1 12-07-2012 10:13 AM
Formation of sql files Naga06 Shell Programming and Scripting 5 12-08-2011 05:50 PM
Extract data based on match against one column data from a long list data patrick87 Shell Programming and Scripting 12 11-17-2009 03:27 AM
date command op formation jaydeep_sadaria Shell Programming and Scripting 1 01-01-2008 07:17 AM
Howto capture data from rs232port andpull data into oracle database-9i automatically boss UNIX for Dummies Questions & Answers 1 09-23-2007 02:35 AM



All times are GMT -4. The time now is 12:08 PM.