Ignore common line between 2 files in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ignore common line between 2 files in perl
# 1  
Old 06-09-2013
Ignore common line between 2 files in perl

I want to ignore the same line which appear in File1 and File2 and then print the final result back in file1

File1
Code:
ABC
123
XYZ

File2
Code:
XYX

Output
Code:
ABC
123

I have to run this command on multiple servers over ssh.
Below is my code that worked only on same server and not over ssh.

Code:
 my $ignoredList = `cat File1 | grep -vf File2`;
           open ACTIVE, ">File1" or die "can't open 'File1': $!";
           print ACTIVE "$ignoredList";
           close (ACTIVE);


Last edited by Scrutinizer; 06-09-2013 at 05:12 AM.. Reason: code tags
# 2  
Old 06-09-2013
Why do you want to embed your shell commands in Perl to perform functionality that the Shell can easily provide itself? Try:
Code:
grep -vf File2 File1 > File3 && mv File3 File1

# 3  
Old 06-09-2013
If you want an exact line match, e.g. ABC should not match ABCD
Code:
fgrep -vxf File2 File1 > File3 && mv File3 File1

# 4  
Old 06-09-2013
With awk
Code:
awk 'FNR==NR {a[$0];next}  !($0 in a)'  File2 File1 > tmp
mv tmp File1

# 5  
Old 06-09-2013
The mv command requires write permission on the directory;
further it replaces a symbolic link by a file.
For an unknown environment (e.g. different OS types) one should replace mv by cp and rm
Code:
 ... > /tmp/File3.$$ &&
cp /tmp/File3.$$ File1
rm -f /tmp/File3.$$

BTW /tmp is not a safe place, and $$ is predictable.
Safer is mktemp
Code:
tmpfile=`mktemp` &&
 ... > $tmpfile &&
cp $tmpfile File1
rm -f $tmpfile


Last edited by MadeInGermany; 06-09-2013 at 11:36 AM..
# 6  
Old 06-09-2013
I don't see what the problem is with the mv command. There is however an issue with the previous awk command in the sense that it will not generate a return code and/or there is no test for success, so IMO that would need to be introduced .
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

3. Shell Programming and Scripting

Perl how to compare two pdf files line by line

Hi Experts, Would really appreciate if anyone can guide me how to compare two pdf files line by line and report the difference to another file. (3 Replies)
Discussion started by: prasanth_babu
3 Replies

4. UNIX for Dummies Questions & Answers

Want to change common line from multiple files

Hi everyone, I've a requirement to modify an existing line which is common to multiple files. I need to replace that existing line with a new line. I've almost 900 ksh files to edit in the similar fashion in the same directory. Example: Existing Line: . $HOME/.eff.env (notice the "." at the... (3 Replies)
Discussion started by: kaleem.adil
3 Replies

5. Shell Programming and Scripting

perl: comparision of field line by line in two files

Hi everybody, First I apologize if my question seems demasiad you silly, but it really took 4 days struggling with this, I looked at books, forums ... And Also ask help to a friend that is software developer and he told me that it is a bad idea do it by perl... but this is my problem. I moved to... (8 Replies)
Discussion started by: Thelost
8 Replies

6. Shell Programming and Scripting

awk : ignore first 5 line and last line of a file

Hi All, I have text file like as below temp.txt 1 Line temp.txt 2 Line temp.txt 3 Line temp.txt 4 Line temp.txt 5 Line temp.txt 6 Line temp.txt 7 Line temp.txt 8 Line temp.txt N Line I expect the output like as below processing 6 ... processing 7 ... (6 Replies)
Discussion started by: k_manimuthu
6 Replies

7. Web Development

Perl join two files by "common" column

Hello; I am posting to get any help on my code that I have been struggling for some time. The project is to join two files each with 80k~180k rows. I want to merge them together by the shared common column. The problem of the shared column is partially matching, not exactly the same. File1:... (5 Replies)
Discussion started by: yifangt
5 Replies

8. Shell Programming and Scripting

Ignore first word using sed in PERL

Please help me in ignoring first word in a line example Input log 123^Babd^Basdf789^B098^Bouiou Desired output abd,asdf789,098,ouiou 123 should be ignored is this possible using sed regular expressions Use code tags - you got a PM with a guide. (2 Replies)
Discussion started by: thankful123
2 Replies

9. Shell Programming and Scripting

Combine common line from 2 Huge files

Hi, I am having 2 huge files having line count more than 10million. The files look like: File 1 45905099 2059 942961505 3007 8450875165 7007 615565331 3015 9415586035 9012 9871573 5367 4415655 4011 44415539519 5361 3250659295 4001 5950718618 9367 File 2 44415539519 TQ03... (2 Replies)
Discussion started by: rochitsharma
2 Replies

10. Shell Programming and Scripting

Perl index function ignore case

Hi, Is there any way of ignoring case in Perl's index function? Thanks. (2 Replies)
Discussion started by: King Nothing
2 Replies
Login or Register to Ask a Question