Compare two files, then outputs line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two files, then outputs line number
# 1  
Old 09-14-2012
Compare two files, then outputs line number

I have two files, "ranked.txt" and "sorted.txt". Sorted.txt is a smaller subset from ranked.txt that is sorted in alpha order. However ranked.txt preserves the ranking of words I would like to keep.

How do I check the rank of every word in sorted.txt when matched to the original ranked.txt? I want to output the rank (which is the line number) from ranked.txt into sorted.txt.

ranked.txt:
1 the
2 quick
3 brown
4 fox
5 jumped

sorted:
brown
fox
jumped
quick
the

output.txt:
3 brown
4 fox
5 jumped
2 quick
1 the

This is what I have so far in Python:

Code:
for word in sorted_list:
    for num, line in enumerate(ranked_list, 1):
        if word in line:
            print word, '\t', num

Not doing exactly what I want... is it easier to do with sed/grep/awk??
# 2  
Old 09-14-2012
If your lists aren't huge, you can do it this way:

Code:
awk  'FNR == NR { rank[$1] = NR; next; } { print rank[$1], $1 }' rank.txt sorted.txt

I assumed the list in ranked.txt was just the words, and didn't contain line numbers. If it contains line numbers, a small tweak:

Code:
awk  'FNR == NR { rank[$2] = NR; next; } { print rank[$1], $1 }' rank.txt sorted.txt

# 3  
Old 09-15-2012
Thanks agama! Works like a charm!

Do you know how to sort the output properly by the rank? I tried using "sort -k2 output.txt", but now it looks like:

output.txt
10002 the
10005 quick
10008 brown
10009 fox
1000 jumped
10001 over

When it should be sorted like:

1 the
2 quick
3 brown
4 fox
# 4  
Old 09-15-2012
Quote:
Originally Posted by pxalpine
T
Do you know how to sort the output properly by the rank? I tried using "sort -k2 output.txt", but now it looks like:
output.txt
10002 the
10005 quick
10008 brown
10009 fox
1000 jumped
10001 over

When it should be sorted like:
1 the
2 quick
3 brown
4 fox
Have you tried using..

Code:
sort -nrk1 file

# 5  
Old 09-15-2012
Code:
sort -nrk1 file

seems to sort the file from the end.. makes it look like:

Code:
997690 últmos
997689 óm
997688 ódicos
997687 ívoro
997686 éxitosos
997685 éara
997684 èrsonal
997683 çilek
997682 áutobuses
997680 zoccolini
997679 zmur

---------- Post updated at 09:03 PM ---------- Previous update was at 08:50 PM ----------

I used sort -nk1 and got the desired output, thanks!
# 6  
Old 09-15-2012
this will sort from first to last..

Code:
sort -n file

# 7  
Old 09-15-2012
Thank you pamu! One final question: when comparing two files, how can i output lines in a.txt but not in b.txt? (while ignoring the lines common in both files)

Code:
file: a.txt
facebook
youtube
yahoo
google
microsoft

Code:
file: b.txt
yahoo
google
pandora
facebook
yelp

Output would be:
pandora
yelp

I tried comm -23 but I still get "facebook" present in the output file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 files having different number of columns and records

Hi , My requirement is to Compare 2 files having different number of columns and records and get the ouptut containing all the non-matching records from File A(with all column values ) .Example data below : File A contains following : Aishvarya |1234... (4 Replies)
Discussion started by: aishvarya.singh
4 Replies

2. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

3. Shell Programming and Scripting

Compare two outputs/files based on criterias

Hello, I currently have a script that outputs to a file that contains the output below. It runs every X minutes. I would like to compare the first run against the second but only output if the minutes column is less than its original or if anything else changes. Thanks for the help. Original ... (2 Replies)
Discussion started by: tworkemon
2 Replies

4. Shell Programming and Scripting

How do I number my for loop outputs with this method?

Currently I am outputting users and I want to number them starting with 1... grep name list.txt | awk -F"=" '{ print $2 }' | while read user; do echo -e "1\t|$user" Currently I have: 1 | john 1 | amy 1 | max I want it to look like 1 | john 2 | amy 3 | max (2 Replies)
Discussion started by: etranman1
2 Replies

5. Shell Programming and Scripting

[ask]how to compare line number

dear all, i need help about this case i have line number extract in file fileA.txt like this: 231 123 444 231 213 ... now i want to compare the line number fileA.txt to find other file in fileB.txt exp: if i use one by one like this: cat fileB.txt | sed -n -e "444"p huff...is not... (3 Replies)
Discussion started by: zvtral
3 Replies

6. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

7. Shell Programming and Scripting

Shell script to count number of ~ from each line and compare with next line

Hi, I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each... (3 Replies)
Discussion started by: Ganesh Khandare
3 Replies

8. Shell Programming and Scripting

Compare number of line

Hi expert, I using csh and i trying to compare no. of line of the file. Example: I have 2 files. file1 ONE TWO THREE FOUR FIVE file 2 ONE TWO THREE FOUR file1 have 5 lines but file2 have 4 lines. (1 Reply)
Discussion started by: vincyoxy
1 Replies

9. Shell Programming and Scripting

Script to Compare a large number of files.

I have a large Filesystem on an AIX server and another one on a Red Hat box. I have syncd the two filesystems using rsysnc. What Im looking for is a script that would compare to the two filesystems to make sure the bits match up and the number of files match up. its around 2.8 million... (5 Replies)
Discussion started by: zippdawg2001
5 Replies

10. Shell Programming and Scripting

compare 2 outputs

Hello, I have a shell script that is used as follows: ./script -s <Oracle_server_name> the script returns several lines in this format: parameter name required value Current Value comments --------------- ------------- -------------- --------- My objective now is to compare 2... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies
Login or Register to Ask a Question