matching file and redirecting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting matching file and redirecting
# 1  
Old 04-28-2009
matching file and redirecting

Hi all

I have two file a.txt and b.txt and i want to compare their first coloum and print the o/p to the third file c.txt
comparing should be in such a way that if matching first coloum is not found in any of the file then it should print No row
like here AAA is present in both file so o/p file have all coloums value but BBB is not in 2n file so in o/p file 4th and the
coloum have No row entry
I am using join for matching but it prints only coloum which is in both the file
so pls help me in this
a.txt

AAA,23,34
BBB,45,67
CCC,21,33
DDD,56,78

b.txt
CCC,88,91
AAA,26,78
EEE,61,11

matching should be based on first coloum of both the file
1st coloum ,2n coloum of fisrt file,3 coloum of fist file,2n coloum of second file,3 coloum of 2n file
and if no matching coloum is found then it should be "No row"


AAA , 23 , 34 ,26 ,78
BBB , 45 , 67 ,NO row ,No row
CCC , 21 , 33 ,88 ,91
EEE , NO row , NO row ,61 ,11
DDD , 56 , 78 ,No row ,No row

pls provide me solution as it is very urgent for me

Last edited by aaysa123; 04-29-2009 at 10:37 AM..
# 2  
Old 04-29-2009
pls provide me solution as it is very urgent for me
# 3  
Old 04-29-2009
Please don't bump up posts.

nawk -F, -f aa.awk a.txt b.txt

aa.awk:
Code:
FNR==NR { a[$1]=$2 FS $3; next}
$1 in a { print a[$1] FS $2 FS $3; delete a[$1];next }
{ print "No row" FS "No row" FS $2 FS $3; delete a[$1] }
END {
  for (i in a)
     print a[i] FS "No row" FS "No row"
}

# 4  
Old 05-02-2009
Hi
I checked solution provided u by but it is not working it is giving me following op
No row,No row,88,91
No row,No row,26,78
No row,No row,61,11
56,78,No row,No row
23,34,No row,No row
21,33,No row,No row
45,67,No row,No row

But i want to have o/p
AAA , 23 , 34 ,26 ,78
BBB , 45 , 67 ,NO row ,No row
CCC , 21 , 33 ,88 ,91
EEE , NO row , NO row ,61 ,11
DDD , 56 , 78 ,No row ,No row


for above two files.
# 5  
Old 05-02-2009
Try this..

Code:
awk 'BEGIN{FS=OFS=",";n="No Row"
while((getline < "a.txt")>0)a[$1]=$2","$3
while((getline < "b.txt")>0)b[$1]=$2","$3
for(i in a){
if (i in b)print i,a[i],b[i]
else print i,a[i],n,n
}
for ( j in b)
{
if (j in a)print j,a[j],b[j]
else print j,n,n,b[j]
}
}' | sort -u


cheers,
Devaraj Takhellambam
# 6  
Old 05-02-2009
Quote:
Originally Posted by aaysa123
Hi
I checked solution provided u by but it is not working it is giving me following op
No row,No row,88,91
No row,No row,26,78
No row,No row,61,11
56,78,No row,No row
23,34,No row,No row
21,33,No row,No row
45,67,No row,No row

But i want to have o/p
AAA , 23 , 34 ,26 ,78
BBB , 45 , 67 ,NO row ,No row
CCC , 21 , 33 ,88 ,91
EEE , NO row , NO row ,61 ,11
DDD , 56 , 78 ,No row ,No row


for above two files.
slight modification:
Code:
FNR==NR { a[$1]=$2 FS $3; next}
$1 in a { print $1 FS a[$1] FS $2 FS $3; delete a[$1];next }
{ print $1 FS "No row" FS "No row" FS $2 FS $3; delete a[$1] }
END {
  for (i in a)
     print i FS a[i] FS "No row" FS "No row"
}

# 7  
Old 05-08-2009
Thanks a lot its working fine
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirecting output to file

