Merging several line in one based on an occurrence


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merging several line in one based on an occurrence
# 1  
Old 01-30-2012
Merging several line in one based on an occurrence

Hi,

I try to gather several line in one based on the first column.

For exemple :

Code:
toto;1;2;3
toto;4;5;6
toto;7;8;9
etc...   (Number of lines not predefined)
ruru;a;b;c
ruru;d;e;f
ruru;g;h;i
...

I'm trying to get :

Code:
toto;1;2;3;4;5;6;7;8;9...
ruru;a;b;c;d;e;f;g;h;i...

Thank you.
# 2  
Old 01-30-2012
Quote:
Originally Posted by xanthos
...
Code:
toto;1;2;3
toto;4;5;6
toto;7;8;9
etc...   (Number of lines not predefined)
ruru;a;b;c
ruru;d;e;f
ruru;g;h;i
...

I'm trying to get :
Code:
toto;1;2;3;4;5;6;7;8;9...
ruru;a;b;c;d;e;f;g;h;i...

...
Code:
$
$ cat -n f52
  1  toto;1;2;3
  2  toto;4;5;6
  3  toto;7;8;9
  4  ruru;a;b;c
  5  ruru;d;e;f
  6  ruru;g;h;i
  7  ruru;j;k;l
$
$ perl -F';' -lane 'if ($F[0] eq $p){$s .= ";".join(";",@F[1..3])} else {print $s if $.>1; $s=$_} $p=$F[0]; END {print $s}' f52
toto;1;2;3;4;5;6;7;8;9
ruru;a;b;c;d;e;f;g;h;i;j;k;l
$
$ # alternatively,
$ perl -lne '/^(\w+)(;.*)$/; if ($1 eq $p){$s .= $2} else {print $s if $.>1; $s=$_} $p=$1; END {print $s}' f52
toto;1;2;3;4;5;6;7;8;9
ruru;a;b;c;d;e;f;g;h;i;j;k;l
$
$

tyler_durden

Last edited by durden_tyler; 01-30-2012 at 10:55 AM..
This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 01-30-2012
Thank you for the 2 solutions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merging rows based on same ID in First column.

Hellow, I have a tab-delimited file with 3 columns : BINPACKER.13259.1.p2 SSF48239 BINPACKER.13259.1.p2 PF13243 BINPACKER.13259.1.p2 G3DSA:1.50.10.20 BINPACKER.13259.2.p2 SSF48239 BINPACKER.13259.2.p2 PF13243 BINPACKER.13259.2.p2 G3DSA:1.50.10.20... (7 Replies)
Discussion started by: anjaliANJALI
7 Replies

2. UNIX for Beginners Questions & Answers

Merging multiple lines into single line based on one column

I Want to merge multiple lines based on the 1st field and keep into single record. SRC File: AAA_POC_DB.TAB1 AAA_POC_DB.TAB2 AAA_POC_DB.TAB3 AAA_POC_DB.TAB4 BBB_POC_DB.TAB1 BBB_POC_DB.TAB2 CCC_POC_DB.TAB6 OUTPUT ----------------- 'AAA_POC_DB','TAB1','TAB2','TAB3','TAB4'... (10 Replies)
Discussion started by: raju2016
10 Replies

3. UNIX for Dummies Questions & Answers

Count occurrence of string (based on type) in a column using awk

Hello, I have a table that looks like what is shown below: AA BB CC XY PQ RS AA BB CC XY RS I would like the total counts depending on the set they belong to: if search pattern is in {AA, BB, CC} --> count them as Type1 | wc -l (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

4. Shell Programming and Scripting

Merging 2 lines based on a string

Dear Unix gurus I need help with a command or script to merge 2 lines where ever we find the string. I have attached scanned document. First line has string value: VSIN, immediate line has value: SETTLEMENT Where it finds the 2 string values in the whole file, one below the other,... (8 Replies)
Discussion started by: Karunyam
8 Replies

5. UNIX for Dummies Questions & Answers

Merging lines based on one column

Hi, I have a file which I'd like to merge lines based on duplicates in one column while keeping the info for other columns. Let me simplify it by an example: File ESR1 ANASTROZOLE NA FDA_approved ESR1 CISPLATIN NA FDA_approved ESR1 DANAZOL agonist NA ESR1 EXEMESTANE NA FDA_approved... (3 Replies)
Discussion started by: JJ001
3 Replies

6. Shell Programming and Scripting

Merging two files with merging line by line

Hi, I have two files and i want to merge it like, file1.txt --------- abc cde efg file2.txt ------- 111 222 333 Output file should be, -------------- abc 111 (2 Replies)
Discussion started by: rbalaj16
2 Replies

7. Shell Programming and Scripting

merging two files based on first column

I had two files file1 and file2. I want a o/p file(file3) like below using first column as ref. Pls give suggestion ass join is not working as the number of lines in each file is nealry 5 C? file1 --------------------- 404000324810001 Y 404000324810004 N 404000324810008 Y 404000324810009 N... (1 Reply)
Discussion started by: p_sai_ias
1 Replies

8. Shell Programming and Scripting

Count no of occurrence of the strings based on column value

Can anyone help me to count number of occurrence of the strings based on column value. Say i have 300 files with 1000 record length from which i need to count the number of occurrence string which is existing from 213 to 219. Some may be unique and some may be repeated. (8 Replies)
Discussion started by: zooby
8 Replies

9. Shell Programming and Scripting

Merging of all files based on a condition

Hi Friends, I am new to UNIX. I need to merge all the files(to FINAL.txt) in single directory based one condition. Out of all the files one of file will have specific value like :GF01: at any where in the file. so the file which is having :GF01: should be appended at the last. EX:... (5 Replies)
Discussion started by: arund_01
5 Replies

10. Shell Programming and Scripting

merging two files based on some key

I have to merge two files: The files are having the same format like A0this is first line TOlast line silmilarly other lines. I have to search for A0 line in the second file also and then put the data in the third file under A0 heading ,then for A1 and so on. A0 portion will be treminated... (1 Reply)
Discussion started by: Vandana Yadav
1 Replies
Login or Register to Ask a Question