group by in files :-)


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers group by in files :-)
# 1  
Old 06-21-2005
group by in files :-)

My file looks like:

X^~^12^d
X^~^19^d
Y^~^5^d
Y^~^10^d
Y^~^11^d
Z^~^14^d

I need a script that will "group by" the first field and Concatenate the values.

My output should be:

X^~^12,19^d
Y^~^5,10,11^d
Z^~^14~d

Does anyone have any idea ?
# 2  
Old 06-21-2005
# 3  
Old 06-21-2005
thank for the example but it doesn't handle

where the delimiter is ^ and if the concatenated field is not last.

Any idea ?
# 4  
Old 06-21-2005
something to start with:

nawk -f hell.awk file.txt

hell.awk:
Code:
BEGIN { FS=OFS="^" }
{
    arr[$1] = ($1 in arr) ? arr[$1] "," $3 : $3
}
END {
  for (i in arr)
    print i OFS "~", arr[i]
}

# 5  
Old 06-21-2005
Thanks!!! Can I Marry You ? :-)

you are the best
# 6  
Old 06-21-2005
Quote:
Originally Posted by hellsd
you are the best
take a number....
glad to be of help.
# 7  
Old 06-21-2005
Now it is more complicate:

Lets say the input file is:
ABC^1^~^data^x
ABC^2^~^data^x
ABC^3^~^data^x
DEF^4^~^data^y
DEF^4^~^data^z
DEF^5^~^data^y
DEF^5^~^data^z
DEF^6^~^data^z
DEF^6^~^data^y
XYZ^7^~^data^t

Now I would like to group couple of fields(field 2 and field 5)
My output should be:

ABC^1,2,3^~^data^x
DEF^4,5,6^~^data^y,z
XYZ^7^~^data^t

Notice that I would like to print all the line's fields.

Is it possible?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace Stub Values In One Group Of Files With Actual Values From Another Group Of Files

I have two directories of files (new-config-files and old-config-files): new-config-files/this-db/config.inc.php new-config-files/that-db/config.inc.php new-config-files/old-db/config.inc.php new-config-files/new-db/config.inc.php new-config-files/random-database/config.inc.php etc. ... (4 Replies)
Discussion started by: spacegoose
4 Replies

2. AIX

Files without owner and group

Dears it is normal that the below binaries stay without any owner and group I have checked it in many servers and the like the below /usr/lpp/bos.net/inst_root/etc/ipsec# ls -lrt total 248 -r-xr-xr-x 1 987 987 13589 Jun 29 2005 default_group -r-xr-xr-x ... (5 Replies)
Discussion started by: thecobra151
5 Replies

3. UNIX for Dummies Questions & Answers

How to Tar group of Files on Sun OS

How to Tar group of Files on Sun OS? (2 Replies)
Discussion started by: Siva Sankar
2 Replies

4. UNIX for Dummies Questions & Answers

Space utilization for group of files

Hi Is there an easy was to list a group of file (*.txt) and report how much disk space they are using in total? Cheers (2 Replies)
Discussion started by: Grueben
2 Replies

5. Shell Programming and Scripting

Group files and zip

Hi I have 4 files in a folder and I am supposed to group and zip them via a mapping file as such: Group. Filename 1. A.txt 1. B.txt 2. C.txt 2. D.txt Result should be 2 zip files - 1.zip and 2.zip created with the contents being the text file. How can... (7 Replies)
Discussion started by: nightrider
7 Replies

6. Shell Programming and Scripting

How to group matched patterns in different files

Hi, I have a master file that i need to split into multiple files based on matched patterns. sample of my data as follows:- scaff_1 a e 123 130 c_scaff_100 scaff_1 a e 132 138 c_scaff_101 scaff_1 a e 140 150 ... (2 Replies)
Discussion started by: redse171
2 Replies

7. Shell Programming and Scripting

Find all files with group read OR group write OR user write permission

I need to find all the files that have group Read or Write permission or files that have user write permission. This is what I have so far: find . -exec ls -l {} \; | awk '/-...rw..w./ {print $1 " " $3 " " $4 " " $9}' It shows me all files where group read = true, group write = true... (5 Replies)
Discussion started by: shunter63
5 Replies

8. Shell Programming and Scripting

need help in remove group of files

i have some 350 files in a dir: i want to remove them in one shot, ls -ltr | grep 'Sep 15' | head -350 the above command gives me those 350 files i need to remove them,how to implement remove logic here in this command? i can get those 350 files using the above command only and therefore... (6 Replies)
Discussion started by: ali560045
6 Replies

9. Shell Programming and Scripting

uncompress a group of files

i have some 100's of files in the format .tar.gz. how to uncompress them in a single shot i have sorted the files according to current date and now they reside in a dir called naveed1. cd naveed1 ls -ltr file1.tar.gz file2.tar.gz : : : file100.tar.gz how to uncompresse them in... (8 Replies)
Discussion started by: ali560045
8 Replies

10. UNIX for Advanced & Expert Users

listing sequential files as one group...

Hi, I posted this over at Macnn and was redirected here... I'm not a unix programmer at all, but I have some backup if needed. Thanks in advance for any input. Is there a command for the osX terminal that will list sequentially numbered groups of file as one line instead of individually,... (1 Reply)
Discussion started by: kentm
1 Replies
Login or Register to Ask a Question