Shell script (sh file) logic to compare contents of one file with another file and output to file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script (sh file) logic to compare contents of one file with another file and output to file
# 1  
Old 07-14-2018
Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic

Hi

I have 2 input files like with file 1 content as (file1)

Code:
"BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt

File 2 contents as fle(2)

Code:
"BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt

I want to get final output as (File 3)

Code:
BRGTEST-244 is depdendent on BRGTEST-242 for file a.txt

BRGTEST-244 is depdendent on BRGTEST-240 for file a.txt

BRGTEST-221 is dependent on BRGTEST-219 for the file e.txt

How to write the shell script logic for this .I tried using awk but could not get the result yet.

Thanks

Chaitanya

Last edited by Scrutinizer; 07-15-2018 at 12:41 AM.. Reason: code tags
# 2  
Old 07-15-2018
What operating system are you using?

What shell are you using?

What logic is used to determine that one test is dependent on another test?

Please show us the awk code that you have tried (in CODE tags) and show us the output it produces (also in CODE tags).
# 3  
Old 07-15-2018
Below is the code I have

Code:
cat $baseDir/File1 | while read line
do
 IFS='
'
JIRAID=`echo $line | grep -Eo '([A-Z]{3,}-)([0-9]+)'`
cat $baseDir/File2 | while read line2
do
ParentfileNameIs=`echo $line2 | awk '{print $2}'`
ParentJIRAIDIS=`echo $line2 | awk '{print $1}'`
ParentID=`echo $ParentJIRAIDIS | tr -d '"'`
if grep -q $ParentfileNameIs "$baseDir/File1"; then
echo "$ParentID is dependent on $JIRAID  for file $ParentfileNameIs" >> $baseDir/File3
awk '!seen[$0]++'  $baseDir/File3 >  $baseDir/UniqueFile3	
fi
done
done

Here is the Output I am getting
Code:
BRGTEST-244 is depdendent on BRGTEST-242 for file a.txt

BRGTEST-244 is depdendent on BRGTEST-240 for file a.txt

BRGTEST-221 is dependent on BRGTEST-219 for the file e.txt

BRGTEST-244 is dependent on BRGTEST-219 for the file e.txt

BRGTEST-221  is dependent on  BRGTEST-242 for file a.txt

BRGTEST-221 is dependent on file BRGTEST-240 for file a.txt


Last edited by Don Cragun; 07-15-2018 at 03:16 AM.. Reason: Add CODE tags.
# 4  
Old 07-15-2018
I asked you four questions. You answered only the fourth one.

Please answer the other three questions. Without a clear answer to the third question we don't stand a chance of being able to help you. Without an answer to the first two questions, there is an good chance that we will suggest solution that will not work in your environment. Please help us help you by answering all of the questions!
# 5  
Old 07-15-2018
What operating system are you using?

I am using red hat linux

What shell are you using?

I am running .sh file

What logic is used to determine that one test is dependent on another test?

Code:
cat $baseDir/File1 | while read line
do
 IFS='
'
JIRAID=`echo $line | grep -Eo '([A-Z]{3,}-)([0-9]+)'`
cat $baseDir/File2 | while read line2
do
ParentfileNameIs=`echo $line2 | awk '{print $2}'`
ParentJIRAIDIS=`echo $line2 | awk '{print $1}'`
ParentID=`echo $ParentJIRAIDIS | tr -d '"'`
if grep -q $ParentfileNameIs "$baseDir/File1"; then
echo "$ParentID is dependent on $JIRAID  for file $ParentfileNameIs" >> $baseDir/File3
awk '!seen[$0]++'  $baseDir/File3 >  $baseDir/UniqueFile3	
fi
done
done


Last edited by Don Cragun; 07-15-2018 at 03:29 AM.. Reason: Add CODE tags again.
# 6  
Old 07-15-2018
Quote:
Originally Posted by pottic
What operating system are you using?

I am using red hat linux
Thank you.

Quote:
What shell are you using?

I am running .sh file
.sh is not a shell. .sh is a common filename extension used on Windows systems to indicate that the contents of the file are a shell script. But, since you're using Red Hat Linux instead of Windows, we don't know if you're using bash (or sh that is often a link to bash with several built-in utilities and syntax features that behave differently), dash, ksh, zsh, or one of several other shells that might or might not be available on your system.
Quote:
What logic is used to determine that one test is dependent on another test?

Code:
cat $baseDir/File1 | while read line
do
 IFS='
'
JIRAID=`echo $line | grep -Eo '([A-Z]{3,}-)([0-9]+)'`
cat $baseDir/File2 | while read line2
do
ParentfileNameIs=`echo $line2 | awk '{print $2}'`
ParentJIRAIDIS=`echo $line2 | awk '{print $1}'`
ParentID=`echo $ParentJIRAIDIS | tr -d '"'`
if grep -q $ParentfileNameIs "$baseDir/File1"; then
echo "$ParentID is dependent on $JIRAID  for file $ParentfileNameIs" >> $baseDir/File3
awk '!seen[$0]++'  $baseDir/File3 >  $baseDir/UniqueFile3	
fi
done
done

