Comparing lists.. Arrays maybe?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Comparing lists.. Arrays maybe?
# 1  
Old 06-08-2011
Comparing lists.. Arrays maybe?

Problem
Part 1.
Gather data from linux server and output to a file named data_DDMMYY
Output file must contain the file name and size

Part 2.
Compare todays data_DDMMYY to yesterdays data_DDMMYY and output results to a file named difference_DDMMYY
Output file must show the difference in size between same named files and identify new and deleted files

Solution
Part 1.
Log into linux, use command "find . -daystart -ctime 0 -type f | xargs ls -sSh > data_DDMMYY" thankyou Yazu
Sample output:
Comparing lists.. Arrays maybe?-example_filepng

Part 2.
Perhaps use of an array and iterator? (my knowledge is very limited when it comes to arrays and iterators)

Idea
Grab file 1, go to line 1, read first string and save to variable x, read second string and save to list2
Grab file 2, using the current line in list2, look for a match.

If a match is found, go to that line, read first string and save to variable y
calculate difference of x-y and save to list1, output first line of list 1 and list 2 to difference_DDMMYY

If match is not found, output <string>.notFound to a new line in difference_DDMMYY


Grab file 1, go to line 2, read first string and save to x, read second string and save to new line in list 2
etc ... until all lines in file 1 have been used.

Then somehow I'd like to find the lines in file 2 that weren't copied to a list and output them to the difference_DDMMYY
as <String>.newFile
I'm sure there's some sort of for loop I can use to solve this, but as of right now I'm stumped and need ideas >.<
Any and all help is appreciated.
Comparing lists.. Arrays maybe?-example_filepng
# 2  
Old 06-09-2011
Have you tried using awk?
Awk can read multiple files and you can use the array concept in there.

ex: awk 'NR==FNR{A[NR]=$0;next}
{do compare operations}{save the value}' file1 file2 > file3
This User Gave Thanks to vidyadhar85 For This Post:
# 3  
Old 06-09-2011
After you save your files as data_DDMMYY you can use them in below code like file1 and file2, compared files has only one column:

Code:
awk '{if(FNR==NR) {arr[$0]++;next} if($0 in arr) { arr[$0]--; if (arr[$0] == 0) delete arr[$0];next}{print $0 >"newfile2"}} END {for(i in arr){print i >"newfile1"}}' file1 file2

newfile1 and newfile2 are the difference files, newfile1 different lines of file1 from file2 etc.


regards
This User Gave Thanks to EAGL€ For This Post:
# 4  
Old 06-09-2011
Thanks heaps, until i started playing with linux i had never heard of AWK. I'm doing some research now and will hopefully have a better understanding of the language soon ^.^
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing multiple network files (edge lists)

I want to compare 4 edge-lists to basically see if an edge is present in all 4 networks. The issue is that an edge A-B in one file can be present as B-A in another file. Example: Input 1: net1.txt A B 0.1 C D 0.65 D E 0.9 E A 0.7 Input 2: net2.txt A Z 0.1 C D 0.65 E D 0.9 E A... (1 Reply)
Discussion started by: Sanchari
1 Replies

2. Shell Programming and Scripting

awk arrays comparing multiple columns across two files.

Hi, I'm trying to use awk arrays to compare values across two files based on multiple columns. I've attempted to load file 2 into an array and compare with values in file 1, but success has been absent. If anyone has any suggestions (and I'm not even sure if my script so far is on the right lines)... (4 Replies)
Discussion started by: hubleo
4 Replies

3. UNIX for Dummies Questions & Answers

Comparing lists when "diff" isn't sufficiently stringent

Greetings. I like to compare two lists of numbers, A.txt and B.txt, to see the numbers that are in B.txt but not in A.txt. I only need the "deletions" with reference to A.txt. Using the diff command doesn't work because it considers too many of the deletions just changes because they are a... (7 Replies)
Discussion started by: Twinklefingers
7 Replies

4. Shell Programming and Scripting

Comparing 2 arrays but ignoring certain patterns

I'm trying to compare 2 array and print the difference at a 3rd file. However how am i going to compare this 2 arrays by ignoring certain patterns: For example: 1st array contains: ctahci cptcsa0 ctsata:25:seed cptcsa1:50:seed ctsata_1:25:seed 2nd array contains: cptcsa0 ctsata... (0 Replies)
Discussion started by: hongping
0 Replies

5. UNIX for Dummies Questions & Answers

File Field Replacement, Assigning Fields to Variables, Lists/Arrays?

Okay, I've made threads on extracting fields and comparing strings in separate files in .csv's. I've written the following code with intentions of learning more. I just want this one question answered: How can I assign fields from a file(comma separated) to variables? My goal is to check... (0 Replies)
Discussion started by: chickeneaterguy
0 Replies

6. Shell Programming and Scripting

Shell Script to Create non-duplicate lists from two lists

File_A contains Strings: a b c d File_B contains Strings: a c z Need to have script written in either sh or ksh. Derive resultant files (File_New_A and File_New_B) from lists File_A and File_B where string elements in File_New_A and File_New_B are listed below. Resultant... (7 Replies)
Discussion started by: mlv_99
7 Replies

7. Shell Programming and Scripting

PERL: simple comparing arrays question

Hi there, i have been trying different methods and i wonder if somebody could explain to me how i would perform a comparison on two arrays for example my @array1 = ("gary" ,"peter", "paul"); my @array2 = ("gary" ,"peter", "joe"); I have two arrays above, and i want to something like this... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

8. Shell Programming and Scripting

Comparing arrays in perl

Hi all, I am trying to compare two arrays in perl using the following code. foreach $item (@arrayA){ push(@arrayC, $item) unless grep(/$item/, @arrayB); ... (1 Reply)
Discussion started by: chriss_58
1 Replies

9. Shell Programming and Scripting

comparing two arrays or strings in bash

Hi there, im having issue with comparing two variables, in a bash script. im trying to do the following: - get a word from user 1 - split the word into array - get a character from user2 trying to compare the character entered by user 2 with every single character in the array... (2 Replies)
Discussion started by: new2Linux
2 Replies

10. Shell Programming and Scripting

Comparing Arrays?

Is there anyway that I can compare two Arrays to see if any new strings have been added in them? eg: Array 1: Joe Bob Jane Array 2: Joe Bob Jane Greg It would then output a new array with the changes: Array 3: Greg I'm not very good at shell scripting, and my google and forum searches... (4 Replies)
Discussion started by: blckleprd
4 Replies
Login or Register to Ask a Question