|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Please clarify:
|
| The Following User Says Thank You to Don Cragun For This Useful Post: | ||
rbatte1 (01-04-2013) | ||
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
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_fileNOTE: 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
|
|||
|
|||
|
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
|
|||
|
|||
|
Code:
awk -F"|" '{ T=$1 ; sub(/^[^|]+[|]/, ""); A[$1]=A[$1]"|"$0 } END { for(X in A) print substr(A[X], 2); }' inputfile |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|