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!
# 15  
Old 07-26-2010
At Post 13, I have provided the latest code, do you see that? With new input, I still get correct O/P.

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
treehouse.txt 1.13 ref5838
treehouse.txt 1.12 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

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}'

treehouse.txt 1.13 ref5838 1.12 ref4738
pictures.txt 1.3 ref5432 1.2 ref4244
dance.txt 1.4 ref0695 1.3 ref5738 1.2 ref4948

# 16  
Old 07-26-2010
Code:
#!/bin/bash
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}'

saved as test.sh
in terminal:
Code:
qp232d: chmod +x  test.sh
qp232d: bash test.sh
awk: syntax error near line 1
awk: bailing out near line 1
awk: a is not an array
  record number 1
qp232d:

same errors occur if i call it like
Code:
./test.sh

# 17  
Old 07-26-2010
Don't forget to use /usr/bin/nawk in Solaris.
# 18  
Old 07-27-2010
Quote:
Originally Posted by methyl
Don't forget to use /usr/bin/nawk in Solaris.
Thank you, that made the script compile!

---------- Post updated at 09:07 AM ---------- Previous update was at 08:49 AM ----------

Quote:
Originally Posted by rdcwayx
At Post 13, I have provided the latest code, do you see that? With new input, I still get correct O/P.

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
treehouse.txt 1.13 ref5838
treehouse.txt 1.12 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

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}'
 
treehouse.txt 1.13 ref5838 1.12 ref4738
pictures.txt 1.3 ref5432 1.2 ref4244
dance.txt 1.4 ref0695 1.3 ref5738 1.2 ref4948

my apologies. I have one LAST EVER problem (PROMISE!). if in file1 we say

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

so treehouse.txt goes from 1.9 to 1.12
and in file2 we then have:
Code:
pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 1.1
dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2 ref4948 1.1
treehouse.txt 1.12 ref8573 1.11 ref3284 1.10 ref5838 1.9 ref4738 1.8 ref4573 1.7 ref3232

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

I think this may be due to the fact that although i am restrcited to naming versions 1,2,3 etc as 1.1,1.2,1.3. When I get to a higer version number like 12 (i.e. 1.12), although I see it as higher than versions like 1.8, the shell doesn't as it see's decimals. I think if we could remove the "1." at the start and then compare them in the if, and before we output, then add it back on, this could work? Is there anyway you could help me please?
# 19  
Old 07-27-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 ($2>$3) {t=$3;$3=$2;$2=t}
        if (s[1]==$1 && s[2]>=$2 && s[2]<=$3 ) print i,a[i]|"sort -nr"}
     }' File2 File1

treehouse.txt 1.9 ref4738
treehouse.txt 1.8 ref4573
treehouse.txt 1.7 ref3232
treehouse.txt 1.12 ref8573
pictures.txt 1.3 ref5432
pictures.txt 1.2 ref4244
pictures.txt 1.1
dance.txt 1.4 ref0695
dance.txt 1.3 ref5738
dance.txt 1.2 ref4948

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 ($2>$3) {t=$3;$3=$2;$2=t}
        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}'

treehouse.txt 1.9 ref4738 1.8 ref4573 1.7 ref3232 1.12 ref8573
pictures.txt 1.3 ref5432 1.2 ref4244 1.1
dance.txt 1.4 ref0695 1.3 ref5738 1.2 ref4948

# 20  
Old 07-27-2010
Quote:
Originally Posted by rdcwayx
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 ($2>$3) {t=$3;$3=$2;$2=t}
        if (s[1]==$1 && s[2]>=$2 && s[2]<=$3 ) print i,a[i]|"sort -nr"}
     }' File2 File1
 
treehouse.txt 1.9 ref4738
treehouse.txt 1.8 ref4573
treehouse.txt 1.7 ref3232
treehouse.txt 1.12 ref8573
pictures.txt 1.3 ref5432
pictures.txt 1.2 ref4244
pictures.txt 1.1
dance.txt 1.4 ref0695
dance.txt 1.3 ref5738
dance.txt 1.2 ref4948

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 ($2>$3) {t=$3;$3=$2;$2=t}
        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}'
 
treehouse.txt 1.9 ref4738 1.8 ref4573 1.7 ref3232 1.12 ref8573
pictures.txt 1.3 ref5432 1.2 ref4244 1.1
dance.txt 1.4 ref0695 1.3 ref5738 1.2 ref4948

this doesn't print out anything between 1.9 and 1.12 though, is there anyway we can change it to do so? and IF possible, arrange the output so it retains its order of highest version number to lowest? many thanks
# 21  
Old 07-27-2010
The outputs follow your rules:

1. output between 1.9 and 1.12

Code:
treehouse.txt 1.9 ref4738 1.8 ref4573 1.7 ref3232 1.12 ref8573

2. "arrange the output so it retains its order of highest version number to lowest."

All from highest to lowest.

Code:
treehouse.txt 1.9 ref4738 1.8 ref4573 1.7 ref3232 1.12 ref8573
pictures.txt 1.3 ref5432 1.2 ref4244 1.1
dance.txt 1.4 ref0695 1.3 ref5738 1.2 ref4948


Last edited by rdcwayx; 07-27-2010 at 07:52 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