Not-so-simple sorting


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Not-so-simple sorting
# 1  
Old 01-26-2010
Data Not-so-simple sorting

Hi Everyone,

I've got 2 problems which I'm hoping your collective brains can help me solve....

problem 1

I have a sorting problem. I have a list of data where three lines must always be kept together (for any chemistry geeks out there, the lines are the coordinates of a molecule made of 3 atoms). I now need to sort this data so that all the C O O lines appear first, and then the Q, N, N.

Basically I need a command or script which will pick out and remove a line beginning with "Q" AND the following 2 lines and write these at the bottom of the remaining C O O data.

example of my data:
Code:
C     -6.6648962     -10.686781     -14.724705
O     -5.6031028     -10.16189     -14.96612
O     -7.7266896     -11.211673     -14.483289
C     2.2802657     -13.111199     -10.25135
O     1.8669079     -13.244478     -10.467101
O     2.6936234     -12.977919     -10.0356
C     12.745019     -7.427413     -28.8138
O     11.614445     -7.4578337     -29.240502
O     13.875593     -7.3969922     -28.387098
C     -6.661843     -13.201875     -11.134106
O     -6.6388564     -13.177452     -9.925771
O     -6.6848296     -13.226298     -12.34244
Q     -15.827417     5.6858957     15.800936
N     -15.940673     5.236937     16.044549
N     -15.714161     6.1348545     15.557323
C     -0.3101226     -13.651874     -27.180986
O     -1.3280167     -13.360691     -26.597632
O     0.70777149     -13.943057     -27.764339

Basically I want the 3 Q N N lines to be moved from the middle of the above list to the end.

Next problem

I would like for each pair of "O" lines, to copy these lines underneath the original O ones and give them the names D1 and D2

ie for every
Code:
C  1 2 3
O  4 5 6
O  6 7 8

to get
Code:
C   1 2 3
O   4 5 6
O   6 7 8
D1  4 5 6
D2  6 7 8

Any help is much appreciated as I've been using excel so far and the lists of data I need to manipulate are now too long for this!

Last edited by Scott; 01-26-2010 at 02:26 PM.. Reason: Added code tags
# 2  
Old 01-26-2010
Code:
awk ' $0 ~ "^[CO]" { print > "COO" } $0 ~ "^[QN]" { print > "QNN" } ' file
cat COO QNN > file

# 3  
Old 01-26-2010
And for the 2nd question Smilie:
Code:
awk '
/^O/{print; $1="D1"; s=$0
  getline
  print; $1="D2"; $0=s ORS $0
}
1' file

# 4  
Old 01-26-2010
Thanks for the help. That works great.

What if I had a third group to sort called Q O O? I've tried extending your awk command:
Code:
awk ' $0 ~ "^[CO]" { print > "COO" } $0 ~ "^[QN]" { print > "QNN" } $0 ~ "^[QO]" { print > "QOO" }' file
cat COO QNN QOO> file

but it doesn't sort the three lines in order this time. Am I missing something? Is awk looking for a pattern in the first symbol in the column data?

Thanks for the help

Last edited by Scott; 01-26-2010 at 02:27 PM.. Reason: Please use code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

A Simple Clock, Well Maybe Not That Simple...

The attachment says it all really... It is a DEMO at a glance digital readout using the "date" command to make it useful... For a Mocbook Pro 13", OSX 10.7.5, but may well work on Linux variants too. Enjoy... #!/bin/bash # # Clock.sh # A bash DEMO to create a 6 x 7 character set... (4 Replies)
Discussion started by: wisecracker
4 Replies

2. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

3. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

4. UNIX for Advanced & Expert Users

HELP on sorting

hi everyone, I am kind of new to this forum. I need help in sorting this data out accordingly, I am actually doing a traceroute application and wants my AS path displayed in front of my address like this; 192.168.1.1 AS28513 AS65534 AS5089 AS5089 .... till the last AS number and if possible... (1 Reply)
Discussion started by: sam127
1 Replies

5. Shell Programming and Scripting

Sorting-help

Hi, Need help in sorting filenames.. I have few files with same timestamp, only the file names differ, for example track_feb01_456.txt track_jan01_687.txt track_feb04_234.txt After sorting i should get track_feb01_456.txt but i need to sort it with month jan...this is what i did ls... (4 Replies)
Discussion started by: raghulshekar
4 Replies

6. Shell Programming and Scripting

Sorting

I've been on holiday, and the brain's clearly not back in gear yet... On Solaris, I have a simple list of package names... DGBUpPost1.43_R1 DGSApp1.44_S1 DGSApp1.47_V1 DGSApp1.48_W0 DGPEuroC1.10_K0 DGPEuroC1.11_L0 DGPEuroC1.9_J0 DGSRet1.10_K0 I just want to properly sort them into... (2 Replies)
Discussion started by: JerryHone
2 Replies

7. Programming

Sorting in C++..

Hi, I need to do a sorting of 2 arrays. One array contains the values of both integer and character and other array can be anything. For example: Array={'1L','2C','NULL','23L','11L','4C','10L','9C'} Array= {'01-02-13-1x','02-11-23-3s','00-12-13-5f','NULL','22k',} If any of these arrays... (6 Replies)
Discussion started by: ronix007
6 Replies

8. Shell Programming and Scripting

Simple to you not simple to me pattern matchin help

hey all, im new and my first question is: say i have a word "blahblah" how do i get and replace the last letter of the word with say k, so replace the h with a k. However you cant just replace the h it has to change the LAST LETTER of the word. Cheers In advance. :b: (0 Replies)
Discussion started by: aleks001
0 Replies

9. Programming

Simple C question... Hopefully it's simple

Hello. I'm a complete newbie to C programming. I have a C program that wasn't written by me where I need to write some wrappers around it to automate and make it easier for a client to use. The problem is that the program accepts standard input to control the program... I'm hoping to find a simple... (6 Replies)
Discussion started by: Xeed
6 Replies

10. UNIX for Dummies Questions & Answers

Ok simple question for simple knowledge...

Ok what is BSD exactly? I know its a type of open source but what is it exactly? (1 Reply)
Discussion started by: Corrail
1 Replies
Login or Register to Ask a Question