Need help comparing two files and deleting some things in those files!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help comparing two files and deleting some things in those files!
# 8  
Old 07-21-2010
Quote:
Originally Posted by citaylor
Will the versions always be a normal floating point value ?
For example 1.1, 1.2, etc ?
Or could you have 1.1.1, 1.1.2 ?
If it is the former we could use a numerical loop, otherwise you will have to use multiple nested loops to loop through from the minimum to the maximum version number.
Nope always normal floating point like 1.1, 1.2 , and so on and can also be as 1.11 1.12 etc etc. CCould you show me how to do this please?

---------- Post updated at 01:57 PM ---------- Previous update was at 11:26 AM ----------

Quote:
Originally Posted by rdcwayx
Seems the for loop in awk has bug? for first awk, i expect to get 4 output: 1.5, 1.4, 1.3, 1.2, but there are only 3.

Code:
$ awk 'BEGIN {for (i=1.5;i>=1.2;i-=0.1) print i}'
1.5
1.4
1.3
 
$ awk 'BEGIN {for (i=15;i>=12;i-=1) print i}'
15
14
13
12

For below code, last one is missed.
Code:
$ awk 'NR==FNR {for (i=2;i<=NF;i+=2) a[$1 FS $i]=$(i+1) ;next}
     {printf $1 FS} {for (i=$3;i>=$2;i-=0.1) printf i FS a[$1 FS i] FS}{printf "\n"}' File2 File1
 
pictures.txt 1.3 ref5432 1.2 ref4244
dance.txt 1.4 ref0695 1.3 ref5738
treehouse.txt 1.5 ref3284 1.4 ref5838

Can anyone help to check?
Could we not take advantage of the bug and do:

Code:
for (i=$3;i>=$2-0.1;i-=0.1)


Last edited by linuxkid; 07-21-2010 at 10:18 AM..
# 9  
Old 07-22-2010
or:

Code:
awk 'NR==FNR {for (i=2;i<=NF;i+=2) a[$1 FS $i]=$(i+1) ;next}
     {printf $1 FS} {for (i=($3*10);i>=($2*10);i--) printf i/10 FS a[$1 FS i/10] FS}{printf "\n"}' File2 File1

This User Gave Thanks to rdcwayx For This Post:
# 10  
Old 07-22-2010
Quote:
Originally Posted by rdcwayx
or:

Code:
awk 'NR==FNR {for (i=2;i<=NF;i+=2) a[$1 FS $i]=$(i+1) ;next}
     {printf $1 FS} {for (i=($3*10);i>=($2*10);i--) printf i/10 FS a[$1 FS i/10] FS}{printf "\n"}' File2 File1

Is this working for you mate? because with me its just displaying:

Code:
pictures.txt 1.3  1.2 1.1
dance.txt 1.4 1.3 1.2
treehouse.txt 1.5  1.4 1.3
pictures.txt 
dance.txt
treehouse.txt

As i'm using Solaris I tried putting /usr/bin/nawk and then it just diplays the filenames on its own. The only difference I found was that I had File1 File2 at the end of the awk statement, but changing that around jsut swapped the outut around.

Just got it to work! put /usr/bin/nawk at the front of this one at it worked! dunno why it didnt for teh otehrs though! thanks!!!

p.s. you couldn't give me a little walk-through the code could you? I get bits of it but not all! Thanks!

Last edited by linuxkid; 07-22-2010 at 05:05 AM..
# 11  
Old 07-22-2010
read File2 first, "a[$1 FS $i]=$(i+1)" is to build two-dimensional array, record filename ($1) and number. the value is the reference number.

Then read File1, it will print the number in the range from File1
# 12  
Old 07-23-2010
Quote:
Originally Posted by rdcwayx
or:

Code:
awk 'NR==FNR {for (i=2;i<=NF;i+=2) a[$1 FS $i]=$(i+1) ;next}
     {printf $1 FS} {for (i=($3*10);i>=($2*10);i--) printf i/10 FS a[$1 FS i/10] FS}{printf "\n"}' File2 File1

Sorry to re-open this, but it doesn't seem to work if we have files in decimals like 1.12 , 1.23 i.e. two decimal places! Smilie help!
# 13  
Old 07-23-2010
Code:
awk 'NR==FNR {for (i=2;i<=NF;i+=2) a[$1 FS $i]=$(i+1) ;next}
     {for (i in a) 
        {split(i,s,FS)
        if (s[1]==$1 && s[2]>=$2 && s[2]<=$3 ) print i,a[i]|"sort -nr"}
     }' File2 File1  |awk '!a[$1] {printf RS $1 FS;a[$1]=1} {printf $2 FS $3 FS}'

