Join, GREP


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Join, GREP
# 1  
Old 09-25-2008
Question Join, GREP

Hi All,

I have 2 file. ACC_NUM contains only account numbers & ACC_DETAIL contains all information including account number seperated by ~ delimiter.

i am serching the account number in ACC_DETAIL from ACC_NUM. If it is exist, then copy all information in ACC_DETAIL_NEW file. For that i have written the following logic. It is working but taking too long time.

for i in 'cat ACC_NUM'
do
grep -w $i ACC_DETAIL > ACC_DETAIL_NEW
done

====

ACC Number is Alpha Numeric, which is a first column in the file. for exampple.

ACC_NUM
======
ABC4567
JUB7546

ACC_DETAIL
=========
ABC4567~ameet~bangalore~savings
UIO8907~rakesh~delhi~xxxxx
JUB7546~aup~mangalore~insurance

Now we need to copy following information into ACC_DETAIL_NEW
======
ABC4567~ameet~bangalore~savings
JUB7546~aup~mangalore~insurance

As grep will search entire file for each Account Number. It is taking time. Could comeone suggest other way to do effectively. ie by using AWK or JOIN. I have tried using JOIN, but i not expert in UNIX & no0t getting result.
# 2  
Old 09-25-2008
ACC_NUM has 6780 record (ie account number) & ACC_DETAIL has 20000 records
# 3  
Old 09-25-2008
Look like a "homework", please read The UNIX and Linux Forums - Forum Rules
Tip: Search the forum for "compare file awk"
# 4  
Old 09-26-2008
join Grep

Try using the join commd, first sort both the files on the field which is
common in both files.
so in your case the account number which is alpha numeric.

sort -d ACC_NUM > a.num
sort -d ACC_DETAIL +0 -1 > acc.num
join -1 1 -2 1 -t~ a.num acc.num -o 2.1 2.2 2.3 2.4 > output

To make the join commd you must sort the files on the key value.

Cheers
# 5  
Old 09-29-2008
Hi Mahesh,

Thanks for your reply. It is working fine. But i also need to know some of the command option you have given below.

sort -d : is used to sort alpha-numeric filed. But also you have given +0 -1 option. What does it mean?

join -1 1 -2 1 -t~ a.num acc.num -o 2.1 2.2 2.3 2.4 > output : This coomand is working fine. But in file2, i have more than 4 column (ie 10 columns). If i do not write 2.1....2.4 or 2.10 will it work same? is there any other option where we can point out all column in file2 for join?

Please advise me so that i can understand it properly
# 6  
Old 09-30-2008
Bug join,GREP

Dear Amit,
The command : sort -d +0 -1 :-> it says to sort the input file on the field no
one (1) in directory order
. The first field in your case is the key field and
that is alpha numeric hence we require to sort on directory order.

Secondly the number 2.1 2.2 2.3 etc are indicative of the field we require
in the output file.The same as per your requirement and you may alter to
suit your need.

I hope the doubts are cleared.
# 7  
Old 10-03-2008
Hi Mahesh,

Thanks for your help. It works fine for me. It takes only 25 seconds to complete my task compare to 5 hrs with grep.

But still there is issue with me.

a.num contains 13155 account number
acc.num contains 660469 account's data. it has 75 columns with delimiter ~

while issuing command

join -1 1 -2 1 -t~ a.num acc.num > output, there is difference of 3 accounts.

in output file, total number of records should be 13155. but it shows only 13152.

Both files are in sorted format. Also i have checked all these 3 acc number in acc.num file. It is available in both.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join, merge, fill NULL the void columns of multiples files like sql "LEFT JOIN" by using awk

Hello, This post is already here but want to do this with another way Merge multiples files with multiples duplicates keys by filling "NULL" the void columns for anothers joinning files file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: 1|123|jojo 1|NULL|bibi... (2 Replies)
Discussion started by: yjacknewton
2 Replies

2. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

3. Shell Programming and Scripting

Join two commands sed and grep

Hi all, I have two separate commands which I would like to join. Basically, I want to match a line and insert a character at the end of the previous line to the matched line Here is what I have gotgrep -B1 '^>' sed 's/$/*/' Any help is much appreciated thanks (5 Replies)
Discussion started by: kaav06
5 Replies

4. UNIX for Dummies Questions & Answers

How to use the the join command to join multiple files by a common column

Hi, I have 20 tab delimited text files that have a common column (column 1). The files are named GSM1.txt through GSM20.txt. Each file has 3 columns (2 other columns in addition to the first common column). I want to write a script to join the files by the first common column so that in the... (5 Replies)
Discussion started by: evelibertine
5 Replies

5. UNIX for Dummies Questions & Answers

how to join two files using "Join" command with one common field in this problem?

file1: Toronto:12439755:1076359:July 1, 1867:6 Quebec City:7560592:1542056:July 1, 1867:5 Halifax:938134:55284:July 1, 1867:4 Fredericton:751400:72908:July 1, 1867:3 Winnipeg:1170300:647797:July 15, 1870:7 Victoria:4168123:944735:July 20, 1871:10 Charlottetown:137900:5660:July 1, 1873:2... (2 Replies)
Discussion started by: mindfreak
2 Replies

6. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

7. UNIX for Dummies Questions & Answers

Join 2 files with multiple columns: awk/grep/join?

Hello, My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns: File A: (tab-delimited) PDB CHAIN Start End Fragment 1avq A 171 176 awyfan 1avq A 172 177 wyfany 1c7k A 2 7... (3 Replies)
Discussion started by: InfoSeeker
3 Replies

8. Programming

sql,multiple join,outer join issue

example sql: select a.a1,b.b1,c.c1,d.d1,e.e1 from a left outer join b on a.x=b.x left outer join c on b.y=c.y left outer join d on d.z=a.z inner join a.t=e.t I know how single outer or inner join works in sql. But I don't really understand when there are multiple of them. can... (0 Replies)
Discussion started by: robbiezr
0 Replies

9. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

10. Shell Programming and Scripting

join (pls help on join command)

Hi, I am a new learner of join command. Some result really make me confused. Please kindly help me. input: file1: LEO oracle engineer 210375 P.Jones Office Runner ID897 L.Clip Personl Chief ID982 S.Round UNIX admin ID6 file2: Dept2C ID897 6 years Dept5Z ID982 1 year Dept3S ID6 2... (1 Reply)
Discussion started by: summer_cherry
1 Replies
Login or Register to Ask a Question