merge same pattern of same column in one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merge same pattern of same column in one line
# 1  
Old 08-23-2012
merge same pattern of same column in one line

Hello,
I have some problem in the modified shell script. I would like to merge the same word in column 1 in one line.

Example :
Code:
A1   B1     2
A2   B1     4
A3   B1     7
A1   B2     1
A2   B2     10
A3   B2     8

Expected output :
Code:
A1    B1   B2   2   1
A2    B1   B2   4   10
A3    B1   B2   7    8


I tried to use the modified shell script like this, but no tab was showed.
Please suggest me. Thanks in advance.

Code:
perl -lane 'if($.==0){print;next}; $x{"$F[0]"} .= $F[1] .= $F[2]; END{for (sort keys %x){print "$_\t$x{$_}"}}' input


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-23-2012 at 04:06 AM.. Reason: code tags
# 2  
Old 08-23-2012
Quote:
Originally Posted by awil
... I would like to merge the same word in column 1 in one line.

Example :
A1 B1 2
A2 B1 4
A3 B1 7
A1 B2 1
A2 B2 10
A3 B2 8

Expected output :
A1 B1 B2 2 1
A2 B1 B2 4 10
A3 B1 B2 7 8

...
Code:
$
$
$ cat f7
A1 B1 2
A2 B1 4
A3 B1 7
A1 B2 1
A2 B2 10
A3 B2 8
$
$
$ perl -lane 'if (defined $x{$F[0]}) {@{$x{$F[0]}}[1,3] = @F[1,2] }
              else { @{$x{$F[0]}}[0,2] = @F[1,2] }
              END {
                for $k (sort keys %x) { printf ("%s %3s %3s %3d %3d\n", $k, @{$x{$k}}) }
              }' f7
A1  B1  B2   2   1
A2  B1  B2   4  10
A3  B1  B2   7   8
$
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 08-23-2012
Code:
awk '{a[$1];b[$1""2]=b[$1""2]" "$2;c[$1""3]=c[$1""3]" "$3} END{for(i in a){print i" "b[i""2]" "c[i""3];}}'  input_file


Last edited by msabhi; 08-23-2012 at 06:29 AM..
This User Gave Thanks to msabhi For This Post:
# 4  
Old 08-23-2012
Thank you very much. They are working Smilie
# 5  
Old 08-23-2012
Quote:
Originally Posted by msabhi
Code:
awk '{a[$1]=a[$1]" "$2" "$3;} END{for(i in a) printf("%s %s",i,a[i]"\n");}'  input_file

That will not produce the output expected by the OP.
# 6  
Old 08-23-2012
Thanks elixir..rectified in the same post.
This User Gave Thanks to msabhi For This Post:
# 7  
Old 08-23-2012
Code:
 awk 'BEGIN{i=1}{if(a[$1]){a[$1]=a[$1]FS$2;b[$1]=b[$1]FS$3}else{a[$1]=$2;b[$1]=$3;c[i]=$1;i++}}END{for(j=1;j<i;j++){print c[j],a[c[j]],b[c[j]]}}' file

This User Gave Thanks to raj_saini20 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

2. Shell Programming and Scripting

Delete line by pattern match in column

file: 1|12322|tow| 5|23422|pow| 6|23423|cow| 3|34324|how| deletelines: 12322 23423 My command to delete line while read NUM do awk -F"\|" '$2 !~ /`"$NUM"`/' file >file.back mv file.back file done<deletelines (3 Replies)
Discussion started by: Roozo
3 Replies

3. Shell Programming and Scripting

Perl removing line match with pattern in column

Hi, I have log like this: ... (1 Reply)
Discussion started by: justbow
1 Replies

4. Shell Programming and Scripting

Perl removing line match with pattern in column

Hi, I have log like this: ... (1 Reply)
Discussion started by: justbow
1 Replies

5. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

6. Shell Programming and Scripting

Merge two files line by line and column by column

Hi All, I have two files having oracle query result. I want to merge to files line by line and also with column File1 23577|SYNC TYPE 23578|Order Number|ConnectionState 23585|Service State|Service NameFile2 23577|AR Alarm Sync 23578|A5499|9 23585|7|test_nov7Result... (18 Replies)
Discussion started by: Harshal22
18 Replies

7. Shell Programming and Scripting

grep based on pattern in a line and print the column before that

$ cat file.log Message Number = : Sending message 10:50:16^|^reqhdr.dummyid^=^02^|^reqhdr.timezone^=^GMT+05:30^|^DUMMYREQUEST^=^BH||||||||||||||||||$BD|OL|C|V||DummyAcctNo|02||24/12/2011|ST_DDM|DDM||||||||reqUUID110612105016$BT||||||||||||||||||$] Length I have the above line in the... (4 Replies)
Discussion started by: kalidass
4 Replies

8. UNIX for Dummies Questions & Answers

loop? print max column in each line for 800 files and merge

Hello, I have 800 or so files with 3 columns each and >10000 lines each. For each file and each line I would like to print the maximum column number for each line. Then I would like to 'paste' each of these files together (column-wise) so that the file with expression in label '_1' is the... (6 Replies)
Discussion started by: peanuts48
6 Replies

9. Shell Programming and Scripting

merge columns into one line after a specific pattern

Hi all, im a linux newbie, plz help! I have a file - box -------- Fox-2 -------- UF29 zip42 -------- zf-CW SNF2_N Heli_Z -------- Fox -------- Kel_1 box (3 Replies)
Discussion started by: sam_2921
3 Replies

10. Shell Programming and Scripting

compare the column from 3 files and merge that line

I have 3 file, each of has got 80000 records. file1.txt ----------------------- ABC001;active;modify;accept; ABC002;notactive;modify;accept; ABC003;notactive;no-modify;accept; ABC004;active;modify;accept; ABC005;active;no-modify;accept; file2.txt ---------------------------... (8 Replies)
Discussion started by: ganesh_mak
8 Replies
Login or Register to Ask a Question