Visit The New, Modern Unix Linux Community


Delete a pattern present in file 2 from file 1 if found in file 1.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete a pattern present in file 2 from file 1 if found in file 1.
# 1  
Delete a pattern present in file 2 from file 1 if found in file 1.

I have two files
Code:
File1
====
1|2000-00-00|2010-02-02||
2| 00:00:00|2012-02-24||
3|2000-00-00|2011-02-02||

File2
====
2000-00-00
 00:00:00

I want the delete the patterns which are found in file 2 from file 1,

Expected output:
Code:
File1
====
1||2010-02-02||
2||2012-02-24||
3||2011-02-02||

I did something like
Code:
while read line
  do
     sed 's/'$line'//g' File1 > File1_tmp
     mv File1_tmp File1
  done < File2

Not sure if this is good approach. Can someone help me with some better solution?
# 2  
Quote:
Originally Posted by machomaddy
Code:
while read line
  do
     sed 's/'$line'//g' File1 > File1_tmp
     mv File1_tmp File1
  done < File2

1. This would only remove that part of string from file1 which matches with file2. It won't remove the whole line from file1.
2. You may do this:
Code:
while read line
do
    grep $line file1 >> file1_tmp
done < file2
mv file1_tmp file1

3. Or you may also do this:
Code:
grep -fv file2 file1

# 3  
I just wanted to remove the data in file1 which matches the pattern in file2. Not the whole line.Smilie
# 4  
Hi machomaddy,

One way using perl:
Code:
$ cat file1
1|2000-00-00|2010-02-02||
2| 00:00:00|2012-02-24||
3|2000-00-00|2011-02-02||
$ cat file2
2000-00-00
 00:00:00
$ cat script.pl
use warnings;
use strict;

die qq[Usage: perl $0 <file-1> <file-2>\n] unless @ARGV == 2;

open my $fh1, qq[<], shift @ARGV or die qq[Cannot open input file\n];
open my $fh2, qq[<], shift @ARGV or die qq[Cannot open input file\n];

my (@patterns, $pattern, $re);

while ( <$fh2> ) {
        next if m/\A\s*\Z/;
        s/\A\s+/\\s*/;
        s/\s+\Z/\\s*/;
        push @patterns, $_;
}

$pattern = join qq[|], @patterns;
$re = qr/($pattern)/;

while ( <$fh1> ) {
        do { print, next } if m/\A\s*\Z/;
        chomp;
        s/$re//go;
        printf qq[%s\n], $_;
}
$ perl script.pl file1 file2
1||2010-02-02||
2||2012-02-24||
3||2011-02-02||

Regards,
Birei
# 5  
See if this works:
Code:
awk -F\| 'NF==1{A[$1];next}$2 in A{$2=x}1' OFS=\| file2 file1

# 6  
try this

Code:
awk -F, 'NR==FNR{a[$0]=$0;next} {for(i in a) gsub(i,""); print} ' file_with_pattern file


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #321
Difficulty: Medium
DHCP stands for Dynamic Host Configuration Port.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to append multiple text files into one file based on pattern present in filaname

Hi All-I am new to Unix , I need to write a script. Can someone help me with a requirement where I have list of files in a directory, I want to Merge the files if a pattern of string matches in filenames? AAAL_555A_ORANGE1_F190404.TXT AAAL_555A_ORANGE2_F190404.TXT AAAL_555A_ORANGE3_F190404.TXT... (6 Replies)
Discussion started by: Shankar455
6 Replies

2. Shell Programming and Scripting

Systemd errors of missing file “No such file or directory” inspite of file being present

The contents of my service file srvtemplate-data-i4-s1.conf is Description=test service for users After=network.target local-fs.target Type=forking RemainAfterExit=no PIDFile=/data/i4/srvt.pid LimitCORE=infinity EnvironmentFile=%I . . . WantedBy=multi-user.target (0 Replies)
Discussion started by: rupeshkp728
0 Replies

3. Shell Programming and Scripting

Speed : awk command to count the occurrences of fields from one file present in the other file

Hi, file1.txt AAA BBB CCC DDD file2.txt abc|AAA|AAAabcbcs|fnwufnq bca|nwruqf|AAA|fwfwwefwef fmimwe|BBB|fnqwufw|wufbqw wcdbi|CCC|wefnwin|wfwwf DDD|wabvfav|wqef|fwbwqfwfe i need the count of rows of file1.txt present in the file2.txt required output: AAA 2 (10 Replies)
Discussion started by: mdkm
10 Replies

4. Shell Programming and Scripting

If first pattern is found, look for second pattern. If second pattern not found, delete line

I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... :) I have the output of an ldapsearch that looks like this: dn: cn=sam,ou=company,o=com uidNumber: 7174 gidNumber: 49563 homeDirectory: /home/sam loginshell: /bin/bash uid: sam... (2 Replies)
Discussion started by: samgoober
2 Replies

5. Shell Programming and Scripting

Need help on appending all the lines in a file after a pattern is found

Hi Friends, I am working on a file which has content as follows Wed,Database,ABC_cube,loaded Wed,Logging,out,user,302002654,active,for,0,minutes Wed,Logging,out,user,109000151,active,for,8,minutes Wed,Logging,out,user,302002654,active,for,0,minutes... (8 Replies)
Discussion started by: dev.devil.1983
8 Replies

6. Shell Programming and Scripting

Need unix commands to delete records from one file if the same record present in another file...

Need unix commands to delete records from one file if the same record present in another file... just like join ... if the record present in both files.. delete from first file or delete the particular record and write the unmatched records to new file.. tried with grep and while... (6 Replies)
Discussion started by: msathees
6 Replies

7. Shell Programming and Scripting

Log File - Getting Info about preceding Date of Pattern Found

Ok Suppose I have a log file like the below: 2010-07-15 00:00:01,410 DEBUG 2010-07-15 00:01:01,410 DEBUG 2010-07-15 00:01:02,410 DEBUG com.af ajfajfaf affafadfadfd dfa fdfadfdfadfadf fafafdfadfdafadfdaffdaffadf afdfdafdfdafafd error error failure afdfadfdfdfdf EBUDGG eafaferror failure... (6 Replies)
Discussion started by: SkySmart
6 Replies

8. Shell Programming and Scripting

How to delete a string pattern in a file and write back to the same file

I have a control file which looks like this LOAD DATA INFILE '/array/data/data_Finished_T5_col_change/home/oracle/emp.dat' PRESERVE BLANKS INTO TABLE SCOTT.EMP FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS (................. ..................) How can i edit the... (1 Reply)
Discussion started by: mwrg
1 Replies

9. UNIX for Advanced & Expert Users

delete line from file if successful partial string found

Id like to delete a line from a file using (preferably a single line unix command) if it contains a certain string pattern. If line contains "abcdef" then delete that line. Help greatly appreciated. (7 Replies)
Discussion started by: cronjob78
7 Replies

10. Shell Programming and Scripting

Delete Strings that are present in another file

HI, if a String is present in file1.txt, i want to delete that String from file2.txt. How can i do this?? I am sure that the file1.txt is a subset of file2.txt. (2 Replies)
Discussion started by: jathin12
2 Replies

Featured Tech Videos