Venn diagram results using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Venn diagram results using awk
# 1  
Old 07-16-2012
Venn diagram results using awk

Hi,

I have the following files

1.txt
Code:
a 10
b 11
c 12
d 13
e 14
f 15
g 16
h 17
i 18
j 19
k 20

2.txt
Code:
a 21
b 22
c 23
g 24
h 25
i 26
l 27
m 28
n 29
n 30

3.txt
Code:
a -31
f -35
g -36
h 40
i 42
j 44
k 47
l 48


I am looking for four separate files that has the following content


common_three.txt

Code:
a 10 21 -31
g 16 24 -36
h 17 25 40
i 18 26 42


common_1_and_2.txt

Code:
a 10 21
b 11 22
c 12 23
g 16 24
h 17 25
i 18 26


common_1_and_3.txt

Code:
a 10 -31
f 15 -35
g 16 -36
h 17 40
i 18 42
j 19 44
k 20 47


common_2_and_3.txt

Code:
a 21 -31
g 24 -36
h 25 40
i 26 42
l 27 48

The 1.txt, 2.txt and 3.txt files have more than 1000 records in each of them. I tried using genevenn, but it doesn't give the exact values.

Thanks
# 2  
Old 07-16-2012
I've got to step out, so can't actually finish this, but I got the bulk of if basically done, just needs some adaptation and to be thrown in a shell script that takes parameters to be done cleaner.

Code:
for num in $(cat 1.txt|awk '{print $1}'); do grep "^$num" [21].txt | tr -d '\n' | sed 's/\([0-9].txt:\)//g' | sed 's/\([0-9]\)[a-zA-Z]/\1/g'; echo ''; done | sed 's/^[ \t]*//;s/[ \t]*$//;/^$/d' | awk 'int($3) {print $0}'

More organized...
Code:
for num in $(cat 1.txt|awk '{print $1}'); do 
    # grep - Pull lines from relevant files and output them
    # Will be in the format '<filename>:<line>
    # Trim the newline so it's all one one-line for each entry.
    # Use sed to remove the filenames from the line
    # We'll have a <number>a <number2> -  sed removes the letter after the number
    grep "^$num" [21].txt | \
    tr -d '\n' | \
    sed 's/\([0-9].txt:\)//g' | \
    sed 's/\([0-9]\)[a-zA-Z]/\1/g'
    #We removed CRs, add one back to delimit EOL
    echo ' '
done | \
    sed 's/^[ \t]*//;s/[ \t]*$//;/^$/d' | \
    awk 'int($3) {print $0}'
    # Use sed to trim all leading/trailing whitespace and delete empty lines
    # Use awk to only print lines that have a 3rd parameter, IE it was found in both files.

That hard coded in a few files will give you what you'd want, just change "[21].txt" to the proper regex or hardcoded names (and this should support 2 or 3 file comparisons as written, just change hardcoded lines). If that isn't enough to get you going I'll try to check back in later this afternoon for anything else.

Edit: What I see running the above with the same text files you gave:
Code:
$ for num in $(cat 1.txt|awk '{print $1}'); do grep "^$num" [21].txt | tr -d '\n' | sed 's/\([0-9].txt:\)//g' | sed 's/\([0-9]\)[a-zA-Z]/\1/g'; echo ' '; done | sed 's/^[ \t]*//;s/[ \t]*$//;/^$/d' | awk 'int($3) {print $0}'
a 10 21
b 11 22
c 12 23
g 16 24
h 17 25
i 18 26


Last edited by Vryali; 07-16-2012 at 02:34 PM.. Reason: Clarity
This User Gave Thanks to Vryali For This Post:
# 3  
Old 07-16-2012
If your real data is, like your sample data, sorted on the first field (the join field):
Code:
join f1 f2 > f12
join f1 f3 > f13
join f2 f3 > f23
join f12 f3 > f123

Running join that many times may seem unappealing, but if each file has something on the order of 1000 records (and not a million), performance probably won't be an issue.

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
# 4  
Old 07-16-2012
Quote:
Originally Posted by Vryali
I've got to step out, so can't actually finish this, but I got the bulk of if basically done, just needs some adaptation and to be thrown in a shell script that takes parameters to be done cleaner.

Code:
for num in $(cat 1.txt|awk '{print $1}'); do grep "^$num" [21].txt | tr -d '\n' | sed 's/\([0-9].txt:\)//g' | sed 's/\([0-9]\)[a-zA-Z]/\1/g'; echo ''; done | sed 's/^[ \t]*//;s/[ \t]*$//;/^$/d' | awk 'int($3) {print $0}'

More organized...
Code:
for num in $(cat 1.txt|awk '{print $1}'); do 
    # grep - Pull lines from relevant files and output them
    # Will be in the format '<filename>:<line>
    # Trim the newline so it's all one one-line for each entry.
    # Use sed to remove the filenames from the line
    # We'll have a <number>a <number2> -  sed removes the letter after the number
    grep "^$num" [21].txt | \
    tr -d '\n' | \
    sed 's/\([0-9].txt:\)//g' | \
    sed 's/\([0-9]\)[a-zA-Z]/\1/g'
    #We removed CRs, add one back to delimit EOL
    echo ' '
