Sort/Grep Question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort/Grep Question
# 1  
Old 03-02-2006
Sort/Grep Question

Hello all,
I have a test file that has the format:

.....
O
3.694950 -.895050 1.480000
O
5.485050 .895050 1.480000
Ti
-4.590000 4.590000 2.960000
Ti
-2.295000 2.295000 1.480000
O
-5.485050 -5.485050 -4.440000
Ti
.000000 4.590000 2.960000
O
3.694950 -5.485050 -4.440000
....

Basically there is a name/descriptor on one line, followed by the data attached to that name/descriptor on the next line. I'm trying to sort the data so that all the "Ti' lines are together, and all the "O" lines are together. Something like:

Ti
data
Ti
data
Ti
data
...
O
data
O
data
...

I've tried using grep like "grep Ti datafile > out1" and "grep O datafile > out2"
"cat out1 out2 > outnew", but this ignores the data lines. The sort command puts all the data lines together, followed by O lines, followed by Ti lines. I want to keep the data attached to the descriptor, which I can't get sort to do. I can't seem to find any switches in grep or sort that will help me out.

Is there a way to solve my problem with a simple command? I'm sure I could write a python script to solve this, but a command-line solution would be nice.

Thanks a lot.
# 2  
Old 03-02-2006
# 3  
Old 03-02-2006
You could use paste to join the two lines, sort then use tr to split, e.g...
Code:
paste - - < file1 | sort | tr \\t \\n > file2

# 4  
Old 03-06-2006
Hmmm... My grep version (from 'man grep') doesn't seem to support those flags.
# 5  
Old 03-06-2006
Quote:
Originally Posted by Ygor
You could use paste to join the two lines, sort then use tr to split, e.g...
Code:
paste - - < file1 | sort | tr \\t \\n > file2

Hmmm... could you explain this command? I admit that it leaves me for a bit of a loss. It looks like a lot of piping, and I'm not following it all.

Thanks,
Aaron
# 6  
Old 03-06-2006
Quote:
Originally Posted by aarondesk
Hmmm... could you explain this command? I admit that it leaves me for a bit of a loss. It looks like a lot of piping, and I'm not following it all.

Thanks,
Aaron
that's pretty good, Ygor!

1. 'paste' - joins adjacent lines and separates them with TAB [by default]
2. 'sort' - well.... sorts its input
3. 'tr' - transliterates 'input' chars to the 'output' chars - in our case: TAB to NL (breaking down the lines we've joined in step 1.)

Last edited by vgersh99; 03-06-2006 at 06:58 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sort question

Have a text file containing 4 columns, the 4th column is sorted numerically in descending order. The 2nd column is alpha text. Been able to get the 4th column to sort in descending order. The 2nd column sorts correctly but it's also in reverse. Would like to be able to keep the 4th column sort... (8 Replies)
Discussion started by: jimmyf
8 Replies

2. Homework & Coursework Questions

awk with Grep and Sort

1. The problem statement, all variables and given/known data: Please bare in mind I am a complete novice to this and have very very basic knowledge so please keep any answers as simple as possible and explain in terms I will understand ahha :):) I have a text file of names and test scores... (1 Reply)
Discussion started by: jamesb18
1 Replies

3. Homework & Coursework Questions

Grep and Sort

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1. Print the number of people that are in the /etc/passwd file with the name of George 2. Sort by name and... (8 Replies)
Discussion started by: Jagst3r21
8 Replies

4. Shell Programming and Scripting

grep from 3 lines and sort

Pseudo name=hdiskpower54 Symmetrix ID=000190101757 Logical device ID=0601 state=alive; policy=SymmOpt; priority=0; queued-IOs=0 ============================================================================== ---------------- Host --------------- - Stor - -- I/O Path - -- Stats --- ### HW... (7 Replies)
Discussion started by: Daniel Gate
7 Replies

5. Shell Programming and Scripting

Sort question.

Hi Guys, I have a file to be sorted and uniq with the following format. S00001002|01|20021231 S00001002|01|20011031 S00001006|01|20120430 S00001006|01|20111231 S00001006|01|20111031 S00001006|01|20110831 S00001006|01|20110731 S00001006|01|20101231 S00001006|01|20091231... (5 Replies)
Discussion started by: nua7
5 Replies

6. Shell Programming and Scripting

how to grep sort userids

hello folks i have a file that have data like /test/aa/123 /test/aa/xyz /test/bb/xyz /test/bb/123 in above lines i just wants to grep "aa" and "bb". Thanks, Bash (4 Replies)
Discussion started by: learnbash
4 Replies

7. Shell Programming and Scripting

sort question

Hi all. HPUX - posix shell - script question Here's my data: f1 f2 f3 f4 f5 f6 |02/12/09|12:33PM|3|Oceanview |OVT #1| VISA/MC |02/12/09|12:14PM|3|Oceanview |OVT #1| VISA/MC |02/12/09|09:13AM|4|Oceanview |OVT #1| VISA/MC ... (8 Replies)
Discussion started by: lyoncc
8 Replies

8. Shell Programming and Scripting

sort question

Hi all. Is there a way that I can use the sort command too sort the following field by earliest time (12:00AM) to latest time (11:59PM)? 07:12PM 07:53PM 07:54PM 08:07PM 10:15AM 10:21AM TIA!!!!!! (1 Reply)
Discussion started by: lyoncc
1 Replies

9. UNIX for Dummies Questions & Answers

SORT question

I'm looking for a sort command that will sort by zip code first then by last name. (zip code is the last field, last name is first field) data file looks like this. Hope Bob 1234 Main ST. Colorado Springs, CO 80920 I was thinking along the lines of: sort -k9n address.data //for the... (2 Replies)
Discussion started by: ssgatbliss
2 Replies

10. UNIX for Dummies Questions & Answers

Sort Question

M 47 HIS:LOT 32 DUTY 2 MIKE, FINISHED MIKE ACTIVE STATUS 23TASK YES GOOD 100TASK NO GOOD ======================================== M 47 HIS:LOT 1 DUTY 1 MIKE, FINISHED MIKE ACTIVE STATUS 23TASK YES GOOD ... (7 Replies)
Discussion started by: bobo
7 Replies
Login or Register to Ask a Question