Showing us code that does not produce the output you want does not explain the logic that is supposed to be used to determine what things are dependent on other things in your desired output.

Please explain, in English, the logic that you want to use to determine dependencies.
# 7  
Old 07-15-2018
So lets say I have file 1 with contents as shown below

Code:
"BRGTEST-242" a.txt 
"BRGTEST-240"  b.txt 
"BRGTEST-219" c.txt

I have file 2 with below contents

Code:
"BRGTEST-244" a.txt 
"BRGTEST-244" b.txt 
"BRGTEST-231" c.txt 
"BRGTEST-231" d.txt 
"BRGTEST-221" e.txt

Desired output is

Code:
BRGTEST-244 is depdendent on BRGTEST-242 for file a.txt

BRGTEST-244 is depdendent on BRGTEST-240 for file a.txt

BRGTEST-221 is dependent on BRGTEST-219 for the file e.txt






So in simple english terms below is my requirement

I want to compare all filename in file 2 against file and see if there is a match.

If there is a match

I want to print the id from file2 is dependent on id from file 1 for the filename matched.I dont want any duplicates printed in my final output file 3.

Currently I am getting below output


Code:
BRGTEST-244 is depdendent on BRGTEST-242 for file a.txt

BRGTEST-244 is depdendent on BRGTEST-240 for file a.txt

BRGTEST-221 is dependent on BRGTEST-219 for the file e.txt

BRGTEST-244 is dependent on BRGTEST-219 for the file e.txt

BRGTEST-221  is dependent on  BRGTEST-242 for file a.txt

BRGTEST-221 is dependent on file BRGTEST-240 for file a.txt

I am getting these extra lines as part of my output which I dont want

Code:
BRGTEST-244 is dependent on BRGTEST-219 for the file e.txt
BRGTEST-221  is dependent on  BRGTEST-242 for file a.txt
BRGTEST-221 is dependent on file BRGTEST-240 for file a.txt

Moderator's Comments:
Mod Comment Please review the rules you agreed to when you joined this forum.

Moderators should not have to edit all of your posts to correctly format your posts for you. If you continue refusing to properly enclose sample input, sample output, and code segments in CODE tags in your posts, you may be banned from using this forum.

Last edited by Don Cragun; 07-15-2018 at 04:23 AM.. Reason: Add CODE tags again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell Script to Read the given file contents into a merged one file

Like to have shell script to Read the given file contents into a merged one file with header of path+file name followed by file contents into a single output file. While reading and merging the file contents into a single file, Like to keep the format of the source file. ... (4 Replies)
Discussion started by: Siva SQL
4 Replies

2. UNIX for Beginners Questions & Answers

Output file name and file contents of multiple files to a single file

I am trying to consolidate multiple information files (<hostname>.Linux.nfslist) into one file so that I can import it into Excel. I can get the file contents with cat *Linux.nfslist >> nfslist.txt. I need each line prefaced with the hostname. I am unsure how to do this. --- Post updated at... (5 Replies)
Discussion started by: Kentlee65
5 Replies

3. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies

4. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

5. Shell Programming and Scripting

script to grep a pattern from file compare contents with another file and replace

Hi All, Need help on this I have 2 files one file file1 which has several entries as : define service{ hostgroup_name !host1,!host5,!host6,.* service_description check_nrpe } define service{ hostgroup_name !host2,!host4,!host6,.* service_description check_opt } another... (2 Replies)
Discussion started by: namitai
2 Replies

6. UNIX for Advanced & Expert Users

Shell Script to compare xml files and print output to a file

All, PLease can you help me with a shell script which can compare two xml files and print the difference to a output file. I have attached one such file for you reference. <Group> <Member ID=":Year_Quad:41501" childCount="4" fullPath="PEPSICO Year-Quad-Wk : FOLDER.52 Weeks Ending Dec... (2 Replies)
Discussion started by: kanthrajgowda
2 Replies

7. Shell Programming and Scripting

shell script to compare file contents

Hello Has anyone got an example shell script that I can use to compare the contents of two files. The files should contain the same contents, eg. file1.txt apple pear grape file2.txt apple pear grape (2 Replies)
Discussion started by: deedaz
2 Replies

8. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

9. Shell Programming and Scripting

compare file size from a output file from a script

Hi guys, firstly I'm working on SunOS 5.10 Generic_125100-10 sun4u sparc SUNW,Sun-Fire-V240 I've made a script to compress two directory and then send them to an other server via ftp. This is working very well. Inside theis script I decide to log usefull data for troubleshooting in case of... (7 Replies)
Discussion started by: moustik
7 Replies

10. Shell Programming and Scripting

search for the contents in many file and print that file using shell script

hello have a file1 H87I Y788O T347U J23U and file2 J23U U887Y I99U T556U file3 I99O J99T F557J file4 N99I T666U R55Y file5 H87I T347U file6 H77U R556Y E44T file7 Y788O K98U H8I May be using script we can use file1 to search for all the files and have the output H87I file5... (3 Replies)
Discussion started by: cdfd123
3 Replies
Login or Register to Ask a Question