Join all the lines matching similar pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join all the lines matching similar pattern
# 1  
Old 12-19-2012
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

Code:
> cat sample.txt
sam 2012/11/23
sam 2012/12/5
sam 2012/12/5
jones 2012/10/16
jones 2012/10/17
jones 2012/10/17
mike 2012/11/24
mike 2012/12/25
mike 2012/12/25
mike 2012/12/25
mike 2012/12/25
mike 2012/12/25

I have tried the following command and however it is joining all the lines. Perhaps, i want to join the lines matching similar pattern.

Code:
> sed -e :a -e '$!N;s/\n'$line'/ '$line'/;ta' -e 'P;D' sample.txt
sam 2012/11/23 sam 2012/12/5 sam 2012/12/5 jones 2012/10/16 jones 2012/10/17 jones 2012/10/17 mike 2012/11/24 mike 2012/12/25 mike 2012/12/25 mike 2012/12/25 mike 2012/12/25 mike 2012/12/25

Thanks in Advance. SmilieSmilieSmilie
# 2  
Old 12-19-2012
try:
Code:
awk '{a[$1]=$0" "a[$1]}; END {for (i in a) print a[i]}' sample.txt

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 12-19-2012
Thanks a lot and it is working Great....
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. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

4. UNIX for Dummies Questions & Answers

Join lines on finding a pattern

I have a file with the following contents. DTP START START START DTP START DTP START DTP START I like to join the lines like this DTP START START START DTP START DTP START (2 Replies)
Discussion started by: nsuresh316
2 Replies

5. UNIX for Dummies Questions & Answers

Join the lines until next pattern match

Hi, I have a data file where data is splitted into multiple lines. And, each valid record starts with a patten date | <?xml and ends with pattern </dmm> e.g. 20120924|<?xml record 1 line1....record 1 line1....record 1 line1.... record 1 line2....record 1 line2....record 1 line2.... record 1... (3 Replies)
Discussion started by: Dipalik
3 Replies

6. Shell Programming and Scripting

Help in unix script to join similar lines of input

Hi, I have been thinking of how to script this but i have no clue at all.. Could someone please help me out or give me some idea on this? I would like to group those lines with the same first variable in each line, joining the 2nd variables with commas. Let's say i have the following input. ... (3 Replies)
Discussion started by: rei125
3 Replies

7. UNIX for Dummies Questions & Answers

sed, join lines that do not match pattern

Hello, Could someone help me with sed. I have searched for solution 5 days allready :wall:, but cant find. Unfortunately my "sed" knowledge not good enough to manage it. I have the text: 123, foo1, bar1, short text1, dat1e, stable_pattern 124, foo2, bar2, long text with few lines, date,... (4 Replies)
Discussion started by: petrasl
4 Replies

8. UNIX for Dummies Questions & Answers

Matching and reporting near-similar lines in a file

Hi, I have a file with the lines as below: C_10_A05_T7 C_10_A06_SP6 C_10_B05_SP6 C_10_B05_T7 C_10_B01_SP6 C_10_B01_T7 C_12_G07_SP6 C_12_G11_SP6 C_12_G11_T7 C_2_H18_T7 C_2_I02_SP6 C_2_I02_T7 C_2_I13_SP6 C_2_I17_SP6 The four segments of each line are connected by '_' symbols. I... (7 Replies)
Discussion started by: Fahmida
7 Replies

9. UNIX for Dummies Questions & Answers

merge lines within a file that start with a similar pattern

Hello! i have a text file.. which contains the data as follows i want to merge the declarations lines pertaining to one datatype in to a single line as follows i've searched the forum for help.. but couldn't find much help.. how can i do this?? (1 Reply)
Discussion started by: a_ba
1 Replies

10. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies
Login or Register to Ask a Question