merge lines within a file that start with a similar pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers merge lines within a file that start with a similar pattern
# 1  
Old 05-19-2011
merge lines within a file that start with a similar pattern

Hello! i have a text file.. which contains the data as follows
Quote:
int a,b,c
int d
float e,f,g
float h,i
float j
char m,p
char q,r
i want to merge the declarations lines pertaining to one datatype in to a single line as follows
Quote:
int a,b,c,d
float e,f,g,h,i,j
char m,p,q,r
i've searched the forum for help.. but couldn't find much help.. how can i do this??
# 2  
Old 05-19-2011
Code:
awk '{a[$1] ? a[$1]=a[$1] OFS $2 : a[$1]=$2} END{for(e in a){print e,a[e]}}' OFS=, infile
char,m,p,q,r
int,a,b,c,d
float,e,f,g,h,i,j

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Fetch lines between 1st and 4th similar pattern

Hi Folks, I have a big file that looks like below ========== kjhjl kjlklkkhcgflj fgf ========== xsww23edc ccdde3rfv ceerfcc vff4 ========== zaq12wsx xsw23edc ========== ========== (3 Replies)
Discussion started by: jayadanabalan
3 Replies

2. Shell Programming and Scripting

Bash/shell merge similar lines

Hello, I've been working on a bash script to parse through firewall logs (cisco). I'm nearing the end and have a dilemma. My data looks as such (actual data is several gigs worth of logs - without the headers): sourceIP destinationIP destinationProtocol destinationPort 1.1.1.1 2.2.2.2 ... (2 Replies)
Discussion started by: umang2382
2 Replies

3. Shell Programming and Scripting

sed Range Pattern and 2 lines before Start Pattern

Hi all, I have been searching all over Google but I am unable to find a solution for a particular result that I am trying to achieve. Consider the following input: 1 2 3 4 5 B4Srt1--Variable-0000 B4Srt2--Variable-1111 Srt 6 7 8 9 10 End (3 Replies)
Discussion started by: y2jacky
3 Replies

4. Shell Programming and Scripting

Need to merge lines based on pattern

Hi, I have a requirement to merge multiple lines based on search pattern. The search criteria is : it will search for CONSTRAINT and when it found CONSTRAINT, it will merge all lines to 1 line till it founds blank line. For Example: CREATE TABLE "AMS_DISTRIBUTOR_XREF" ( "SOURCE"... (5 Replies)
Discussion started by: satyaatcgi
5 Replies

5. Shell Programming and Scripting

Join all the lines matching similar pattern

I am trying to Join all the lines matching similar pattern. Example ; I wanted to join all the lines which has sam to a single line. In next line, i wanted to have all the lines with jones to a single line....etc > cat sample.txt sam 2012/11/23 sam 2012/12/5 sam 2012/12/5 jones... (2 Replies)
Discussion started by: evrurs
2 Replies

6. Shell Programming and Scripting

Help with merge two file based on similar column content

Input file 1: A1BG A1BG A1BG A1CF A1CF BCAS BCAS A2LD1 A2M A2M HAT . . Input file 2: A1BG All A1CF TEMP (5 Replies)
Discussion started by: perl_beginner
5 Replies

7. Shell Programming and Scripting

merge same pattern lines together

Hi people... I normally find with out any problem the solutions I need just by searching. But for this I'm not having any joy or jsut failing to adapt what I'ev found to work. I have applciation report that doesn't allow for manipulation at creation so I want to do some post modifcation... (2 Replies)
Discussion started by: nhatch
2 Replies

8. Shell Programming and Scripting

Merge lines from one file if pattern matches

I have one comma separated file (a.txt) with two or more records all matching except for the last column. I would like to merge all matching lines into one and consolidate the last column, separated by ":". Does anyone know of a way to do this easily? I've searched the forum but most talked... (6 Replies)
Discussion started by: giannicello
6 Replies

9. Shell Programming and Scripting

Merge lines in text file based on pattern

Hello, I have searched forum trying to find a solution to my problem, but could not find anything or I did not understand the examples.... I should say, I am very inexperienced with text processing. I have a text file with approx 60k lines in it. I need to merge lines based on the number... (8 Replies)
Discussion started by: Bertik
8 Replies

10. Shell Programming and Scripting

Urgent : Merge similar lines

Hi, I have a file like this. please notice that ./usr/orders1/order_new_2627 appears more than once, thus needs to be merged. I would like to merge the lines where the first column match so the output should be like this: Please help (2 Replies)
Discussion started by: rakeshou
2 Replies
Login or Register to Ask a Question