How to sort a file and then print similar lines once


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to sort a file and then print similar lines once
# 1  
Old 01-27-2009
How to sort a file and then print similar lines once

Hi!

I have a trouble with the sort and the uniq.
I know I have to use them, I just have trouble with putting them in the right order.

I have a text file with unsorted lines (each line has a few words, the first word in the line is a number.).

I need to sort this file in order to be able to use the uniq command.

I tried to write it like this:

Code:
 
sort -n file | uniq

(I wrote the -n option because the first string in each line is a number. Is it okay?)

but when I cat the file it's not changed, and furthermore, the output is doubling a few lines not necessarily.

What is wrong?

Thanks,
Shira.
# 2  
Old 01-27-2009
Might need a little more info.

What are the file contents?

You also need to redirect and then overwrite the original file.

sort -n $file | uniq > $ftemp

This ~should~ give you a numerically sorted file --- with all duplicate
lines removed.

HOWEVER!!

If you're thinking of changing this:

1 ant
3 cat
2 bat
4 ant

to this:

1 ant
2 bat
3 cat

No, that won't work.
# 3  
Old 01-27-2009
this is the "formula" of my file:

123 12 something1
234 24 something2
345 36 something3
234 24 something2

I know that if a line starts with a certain number, then all the lines that start with the same number equal to this line in all fields.

The problem is to sort it and to uniq it.
# 4  
Old 01-27-2009
don't know, shira.

sort -n a | uniq

worked for me:

123 12 something1
234 24 something2
345 36 something3

file a contained:

123 12 something1
234 24 something2
345 36 something3
234 24 something2

maybe something more specific like:

sort -n -k1,2 a | uniq
# 5  
Old 01-27-2009
ok, now I checked it, and the problem was that I didn't redirect the whole command into another file. So thanks. Smilie

This is what I wrote and it worked:

Code:
sort -n file | uniq > temp

Why can't I change the original file? Is there any way I can?
# 6  
Old 01-27-2009
Tools

Most unix commands and functions relative to files require the input and output files to be different. For safety reasons, but also the issue of how to process a large file at the same time you are doing something to a file.
Typical solution is of the form

Code:
sort file1 >file1.srt
#sometimes put a verification here; just in case
mv file1.srt file1

But, beware of automatically over-writing your original file. If you start with 1000 records, but some part of your command does not quite work as you thought, then you might end up with zero records.

Last edited by joeyg; 01-27-2009 at 01:27 PM..
# 7  
Old 01-27-2009
Code:
{ rm file; sort -nu > file;}< file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

2. Shell Programming and Scripting

Uniq or sort -u or similar only between { }

Hi ! I am trying to remove doubbled entrys in a textfile only between delimiters. Like that example but i dont know how to do that with sort or similar. input: { aaa aaa } { aaa aaa } output: { aaa } { (8 Replies)
Discussion started by: fugitivus
8 Replies

3. Shell Programming and Scripting

Reducing text file using similar lines

Hello, I am a java programmer but want to try unix for a purpose where I need to reduce a file using its first field.. Here is the sample data: admin;2;0;; admission;8;0;; aman;1;0;; caroline;0;4;; cook;0;4;; cook;2;0;; far;0;3;; far;1;5;; I am explaining the dataset first. There... (5 Replies)
Discussion started by: shekhar2010us
5 Replies

4. Shell Programming and Scripting

removing lines with similar values from file

Hello, got a file with this structure: 33274 171030 02/29/2012 37897 P_GEH 2012-02-29 10:31:26 33275 171049 02/29/2012 38132 P_GEH 2012-02-29 10:35:27 33276 171058 02/29/2012 38515 P_GEH 2012-02-29 10:43:26 33277 170748 02/29/2012 40685 P_KOM ... (3 Replies)
Discussion started by: krecik28
3 Replies

5. Shell Programming and Scripting

extracting lines from a file with similar first name

consider i have two files cat onlyviews1.sql CREATE VIEW V11 AS SELECT id, name, FROM etc etc WHERE etc etc; CREATE VIEW V22 AS SELECT id, name, FROM etc etc WHERE etc etc; CREATE VIEW V33 AS (10 Replies)
Discussion started by: vivek d r
10 Replies

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

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

8. Shell Programming and Scripting

remove one of each similar lines in a file

Hello folks I have a question for you gurus of sed or grep (maybe awk, but I would prefer the first two) I have a file (f1) that says: (actually, these are not numbers but md5sum, but for simplicity, let's assume these numbers.) 1 2 3 4 5And I have a file (f2) that says 1|a 1|b 1|c 2|d... (3 Replies)
Discussion started by: tukuyomi
3 Replies

9. Shell Programming and Scripting

Counting similar lines from file UNIX

I have a file which contains data as below: nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/RMBS/RMBSHome.jsf nbk1j7o pageName=/jsp/common/index.jsf nbk1j7o pageName=/jsp/common/index.jsf nbk1wqe... (6 Replies)
Discussion started by: mohsin.quazi
6 Replies

10. Infrastructure Monitoring

Remove Similar Lines from a File

I have a log file "logreport" that contains several lines as seen below: 04:20:00 /usr/lib/snmp/snmpdx: Agent snmpd appeared dead but responded to ping 06:38:08 /usr/lib/snmp/snmpdx: Agent snmpd appeared dead but responded to ping 07:11:05 /usr/lib/snmp/snmpdx: Agent snmpd appeared dead... (4 Replies)
Discussion started by: Nysif Steve
4 Replies
Login or Register to Ask a Question