write a script to compare two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting write a script to compare two files
# 1  
Old 02-02-2009
write a script to compare two files

Hi all,
i am new to unix,i have never worked on scripting and all,i am learning now and i have to write a script to compare two files.
The requirement is like : in the first file i am searching for a word and after i get that word i have to select everything from the rest of the file and redirect to some output file.the second part is to compare the output file with my second file.the second part is feasible for me to do,but i am facing problem to proceed with the first part,Could anyone help me.Smilie
# 2  
Old 02-02-2009
For the first part you can use sed:

sed -n '/pattern/,$p' file

Regards
# 3  
Old 02-02-2009
Please refer the link, this might help you.

https://www.unix.com/shell-programmin...d1-field2.html

Thanks.
# 4  
Old 02-03-2009
Hi all,thanks for ur help but i think i couldn't get what i needed.
Here is the sample file eg:abc.txt
<servergroup id="8">
<application value="OnlineDisputes"></application>
<minheap value="256"></minheap>
<maxheap value="512"></maxheap>
<httpport value="51058"></httpport>
<additionalcmdlineargs value="-DSLConfigFile=app_servicelocator.xml -verbosegc -Xss8m"/>
</servergroup>
<servergroup id="9">
<application value="Transfer"></application>
<minheap value="256"></minheap>
<maxheap value="512"></maxheap>
<httpport value="51059"></httpport>
<additionalcmdlineargs value="-DSLConfigFile=app_servicelocator.xml -verbosegc -Xrunheappro
file"/>
</servergroup>
<servergroup id="10">
<application value="INTLEStatement"></application>
<minheap value="256"></minheap>
<maxheap value="512"></maxheap>
<httpport value="51063"></httpport>
<additionalcmdlineargs value="-DSLConfigFile=app_servicelocator.xml -verbosegc
-XX:MaxPermSize=128m"/>
</servergroup>
</servergroup-info>
<clone-info>
<clone id="1">
<application value="EnterpriseServices"></application>
<cloneindex value="1"></cloneindex>
<node value="spdwa013"></node>
<httpport value="21050"></httpport>
<workingdir value="/tmp"></workingdir>
</clone>
<clone id="2">
<application value="GlobalMR"></application>
<cloneindex value="1"></cloneindex>
<node value="spdwa013"></node>
<httpport value="21051"></httpport>
<workingdir value="/tmp"></workingdir>
</clone>
<clone id="3">
<application value="GlobalUserManagement"></application>
<cloneindex value="1"></cloneindex>
<node value="spdwa013"></node>
<httpport value="21052"></httpport>
<workingdir value="/tmp"></workingdir>
</clone>
<clone id="4">
<application value="USAccountSummary"></application>
<cloneindex value="1"></cloneindex>
<node value="spdwa013"></node>
<httpport value="21053"></httpport>
<workingdir value="/tmp"></workingdir>
</clone>

In the above file i need to select everything below <clone-info>
and redirect it to an output file.
I would be very greatful if anybody can help me in this regard.
# 5  
Old 02-03-2009
Quote:
Originally Posted by usha rao
Hi all,thanks for ur help but i think i couldn't get what i needed.
You 'think' or are you sure?
What is it that you 'know' that you want as the output?
And how's that compare to the 'sed' solution provided?

Quote:
Originally Posted by usha rao
Here is the sample file eg:abc.txt
<servergroup id="8">
<application value="OnlineDisputes"></application>
<minheap value="256"></minheap>
<maxheap value="512"></maxheap>
<httpport value="51058"></httpport>
<additionalcmdlineargs value="-DSLConfigFile=app_servicelocator.xml -verbosegc -Xss8m"/>
</servergroup>
<servergroup id="9">
<application value="Transfer"></application>
<minheap value="256"></minheap>
<maxheap value="512"></maxheap>
<httpport value="51059"></httpport>
<additionalcmdlineargs value="-DSLConfigFile=app_servicelocator.xml -verbosegc -Xrunheappro
file"/>
</servergroup>
<servergroup id="10">
<application value="INTLEStatement"></application>
<minheap value="256"></minheap>
<maxheap value="512"></maxheap>
<httpport value="51063"></httpport>
<additionalcmdlineargs value="-DSLConfigFile=app_servicelocator.xml -verbosegc
-XX:MaxPermSize=128m"/>
</servergroup>
</servergroup-info>
<clone-info>
<clone id="1">
<application value="EnterpriseServices"></application>
<cloneindex value="1"></cloneindex>
<node value="spdwa013"></node>
<httpport value="21050"></httpport>
<workingdir value="/tmp"></workingdir>
</clone>
<clone id="2">
<application value="GlobalMR"></application>
<cloneindex value="1"></cloneindex>
<node value="spdwa013"></node>
<httpport value="21051"></httpport>
<workingdir value="/tmp"></workingdir>
</clone>
<clone id="3">
<application value="GlobalUserManagement"></application>
<cloneindex value="1"></cloneindex>
<node value="spdwa013"></node>
<httpport value="21052"></httpport>
<workingdir value="/tmp"></workingdir>
</clone>
<clone id="4">
<application value="USAccountSummary"></application>
<cloneindex value="1"></cloneindex>
<node value="spdwa013"></node>
<httpport value="21053"></httpport>
<workingdir value="/tmp"></workingdir>
</clone>