done | \
    sed 's/^[ \t]*//;s/[ \t]*$//;/^$/d' | \
    awk 'int($3) {print $0}'
    # Use sed to trim all leading/trailing whitespace and delete empty lines
    # Use awk to only print lines that have a 3rd parameter, IE it was found in both files.

That hard coded in a few files will give you what you'd want, just change "[21].txt" to the proper regex or hardcoded names (and this should support 2 or 3 file comparisons as written, just change hardcoded lines). If that isn't enough to get you going I'll try to check back in later this afternoon for anything else.

Edit: What I see running the above with the same text files you gave:
Code:
$ for num in $(cat 1.txt|awk '{print $1}'); do grep "^$num" [21].txt | tr -d '\n' | sed 's/\([0-9].txt:\)//g' | sed 's/\([0-9]\)[a-zA-Z]/\1/g'; echo ' '; done | sed 's/^[ \t]*//;s/[ \t]*$//;/^$/d' | awk 'int($3) {print $0}'
a 10 21
b 11 22
c 12 23
g 16 24
h 17 25
i 18 26

Vryali,

Thanks for ur time, patience and interest. You proved the core value of this forum.

I will see how this solution is working and will post the outcome too.

Please look into it to make it generate all requested files.

I am a novice in coding as I come from the life sciences shop.

Thanks a ton for all ur support.
# 5  
Old 07-16-2012
You'll probably just want to look at Alister's join, I didn't realize the command existed, so new info for me, too.
This User Gave Thanks to Vryali For This Post:
# 6  
Old 07-16-2012
Quote:
Originally Posted by alister
If your real data is, like your sample data, sorted on the first field (the join field):
Code:
join f1 f2 > f12
join f1 f3 > f13
join f2 f3 > f23
join f12 f3 > f123

Running join that many times may seem unappealing, but if each file has something on the order of 1000 records (and not a million), performance probably won't be an issue.

Regards,
Alister
Hahahahahahahahah!!!!

Experience is what you get when you dont get what you want

Sometimes such experiences will leave a golden foot print in others lifes.

Thanks a bunch Allister. It worked like a charm. I re-learned today that simple things are the ones you have to approach before starting any complex issues.

Thanks Vryali for all ur efforts.
# 7  
Old 07-23-2012
Hi,

I asked you for the common records. The Join command worked fine.

Could someone let me know with how to extract the uncommon ones into a separate file?

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

Venn Data Maker

Hi, My input is like this head input.txt Set1,Set2,Set3 g1,g2,g3 g2,g1,g3, g4,g5,g5 g1,g1,g1, g2,g1,g1, g6,g7,g8 ,g7,g8 ,,g8 My output file should be Name,Set1,Set2,Set3 g1,1,1,1 (18 Replies)
Discussion started by: jacobs.smith
18 Replies

2. Shell Programming and Scripting

Substitute from awk results

Hi, The following awk command : asmcmd lsdg | awk '{print $13;}' | grep -i ${SID} return the following output . An Empty line + two lines contain "/" at the end of the line INDEVDATA/ INDEVFRA/ I need to remove the "/" as well as the empty line. Please advise Thanks (3 Replies)
Discussion started by: Yoav
3 Replies

3. Hardware

hardware diagram / database

Hi - we are looking for a (hopefully free/opensource) solution for diagramming our rack/hardware configuration. the rack solution seems easier to find than the hardware piece. i.e. on our IBM 770 with two CEC's, a method of noting what hardware points to what... for example, on the primary CEC,... (0 Replies)
Discussion started by: TinWalrus
0 Replies

4. Shell Programming and Scripting

Loop through awk results

I have files structured in stanzas, whose title is '', and the rest couples of 'id: value'. I need to find text within the title and return the whole stanzas that match the title. The following works: awk 'BEGIN{RS="";IGNORECASE=1}/^\/' myfileI would need to count all of the occurences, though,... (7 Replies)
Discussion started by: hermes14
7 Replies

5. Shell Programming and Scripting

Output of AWK Results

In the following line The AWK statement parses through a listing for files and outputs the results using the {print} command to the screen. Is there a way to (a) send the output to a file and (b) actually perform a cp cmd to copy the listed files to another directory? ls | awk -va=$a -vb=$b... (1 Reply)
Discussion started by: rdburg
1 Replies

6. Shell Programming and Scripting

need a little help with results from awk

Hi there all, I am using a line to get some replys from my PS I do ps -ef |awk '{printf $9}' But my result is 1 big line. No spaces between the lines or someting for example:... (2 Replies)
Discussion started by: draco
2 Replies

7. Shell Programming and Scripting

Wierd results with awk

Hey, I'm trying to use awk for some simple file manipulations but i'm getting soem wierd results. So i want to open up a file which looks like this: @relation 'autoMpg' @attribute a numeric @attribute b numeric @attribute c numeric @data -1.170815,0.257522,0.016416... (2 Replies)
Discussion started by: amatheny
2 Replies

8. UNIX for Advanced & Expert Users

Process diagram for 50+ unix scripts

Can someone point me to a good book for drawing a process diagram for 50+ unix scripts? The scripts run at different frequencies, and do everything from putting and getting data via FTP, to database I/O, to checking disk space ... etc. (0 Replies)
Discussion started by: tomstone_98
0 Replies
Login or Register to Ask a Question