The first awk will generate the search result with sort as below.

Code:
treehouse.txt 1.5 ref3284
treehouse.txt 1.4 ref5838
treehouse.txt 1.3 ref4738
pictures.txt 1.3 ref5432
pictures.txt 1.2 ref4244
dance.txt 1.4 ref0695
dance.txt 1.3 ref5738
dance.txt 1.2 ref4948

The second awk Rearranged the output.

Code:
treehouse.txt 1.5 ref3284 1.4 ref5838 1.3 ref4738
pictures.txt 1.3 ref5432 1.2 ref4244
dance.txt 1.4 ref0695 1.3 ref5738 1.2 ref4948

# 14  
Old 07-26-2010
The problem is if I have the file like
File1


Code:
pictures.txt 1.1 1.3
dance.txt 1.2 1.4
treehouse.txt 1.12 1.14


File2


Code:
pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244
dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2 ref4948 1.1
treehouse.txt 1.15 ref8573 1.4 ref3284 1.13 ref5838 1.12 ref4738 1.11 ref4573

Then the output from the code:

Code:
awk 'NR==FNR {for (i=2;i<=NF;i+=2) a[$1 FS $i]=$(i+1) ;next}
     {printf $1 FS} {for (i=($3*10);i>=($2*10);i--) printf i/10 FS a[$1 FS i/10] FS}{printf "\n"}' File2 File1

is:

Code:
pictures.txt 1.3 ref5432 1.2 ref4244 1.1
dance.txt 1.4 ref0695 1.3 ref5738 1.2 ref4948
treehouse.txt

as it totally ignores whatever is after the filename if it has a decimal to 2 decimal places

Last edited by linuxkid; 07-26-2010 at 05:21 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. Shell Programming and Scripting

Trying to make a bash script that goes through directory files and changes things

I'm trying to write a script in a directory that goes through the column the user specifies of 4 files that are inside the directory and calculates the min and the max values. This means that if the user specifies column 5, the script will go through column 5 of all 4 files and all that should give... (2 Replies)
Discussion started by: Eric1
2 Replies

3. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

4. Shell Programming and Scripting

Comparing files in a directory against an array of files

I hope I can explain this correctly. I am using Bash-4.2 for my shell. I have a group of file names held in an array. I want to compare the names in this array against the names of files currently present in a directory. If the file does not exist in the directory, that is not a problem.... (5 Replies)
Discussion started by: BudMan
5 Replies

5. UNIX for Advanced & Expert Users

How to find duplicates contents in a files by comparing other files?

Hi Guys , we have one directory ...in that directory all files will be set on each day.. files must have header ,contents ,footer.. i wants to compare the header,contents,footer ..if its same means display an error message as 'files contents same' (7 Replies)
Discussion started by: Venkatesh1
7 Replies

6. Shell Programming and Scripting

Comparing the matches in two files using awk when both files have their own field separators

I've two files with data like below: file1.txt: AAA,Apples,123 BBB,Bananas,124 CCC,Carrot,125 file2.txt: Store1|AAA|123|11 Store2|BBB|124|23 Store3|CCC|125|57 Store4|DDD|126|38 So,the field separator in file1.txt is a comma and in file2.txt,it is | Now,the output should be... (2 Replies)
Discussion started by: asyed
2 Replies

7. Shell Programming and Scripting

AIX system.... deleting files in remote directory after retrieving files

Hi Friends, I am new to this , I am working on AIX system and my scenario is to retrive the files from remote system and remove the files from the remote system after retreving files. I can able to retrieve the files but Can't remove files in remote system. Please check my code and help me out... (3 Replies)
Discussion started by: vinayparakala
3 Replies

8. UNIX for Dummies Questions & Answers

Deleting the files comparing the creation dates

Hi Gurus, I am new to unix. I have a requirement where i need to delete some files in a folder twice a week. Suppose i have a folder AAA. In that i have files from 01/04/2008 to 10/04/2008 I want to remove all the files except last 3 days i.e., 10,9th & 8th. Every week twice we want to... (2 Replies)
Discussion started by: pssandeep
2 Replies

9. UNIX for Advanced & Expert Users

comparing shadow files with real files

Hi I need to compare shadow file sizes with their real file counterparts. If the shadow file size differs form the realfile size then it must send a mail. My problem is that our system has over 1600 shadowfiles in different directories, with different names. the only consistancy is the .sh file... (4 Replies)
Discussion started by: terrym
4 Replies
Login or Register to Ask a Question