In the above file i need to select everything below <clone-info>
and redirect it to an output file.
I would be very greatful if anybody can help me in this regard.
# 6  
Old 02-03-2009
Franklin's solution works with slight modification:

Code:
sed -n '/<clone-info>/,$p' file_in > file_out

# 7  
Old 02-03-2009
SmilieSmilieHey all, Thanks a lot..!!!
Its working now..!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to open files and write into new one

Hello! I am a real beginner in scripting, so I am struggling with a really easy task! I want to write a script to concatenate several text files onto each other and generate a new file. I wanted the first argument to be the name of the new file, so: ./my_script.sh new_file file1.txt... (5 Replies)
Discussion started by: malajedala
5 Replies

2. Shell Programming and Scripting

Compare two files and write data to second file using awk

Hi Guys, I wanted to compare a delimited file and positional file, for a particular key files and if it matches then append the positional file with some data. Example: Delimited File -------------- Byer;Amy;NONE1;A5218257;E5218257 Byer;Amy;NONE1;A5218260;E5218260 Positional File... (3 Replies)
Discussion started by: Ajay Venkatesan
3 Replies

3. Shell Programming and Scripting

Request to check: compare two files , match same entries, write data before it

Hi all, I have 2 files:Column1 of first file has to be matched with column 3 of second file first file contain DATA like this in 2 columns one with gene name second with whether CAD,HT,RA T2Dor any one column 1 column2 ARFGEF2 CAD DDEF2 CAD PSCD3 CAD PSCD4 CAD CAMK1... (5 Replies)
Discussion started by: manigrover
5 Replies

4. UNIX for Dummies Questions & Answers

Not sure how to write a script to delete certain files?

I'm a total *nix newb so I really could use some help! :o I plan on running a cron job on my server that deletes some files and then backs up and emails the website for archival purposes. Before I get in too deep I'd like to remove some unnecessary files that phpThumb creates. I'm not sure... (4 Replies)
Discussion started by: Sinistral
4 Replies

5. Shell Programming and Scripting

write script for more then 600 files

Hi, i have 600 or more file on my server and from this 600 files i want to make one file. i have written this script SPATH=/etlstg/DAT TPATH=/etlstg/DAT/DAT_PROCESSED FILE_DATE=`TZ=CST+24 date +20%y%m%d` a1=0 while do a2=0 while do a3=0 while ... (1 Reply)
Discussion started by: prasson_ibm
1 Replies

6. Shell Programming and Scripting

Write a new file from 2 files as input to the script

Hi- I am hoping someone can give me some pointers to get me started. I have a file which contains some dn's .e.g file 1 cn=bob,cn=user,dc=com cn=kev,cn=user,dc=com cn=john,cn=user,dc=com I have a second file e.g. file.template which looks something like :- dn: <dn> objectclass:... (5 Replies)
Discussion started by: sniper57
5 Replies

7. Shell Programming and Scripting

to write a script to compare the file size in the current directory and previous dir

hi, i am new to this site. i want to write a script to compare the file size of the files in the current dir with the files in the previous directory. the files name will be same, but the filename format will be as xyzddddyymm.txt. the files will arrive with the month end date(i want to... (5 Replies)
Discussion started by: tweety
5 Replies

8. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies

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

10. Shell Programming and Scripting

Script with read/write Files

Hello, I am a Newbie in ksh Unix Script. So I must write a ksh/sh script who read character at a position in a File. So also it must read all the lines who belongs at these characters , then write these lines in a another File. Can you help me , or give little councils to advance with my... (5 Replies)
Discussion started by: steiner
5 Replies
Login or Register to Ask a Question