Beginner: Count & Sort Using Array's


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Beginner: Count & Sort Using Array's
# 8  
Old 02-07-2011
Code:
awk 'NR==FNR && ++a[$1] >1{b[$1]=1} NR!=FNR{if(b[$1]) {print >>"file1.txt"} else {print >> "file2.txt"}}' file1.txt file2.txt

That works absolutely perfectly! Thanks guys!

The only thing is I havent got a clue how it works, specifically how it knows to look in 'mainfile.txt' as it is not specified. There are a few other files being generated immediately before this line of code and are in the same directory so how does NR (number of records?)and FNR (all files?) know which file to look in (and it is definitely looking at mainfile.txt because i've checked).

Thanks again for all contributions and the solutionSmilie

Last edited by Scott; 02-07-2011 at 01:59 PM.. Reason: Code tags, please...
# 9  
Old 02-07-2011
when NR==FNR, code read through the mainfile file to get which the repeat time of item ($1) is over 1(++a[$1]>1), and give them a tag (b[$1]=1), next, NR>FNR(or NR!=FNR), code read throught the mainfile again, for this time, based on your rule (b[$1]==1, file1; b[$1]==0, file2), generate two output files: file1 and file2. Does it make sense?
# 10  
Old 02-08-2011
Hi Yinyuemi,

I'm afraid not. I understand the mathematics of how it decides if $1 is in more than once and if so what to do with it, and if not what to do with it.

it's just, at no point do i explicitly state "look in 'mainfile.txt' for this information"

there is a Master file. From this i strip some things out into another file. And again i strip some more data from this to create Mainfile.txt.

it seems no matter where i put this:

Code:
awk 'NR==FNR && ++a[$1] >1{b[$1]=1} NR!=FNR{if(b[$1]) {print >>"file1.txt"} else {print >> "file2.txt"}}' file1.txt file2.txt

no matter at which stage i place it as all the above files are created - at the beginning, in between them - it does the same thing and looks in 'mainfile.txt'. How?

I understand the high likelihood that this is just a case of me being a complete idiot and need something incredibly simple spelled out for me, but with only three months sporadic linux/bash programming under my belt that's not too surprising Smilie

Cheers again for your help.

Ian

Last edited by radoulov; 02-08-2011 at 08:10 AM.. Reason: Code tags!
# 11  
Old 02-08-2011
Quote:
Originally Posted by BigTOE
Code:
awk 'NR==FNR && ++a[$1] >1{b[$1]=1} NR!=FNR{if(b[$1]) {print >>"file1.txt"} else {print >> "file2.txt"}}' file1.txt file2.txt

That works absolutely perfectly! Thanks guys!
really, I can't believe. tpyo in it?
# 12  
Old 02-09-2011
Haha yeah it works, I already put in the '.txt' and the correct paths.

just dont know how it works...

---------- Post updated at 11:26 AM ---------- Previous update was at 09:34 AM ----------

OK,

remember how i said there is a Master file...

and from this i strip data out to produce another file...

and from this i strip more data out to get Mainfile.txt

and when i ran the array it somehow looked in Mainfile.txt and it all tallied up correctly.



well now i've tried using a different Master file. All the changes filter through all subsequent steps until it gets to the array part - and it is still outputting the same data as before Smilie .i.e it is still looking in the same place for it's data.

Is there any way i can explicitly state to look in mainfile.txt?
# 13  
Old 02-10-2011
Solved

OK, scratch NR==FNR.

I tweaked jim's:-

Code:
awk ' {arr[$1]++; next} END{for (i in arr) {print arr[i], i }} ' mainfile.txt > t.tmp
awk ' FILENAME=="t.tmp" {arr[$2]=$1; next}
       FILENAME=="mainfile.txt" { if(arr[$1]>1) 
                                                   {print$0 >"file1.txt" }  
                                             else 
                                                  {print $0 >"file2.txt" } }' t.tmp mainfile.txt




this one does work perfectly. Thankyou to everyone who contributed

Ian Smilie
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 02-10-2011 at 09:12 AM.. Reason: code tags, please!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Connection Logging in Solaris 10 & 11 - Beginner

Excuse my ignorance as I am very new to working with Solaris. I'm looking for documentation on how to create a network log in Solaris 10 & 11. I don't wish to edit any of the logs currently the system. I simply want a log that will capture all incoming IP addresses and log them with a time-in... (8 Replies)
Discussion started by: FamousAv8er
8 Replies

2. Shell Programming and Scripting

Sort multidimensional Array

Hello I have a problem. I create a Multidimensional Array Like this: ENTRY="$kunnum-$host" ENTRY="$host" ENTRY="# $3" for key in "${!ENTRY}"; do ENTRIES=${ENTRY} # INDEX=IP(5) donedeclare -p declare -A ENTRIES=(="unas15533" ="unas" ="# RDP-Terminal 2"... (12 Replies)
Discussion started by: Marti95
12 Replies

3. UNIX for Beginners Questions & Answers

Difference of Sort -n -k2 -k3 & Sort -n -k2,3

Hi, Could anyone kindly show me a link or explain the difference between sort -n -k2 -k3 & sort -n -k2,3 Also, if I like to remove the row with repetition at both $2 and $3, Can I safely use sort -u -k2 -k3 Example; 100 20 30 100 20 30 So, both $2 and $3 are same and I... (2 Replies)
Discussion started by: Indra2011
2 Replies

4. Shell Programming and Scripting

File Move & Sort by Name - Kick out Bad File Names & More

I have a dilemma, we have users who are copying files to "directory 1." These images have file names which include the year it was taken. I need to put together a script to do the following: Examine the file naming convention, ensuring it's the proper format (e.g. test-1983_filename-123.tif)... (8 Replies)
Discussion started by: Nvizn
8 Replies

5. Shell Programming and Scripting

Array Count and Array Copy help

Hi all, I have the code as below " echo "File carried list after 1st loop "${fileStreamAssiagnInit}"" and I have the out put for the above code as below : Output : File carried list after 1st loop abcInd.csv sdgUS.csv sopSing.csv Here i want to count the number of elements in... (3 Replies)
Discussion started by: Balasankar
3 Replies

6. Shell Programming and Scripting

[Beginner's questions] Filename Validation & Parsing

Hi !! I'm rather new both to the UNIX and scripting worlds, and I'm learning the ropes of scripting. Having said this, please excuse me if you notice certain basic errors. I'm working on a script that implements .jar and .war files for a WAS environment and I need to perform certain... (4 Replies)
Discussion started by: levaldez
4 Replies

7. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

8. UNIX for Dummies Questions & Answers

Sort with respect to count

Hello! I have a file with 4 columns. I am trying to have it sort first with respect to the first column, and then with respect to the number of counts (in descending count) in the second column within the same first column identity. For example: Input: 1 2 A 1 1 6 B 2 2 5 G 7 1 6 D 4... (8 Replies)
Discussion started by: anchuz
8 Replies

9. Shell Programming and Scripting

How to sort by count

Hello! I have a file with 4 columns. I am trying to have it sort first with respect to the first column, and then with respect to the number of counts (in descending count) in the second column within the same first column identity. For example: Input: 1 2 A 1 1 6 B 2 2 5 G 7 1 6 D 4 1... (1 Reply)
Discussion started by: anchuz
1 Replies

10. Shell Programming and Scripting

Sort and count using AWK

Hi, I've a fixed width file where I need to count the number of patterns from each line between characters 1 to 15 . so can we sort them and get a count for each pattern on the file between 1 to 15 characters. 65795648617522383763831552 410828003265795648 6175223837... (5 Replies)
Discussion started by: rudoraj
5 Replies
Login or Register to Ask a Question