File comparision and modification using shell script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers File comparision and modification using shell script
# 1  
Old 02-16-2009
Bug File comparision and modification using shell script

Hello everyone,
I would like to know how to compare two files and modify any differences with some other data using shell script.

I think it would be better understood with an example.

I got two files named 'filex' and filey'. 'filex' is constant file without any changes in data. 'filey' is random file and any new data will be added in first line.

data in filex:
a
b
c
d

data in filey:
b
a

Problem is I want to compare first line in 'filey' with each and every line of 'filex' and if there are any matching lines, the matching lines should be shown and non matching lines should b shown as blanks.

In the above example, first line in 'filey' matches with second line in 'filex' so output should be :

_
b
_
_

and if there is new line which is 'd' added to first line of 'filey' then output should be (by comparing with 'filex')

_
b
_
d

I'm trying to use function , which takes files as arguments.

Thanks in advance.
# 2  
Old 02-16-2009
start with grep, this gives you what appears to be close to what you want
Code:
grep -f filey  filex

this may be closer but it will be slow on big files, it is really inefficient:
Code:
while read recordy
do
      grep -q "$recordy" filex
      if [[ $? -eq 0 ]] ; then
            echo "$recordy"
      else
            echo " "
      fi
done  < filey

# 3  
Old 02-16-2009
Hey Jim thanks for your reply. I could get matching lines between two files.
But I cant replace non-matching lines with "_" (under-score) in 'filex'. any help on this would be appreciated.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modification of perl script to split a large file into chunks of 5000 chracters

I have a perl script which splits a large file into chunks.The script is given below use strict; use warnings; open (FH, "<monolingual.txt") or die "Could not open source file. $!"; my $i = 0; while (1) { my $chunk; print "process part $i\n"; open(OUT, ">part$i.log") or die "Could... (4 Replies)
Discussion started by: gimley
4 Replies

2. Shell Programming and Scripting

Shell backup script modification

so I'm using this shell script someone made to backup data on a server in an archive (gpg encrypted) and upload to an FTP (meant to be run as a cron job daily). can one of the experts here confirm if the script is fine? It is meant to backup the folder /opt and the sql data i want to know how I... (9 Replies)
Discussion started by: galapagos8000
9 Replies

3. Shell Programming and Scripting

Needed shell script to read txt file and do some modification

Hi ...programmers... I need a shell script to perform some specific task.. my txt file looks like this netcdf new { dimensions: XAX1_11 = 11 ; variables: double XAX1_11(XAX1_11) ; XAX1_11:point_spacing = "even" ; XAX1_11:axis = "X" ; float DEPTH(XAX1_11) ;... (19 Replies)
Discussion started by: Akshay Hegde
19 Replies

4. Shell Programming and Scripting

Modification in shell script

Hello Team, I have prepared script which will check for listening message for ports 1199,1200 and 1201. I need modifcation in script in such a way that if port 1200 is not listening then it should message rmi port 1200 is not listening. Smap for port 1199 and 1201. kindly guide me to acheive... (4 Replies)
Discussion started by: coolguyamy
4 Replies

5. Homework & Coursework Questions

How do I get at the modification date for a file as a variable for a script?

I realize this is basic and probably obvious, but I'm pulling my hair out. I'm guessing this is just some flag on the file command or somesuch, but I can't find it. Help me get unstuck please? EDIT: I guess what I'm asking is once I've got the ls -l output for a file, what command do I use to... (3 Replies)
Discussion started by: Timespike
3 Replies

6. Shell Programming and Scripting

Help with Shell Script Modification

Hi all Iam very new to Shell Scripting, I have to modify a shell script looking at an existing one except that it will query against some table X in A database. Befor Spooling check if there are any reload files if there archive the files. The above scipt executes some abc.sql which will b a new... (2 Replies)
Discussion started by: Varunkv
2 Replies

7. Shell Programming and Scripting

String comparision in shell scripting

Hi Guys, I am new to scripting I have written a code to compare strings,but I am getting some Exception Code snippet: MODE="D" if ]; then . $file1 fi Error: ./BatchJobs.sh: [[: execute permission denied I have given all Execute permissions to the script(chmod 755... (2 Replies)
Discussion started by: Anji
2 Replies

8. Shell Programming and Scripting

crontab entry modification using shell script

hi Friends, iam trying to write a script which will grep the particular entry in crontab of root and enable/disable it .iam a novice in scripting. your suggestions are most welcome..please help Cheers!! (4 Replies)
Discussion started by: munishdh
4 Replies

9. UNIX for Dummies Questions & Answers

Shell script: last modification date for a file

Hi i have a ques in Shell scripting: ques: accept a filename as a command line argument. Validate the input and display the last modification date for that file. Help pls. (4 Replies)
Discussion started by: onlyc
4 Replies

10. Shell Programming and Scripting

File Comparision by using Shell Script

Hello All, I am trying to find 2 file comparision by using Shell Script. For example, I am having 2 directories namely DAY1 & DAY2. DAY1 directory contains file1.dat, file2.dat, file3.dat, file4.dat, file5.dat & DAY2 directory contains file1.dat, file2.dat, file3.dat, file4.dat, file5.dat. Now,... (3 Replies)
Discussion started by: nvkuriseti
3 Replies
Login or Register to Ask a Question