Kindly check it: Camparison of files only column1 of 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Kindly check it: Camparison of files only column1 of 2 files
# 15  
Old 07-30-2012
Request to check

Hi

I tried this but ourput is not accodring to expected
[
Code:
bash-3.2$ awk 'NR==FNR{
>                 a[$1]=$0
>         }
> 
> NR!=FNR{
>         n=split($1,b,",");
>         for(i=1;i<=n;i++)
>                 {       if(a[b[i]])
>                                 {
>                                         print a[b[i]]" "$2" "$3
>                                 }
>                 }
>         }' saragenename4 TTDtargets2.txt >sararoughttddrugs5
bash-3.2$

The output is just like
Quote:
1
1
1
1
1
2
2
2
2
2
its just repeating the common entries of first columns i think and that also doesnt cotain dat of column2,3, it's blank in fornt of 1,2....
# 16  
Old 08-01-2012
try this (copy in file and run)

Code:
awk '
NR==FNR&&NF>1{
		x=$1;
		for(i=2;i<NF;i++)
				{
					x=substr(x,1)":"substr($i,1);
				};
		a[x]=$NF
	}
(NR!=FNR&&NF>1){
		x=$1;
		for(i=2;i<NF-2;i++)
				{
					x=substr(x,1)":"substr($i,1);
				};
		if(a[x])
				{
					n=split(a[x],b,":");
					if(n==1)
						{
							a[x]=substr(a[x],1)":"substr($NF-2,1)":"substr($NF-1,1)":"substr($NF,1);
						}
					else
						{
							f1=substr(b[2],1)","substr($NF-2,1);
							f2=substr(b[3],1)","substr($NF-1,1);
							f3=substr(b[4],1)","substr($NF,1);
							a[x]=substr(b[1],1)":"substr(f1,1)":"substr(f2,1)":"substr(f3,1);
						}
				}
	}END{
		while(getline < "file2")
			{
				x=$1;
				for(i=2;i<NF;i++)
					{
						x=substr(x,1)":"substr($i,1);
					};
				if(a[x])
					{
						n1=split(a[x],b,":");
						n2=split(x,c,":");
						if(n1>1)
							{
								for(j=1;j<=n2;j++)
									{
										printf c[j]" ";
									};
								print b[1]" "b[2]" "b[3]" "b[4];
							}
					}
			}
	}
' file2 file1

and output is

Code:
Mani Grover z34 1,3,4 2,4,5 3,5,6
Harsh Grover z44 4 5 6

Here assumption is made that in file2 the names entries are unique
This User Gave Thanks to raj_saini20 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get CRC check sum of files in java EAR file without extracting .jar/.war files to disk.?

unzip -v gives CRC info of each file in a zip(in my case .EAR) file. # unzip -v my-application.ear Archive: my-application.ear Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 197981 Defl:N 183708 7%... (1 Reply)
Discussion started by: kchinnam
1 Replies

2. Shell Programming and Scripting

Check files and archive the files using sftp

Hi, I have to check the files in another server using sftp to do that, below is the code i am going with #!/bin/bash export SRC_FOLDER=$1 export ARC_FOLDER=$2 HOST=as07u3456 USER=relfag sftp ${USER}@${HOST} <<EOF cd $SRC_FOLDER/DSCOR ls bye EOF echo "done" whatever the files i... (8 Replies)
Discussion started by: ursrami
8 Replies

3. Shell Programming and Scripting

common entries of first column in 2 or 3 files:kindly check

Hi all, I have 3 files with such data first files second file third file I have to find common entries of first column in two ways 1) between 2 files (2 Replies)
Discussion started by: manigrover
2 Replies

4. Shell Programming and Scripting

Kindly check:remove duplicates with similar data in front of it

Hi all, I have 2 files containing data like this: so if there is same entry repeated in the column like1,2,3,4 I have to check if there is different entries column like 2,4 but similar entries for duplicatein column 2 like1,3 the output shuld be like this for first file ... (5 Replies)
Discussion started by: manigrover
5 Replies

5. Shell Programming and Scripting

Perl code to check date and check files in particular dir

Hi Experts, I am checking how to get day in Perl. If it is “Monday” I need to process…below is the pseudo code. Can you please prove the code for below condition. if (today=="Monday" ) { while (current_time LESS THAN 9:01 AM) ... (1 Reply)
Discussion started by: ajaypatil_am
1 Replies

6. Shell Programming and Scripting

need a shell script to extract the files from source file and check whether those files existonserve

Hi, I am new to shell scripting.Please help me on this.I am using solaris 10 OS and shell i am using is # echo $0 -sh My requirement is i have source file say makefile.I need to extract files with extensions (.c |.cxx |.h |.hxx |.sc) from the makefile.after doing so i need to check whether... (13 Replies)
Discussion started by: muraliinfy04
13 Replies

7. Shell Programming and Scripting

How to change value in CSV columns and compare two files where Column1 is identical

Hi all, Could someone help me with the following issue: 1st I have an CSV file delimiter is ";" I I have a column 7 where I need to do some multiple mathem. operation, I need all values in this columns to be multiplied by 1.5 and create a new CSV file with the replaced values. 2nd. I... (3 Replies)
Discussion started by: kl1ngac1k
3 Replies

8. Shell Programming and Scripting

How to check files and move the results to differents files?

Hi, I am a newbie to shell scripting. here is my objective: 1)The shell program should take 2 parameters - ie-> DestinationFolder, WebFolder 2)Destination folder contains few files that has to has be verified and deleted. 3)WebFolder is a folder containing a list of master files 4)It... (1 Reply)
Discussion started by: sandhyagupta
1 Replies

9. Shell Programming and Scripting

check how many files in folder or total files in folder

Hi all, I have copied all my files to one folder.and i want to check how many files (count) in the folder recently moved or total files in my folder? please send me the query asap. (3 Replies)
Discussion started by: durgaprasad
3 Replies

10. Shell Programming and Scripting

comparing 2 files - kindly assist expert

Hi, I'm new in shell scripting and would like u guys to help on this. OS: Solaris 9 currently, i have 2 files. file1: hello hi test god file2: hi hello file1 is my masterfile, and i wanted to print out string that are in file1 and NOT in file2 and send out a mail (with the... (15 Replies)
Discussion started by: chew
15 Replies
Login or Register to Ask a Question