merge same pattern lines together


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merge same pattern lines together
# 1  
Old 04-23-2010
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 (make it easier to drop into Excel).

I have a file that follow this pattern

Code:
AAA - 123 - 99999 - 20100418
BBB - 32243 - 9999999 - 20100418
CCC - 789 - 99 - 20100418
AAA - 123 - 99999 - 20100419 
BBB - 32243 - 9999999 - 20100419 
CCC - 789 - 99 - 20100419
AAA - 123 - 99999 - 20100420
BBB - 32243 - 9999999 - 20100420
CCC - 789 - 99 - 20100420

But to pritify it i would want an output like

Code:
,20100418, 20100419, 20100420
AAA,123,124,123
BBB,32243,32243,32243
CCC,789,789,789


Amongst the many clever people here are there anysuggestions on getting this done (preferably in a shell script). This would have many more days and categorys i.e. AAA -> ZZZ

Cheers and beers,
Neil
# 2  
Old 04-23-2010
Code:
cat file | tr -d " " | awk -F "-" '
{  id[$1]=sprintf("%s,%s",id[$1],$2)
   last[$4]=1
}
END {
        for (i in last) {
                printf ",%s",i
                }
        printf "\n"
        for (i in id) {
                printf "%s%s\n",i,id[i]
                }

}'

# 3  
Old 04-23-2010
Totally amazing....that is the wonder on the Internet (I wish i'd come up with that idea).


Thanks a load.....works a treat.


N
e
i
l
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. 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

3. 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

4. Shell Programming and Scripting

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 : 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 ... (6 Replies)
Discussion started by: awil
6 Replies

5. Shell Programming and Scripting

copy and merge texts between two pattern

I need 1. to find all the files from a Folder "folder_name" and from its 4 or 5 sub folders which contain a certain pattern "pattern1". 2. from these files copy and merge all the text between "pattern1" and another different pattern "pattern2" to "mergefile". 3. Get rid of every html tag. 4.... (3 Replies)
Discussion started by: mjomba
3 Replies

6. 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

7. Shell Programming and Scripting

remove blank lines and merge lines in shell

Hi, I'm not a expert in shell programming, so i've come here to take help from u gurus. I'm trying to tailor a csv file that i got to make it work for the LOAD FROM command. I've a datatable csv of the below format - --in file format xx,xx,xx ,xx , , , , ,,xx, xxxx,, ,, xxx,... (11 Replies)
Discussion started by: dvah
11 Replies

8. Shell Programming and Scripting

Merge lines if pattern matches in ksh

I have a file like this. Pls help me to solve this . (I should look for only Message : 111 and need to print the start time to end time Need to ignore other type of messages. Ex: if first message is 111 and second message is 000 or anything else then ignore the 2nd one and print start time of the... (1 Reply)
Discussion started by: mnjx
1 Replies

9. 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

10. 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
Login or Register to Ask a Question