Hi, I have created script which redirect the output to file.I am able to get the output in file but not in the format. Output :Content of the log which have 10 -15 lines. Actal :Line1 ..Line 2Line3 Line4 Line 5 Expected:Line1 Line 2 Line3 Please... (7 Replies)
Discussion started by: karthik771
7 Replies

2. Shell Programming and Scripting

awk script issue redirecting to multiple files after matching pattern

Hi All I am having one awk and sed requirement for the below problem. I tried multiple options in my sed or awk and right output is not coming out. Problem Description ############################################################### I am having a big file say file having repeated... (4 Replies)
Discussion started by: kshitij
4 Replies

3. Shell Programming and Scripting

redirecting with file descriptor

hello, Someone can help me with redirectors? I am writing this script in bash enviroment on Fedora: exec 4<> /dev/tcp/10.10.11.30/5000 #open socket in input/output strings<&4 >file.txt & I send file descriptor 4 to string command to purge data stream from special char while come from... (3 Replies)
Discussion started by: rattoeur
3 Replies

4. Shell Programming and Scripting

Redirecting STDERR to file and screen, STDOUT only to file

I have to redirect STDERR messages both to screen and also capture the same in a file but STDOUT only to the same file. I have searched in this formum for a solution, but something like srcipt 3>&1 >&2 2>&3 3>&- | tee errs doesn't work for me... Has anyone an idea??? (18 Replies)
Discussion started by: thuranga
18 Replies

5. Shell Programming and Scripting

extracting line numbers from a file and redirecting them to another file

i have applied the following command on a file named unix.txt that contains the string "linux from the text" grep -n -i -w "linux from the text" unix.txt and the result is 5:Today's Linux from the text systems are split into various branches, developed over time by AT&T as well as various... (5 Replies)
Discussion started by: arindamlive
5 Replies

6. Shell Programming and Scripting

Reading UNIX commands from file and redirecting output to a file

Hi All I have written the following script: #!/bin/ksh while read cmdline do echo `$cmdline` pid="$cmdline" done<commands.txt =========== commands.txt contains: ps -ef | grep abc | grep xyz |awk '{print $2}; My objective is to store the o/p of the command in a variable and do... (8 Replies)
Discussion started by: rahulparo
8 Replies

7. Shell Programming and Scripting

redirecting the terminal to file

Hi, I want to save the whole Output of the terminal in a file. I dont want to redirect a single command to a file (ls -l > test.txt), I want to redirect the whole last 40 lines into a file. Maybe i can read out the terminal while working with it, but i cant find a way to save the whole... (2 Replies)
Discussion started by: niratschi
2 Replies

8. Shell Programming and Scripting

Redirecting output to file

Hi, Below is the whole string which is to be redirected to the new file. su - oracle -c "exp $user/$pass file=/oracle/oradata/backup/exp_trn_tables_`date +%d_%b_20%y_%H_%M_%S`.dmp log=/oracle/oradata/backup/exp_trn_tables_`date +%d_%b_20%y_%H_%M_%S`.log tables=table1,table2 statistics=none" ... (3 Replies)
Discussion started by: milink
3 Replies

9. UNIX for Advanced & Expert Users

Redirecting to Error File

Hi- I want to redirect the output of the echo to the standard error file. We require this because, when we try to scp a file from a source server to a target server we get an error code 1. Later we found that it was due to the .cshrc file which outputs on stdout which is making the scp to fail.... (1 Reply)
Discussion started by: lorcan
1 Replies

10. Shell Programming and Scripting

redirecting a file

Hi, I have a normal txt file which contains say 5 lines. in that i need to change few of the the parameters dynamically. for eg.. line 1..contains account line 2 contains date line 3 contains user .. .. .. i will be taking the input using read command and store it in variables... (3 Replies)
Discussion started by: wip_vasikaran
3 Replies
Login or Register to Ask a Question