Script to extract/compare from two files.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to extract/compare from two files.
# 1  
Old 01-16-2014
Lightbulb Script to extract/compare from two files.

I have two files : Alpha and Beta.
The files are as follows (without arrow marks.)

Alpha:
Code:
A    1
D    90
G    11
B    24
C    15

Beta:
Code:
B    24
C    0    <--
G    11
D    20   <--
A    4    <--
E    777  <--

Expected output of the script :
Alpha:
Code:
C    15
A    1
D    20
E    Not found

Beta:
Code:
C    0
D    20
A    4
E   777

How can I achieve this. ?
I used to write bash script. Now I need the simplest logic to do this

Last edited by Scott; 01-16-2014 at 08:19 AM.. Reason: Please use code tags
# 2  
Old 01-16-2014
Hello,

Following may help in same.

Code:
> lesser_values
> greater_values
a=0
v=""
while read line
do
value_first_column=`echo $line | awk '{print $1}'`
value_sec_column=`echo $line | awk '{print $2}'`
value_search=`cat beta | grep "$value_first_column" | awk '{print $2}'`
#echo $value_search
if [[ $value_sec_column -gt $value_search ]]
then
echo $line >> greater_values
echo $value_first_column" "$value_search >> lesser_values
else
if [[ $value_sec_column -eq  $value_search ]]
then
a1=0
fi
echo $value_first_column" "$value_search >> greater_values
echo $line >> lesser_values
fi
search_first_column=`cat beta | grep "$value_first_column"`
#echo $search_first_column
if [[  "$search_first_column" ==  " " ]]
then
echo $line >> lesser_values
fi
done < "alpha"
while read line2
do
value=`echo $line2 | awk '{print $1}'`
search_value=`cat alpha | grep "$value"`
if [[ -z $search_value ]]
then
echo $value "NOT find" >> greater_values
echo $line2 >> lesser_values
fi

done < "beta"
echo "alpha..."
cat greater_values
echo "beta.."
cat lesser_values


Output will be as follows.

Code:
$ ksh check_tot.ksh
alpha...
A 4
D 90
G 11
B 24
C 15
E NOT find
beta..
A 1
D 20
G 11
B 24
C 0
E 777


kindly let us know in case you have any queries.



Thanks,
R. Singh
# 3  
Old 01-16-2014
The output from RavinderSingh13's script doesn't come close to the requested output for either file, but the original post in this thread needs to give an English description of the processing used to produce the desired outputs from the given inputs. There is no explanation given that describes which lines are supposed to be copied, changed, or deleted in the output files???
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare 2 files and extract the data which is present in other file - awk is not working

file2 content f1file2 content f1,1,2,3,4,5 f1,2,4,6,8,10 f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; !($1 in a)' file2 file1output f10,1,2,3,4,5 f10,2,4,6,8,10 f5,1,2,3,4,5 f5,2,4,6,8,10awk 'FNR==NR{a;next}; ($1 in a)' file2 file1output nothing... (4 Replies)
Discussion started by: gksenthilkumar
4 Replies

2. Shell Programming and Scripting

Compare two files and extract info

Hello, I have two files which look like this File 1 Name test1 status P Gene1 0.00236753 1 1.00E-01 Gene2 0.134187 2 2.00E-01 Gene3 0.000608716 2 3.00E-01 Gene4 0.0016234 1 4.00E-01 Gene5 0.000665868 2 5.00E-01and file 2 No Pos ... (2 Replies)
Discussion started by: nans
2 Replies

3. Shell Programming and Scripting

Compare two files and extract

Assume we have two files - FileA and FileB. Content of files are as shown below : FileA:1001,value1,value4,value8,value9 1002,value4,value32,value46,value33 1503,value5,value45,value68,value53 1605,value4,value67,value56,value57 1073,value5,value45,value68,value53... (3 Replies)
Discussion started by: alnhk
3 Replies

4. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

5. Shell Programming and Scripting

Compare files & extract column awk

I have two tab delimited files as given below: File_1: PV16 E1 865 2814 1950 PV16 E2 2756 3853 1098 PV16 E4 3333 3620 288 PV16 E5 3850 4101 252 PV16 E6 83 559 477 PV16 E7 562 858 297 PV16 L2 4237 5658 ... (10 Replies)
Discussion started by: vaibhavvsk
10 Replies

6. Shell Programming and Scripting

Compare multiple files, and extract items that are common to ALL files only

I have this code awk 'NR==FNR{a=$1;next} a' file1 file2 which does what I need it to do, but for only two files. I want to make it so that I can have multiple files (for example 30) and the code will return only the items that are in every single one of those files and ignore the ones... (7 Replies)
Discussion started by: castrojc
7 Replies

7. 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

8. Shell Programming and Scripting

compare 2 files and extract the data which is not present in other file with condition

I have 2 files whose data's are as follows : fileA 00 lieferungen 00 attractiop 01 done 02 forness 03 rasp 04 alwaysisng 04 funny 05 done1 fileB alwayssng dkhf fdgdfg dfgdg sdjkgkdfjg funny rasp (7 Replies)
Discussion started by: rajniman
7 Replies

9. Shell Programming and Scripting

Compare Records between to files and extract it

I am not an expert in awk, SED, etc... but I really hope there is a way to do this, because I don't want to have to right a program. I am using C shell. FILE 1 FILE 2 H0000000 H0000000 MA1 MA1 CA1DDDDDD CA1AAAAAA MA2 ... (2 Replies)
Discussion started by: jclanc8
2 Replies

10. Shell Programming and Scripting

script to compare files

HI i wil get input from sql query and that too i can get a list o f files or just one. i have to pick up a file from another directory which hads prefix to this prefix.x.x.x.x.x. And we have to discard prefix and use that file name. we have to compare this file name(no need... (0 Replies)
Discussion started by: pulse2india
0 Replies
Login or Register to Ask a Question