Compare two files and lines which are the same just delete it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two files and lines which are the same just delete it
# 1  
Old 09-07-2017
Compare two files and lines which are the same just delete it

I am having a two files and different days, and this is example:
file1: 06.09.2017.
Code:
abcd
123

file2: 07.09.2017.
Code:
abcd
1234

So what I want is that file2 with today's date contains only 1234, so where is a problem you would ask?
Problem is here that I put these commands into routers,. and error occurred with message that interface already exist, and no other commands are pasted into router.
Solution that I think is to compare two files, one yesterday and one for today, and compare it, if lines are the same DELETE IT FROM TODAY and leave only what is different.

Last edited by rbatte1; 09-07-2017 at 04:56 AM.. Reason: Added CODE tags, corrected spelling etc.
# 2  
Old 09-07-2017
Quote:
Originally Posted by tomislav91
compare two files, one yesterday and one for today, and compare it, if lines are the same DELETE IT FROM TODAY and leave only what is different.
If your file contents are "well-behaved", that is: contain no regexp metacharacters (somehow i don't that i have been shown the real content with "1234"), you can use grep -vf file1 file2 to filter all lines out of file2 which are also in file1.

I hope this helps.

bakunin
# 3  
Old 09-07-2017
i manage to succed with this command
Code:
awk 'NR==FNR{a[$0]=1;next}!a[$0]' file1 file2

and it give me the line which are only changed, but there is a some another problem...
the line which is changed is inside a interface setup.
And only THAT command when put to router does nothing, it must first go into interface
example:
Code:
/interface wireless

set [ find default-name=wlan1 ] ssid=test

so if I change only ssid, my awk command give me output
Code:
set [ find default-name=wlan1 ] ssid=test1

and that command i cant paste into router, i must and
Code:
/interface wireless

So i cant figure out how to do it.
I want to give a backup to another rotuer and if something is changed on the first one to be changed to the second, and my script does it good, but when change something like this, nothing happend, it error at first command with interface with error that interface already exist...
# 4  
Old 09-07-2017
Quote:
Originally Posted by tomislav91
And only THAT command when put to router does nothing, it must first go into interface
example:
Code:
/interface wireless

set [ find default-name=wlan1 ] ssid=test

so if I change only ssid, my awk command give me output
Code:
set [ find default-name=wlan1 ] ssid=test1

and that command i cant paste into router, i must and
Code:
/interface wireless

Please understand: this type of things is context-aware: your "set ..." means different things if it is prefaced by "/interface wireless" (then it is a command part) or not (then it is something undecipherable by tre router).

To make a program understand (and hence take into account) such differences you need to parse the input. And parsing - as a recursive process - cannot be done by regexps. Therefore your whole problem description:

Quote:
So what I want is that file2 with today's date contains only 1234
is basically meaningless. In fact you need a parser which identifies not the changed lines but the changed configuration commands leading to a different setup. For this you need a language interpreter (of which the parser is one part, the lexical analyser is the other) for the routers configuration language.

I hate to break it to you but writing a parser is too big a project to do it here in a post. There are a lot of articles in the internet, though, which explain how to do it.

I hope this helps.

bakunin
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 compare files in 2 folders and delete the large file

Hello, my first thread here. I've been searching and fiddling around for about a week and I cannot find a solution.:confused: I have been converting all of my home videos to HEVC and sometimes the files end up smaller and sometimes they don't. I am currently comparing all the video files... (5 Replies)
Discussion started by: Josh52180
5 Replies

2. Shell Programming and Scripting

Compare lines between two files

I have two files I need to compare these two files and take the lines that are common in both the files and consider the line present in second file for my further processing I have used "Awk" along with "FNR and NR" but that is not working gawk -F= ' > FNR==NR {a=$1; next}; > ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

3. UNIX for Dummies Questions & Answers

Compare lines in 2 files

I have 2 files with exactly the same information (with header and separated by ";") and what I would like to do is print (for both files!) the columns that are different and also print the "key" column that is equal in the 2 files For example, if File1: key1;aaa;bbb;ccc key2;ddd;eee;fff... (4 Replies)
Discussion started by: mvalonso
4 Replies

4. Shell Programming and Scripting

need to delete all lines from a group of files except the 1st 2 lines

Hello, I have a group of text files with many lines in each file. I need to delete all the lines in each and only leave 2 lines in each file. (3 Replies)
Discussion started by: script_op2a
3 Replies

5. Shell Programming and Scripting

Compare 3 files, delete data equals.

Hi, i have a problem, I have three files, file_1, File_2 file_3 and I need to compare the data with file_3 file_1, data that are equal to file_3 file_1 should be deleted, file_1 receive data and file_2 file_3. Ex: file_1 374905,2001, Selmar Santos, Técnico de Sistemas, U$3.000,00 789502,... (3 Replies)
Discussion started by: selmar
3 Replies

6. Shell Programming and Scripting

Compare files in two folders and delete missing ones

I do not know much about shell scripting so I am at a loss here. If someone can help me, that would be great! I have two directories /dir1 /dir2 I need to delete all files from /dir1 and that does not have a correspondent file in /dir2. It should NOT check file suffixes in /dir2 . Why?... (20 Replies)
Discussion started by: kaah
20 Replies

7. Shell Programming and Scripting

compare two files and to remove the matching lines on both the files

I have two files and need to compare the two files and to remove the matching lines from both the files (4 Replies)
Discussion started by: shellscripter
4 Replies

8. UNIX for Dummies Questions & Answers

Compare these two lines and delete the old entry

I have a unique situation here which looks easier at first but I am not able to solve it. # SSort UNIX 10/14/2005 10/13/2010 "tox" "9000/800" 16 * * * V849-6-1 # SSort UNIX 11/31/1996 11/02/2010 "tox" "9000/800" 16 * * * W237-S-2 I have a text file with two or multiple values like this and... (1 Reply)
Discussion started by: pareshan
1 Replies

9. Shell Programming and Scripting

How to delete first 5 lines and last five lines in all text files

Hi I want to delete first five and last five lines in text files without opening the file and also i want to keep the same file name for all the files. Thanks in advance!!! Ragav (10 Replies)
Discussion started by: ragavendran31
10 Replies

10. Shell Programming and Scripting

Compare data in 2 files and delete if file exist

Hi there, I have written a script called "compare" (see below) to make comparison between 2 files namely test_put.log and Output_A0.log #!/bin/ksh while read file do found="no" while read line do echo $line | grep $file > /dev/null if then echo $file found found="yes" break fi... (3 Replies)
Discussion started by: lweegp
3 Replies
Login or Register to Ask a Question