Diff command with a condition


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Diff command with a condition
# 1  
Old 09-12-2011
Diff command with a condition

Hi

I have 2 files test1 & test2.

Test1 contains :
BSC
AFCN

Test2 contains:
AFCN

I than use the following command :

diff file1 file2

which displays than displays : 2d1
< BSC

Basically I want to confirm that except for "BSC", AFCN is the only word in each file.So when I do a "Diff" how do i say do not take "BSC" into consideration but just check for other differences(in this case only AFCN)?

Thanks & Regards
Pragesan
# 2  
Old 09-12-2011
Well the most obvious would be to remove the unwanted line before to use diff...
To really help you, we would need to know a bit more:
Is this case always true? (First line unwanted) etc...
# 3  
Old 09-12-2011
Hi vbe

Thanks for the reply.

Yes this is always true.File1 i.e Test1 will always contain the first line "BSC" which I don't want.So when i'm doing the diff between the two files I'm hoping to see only 1 difference i.e the word "BSC" but to make it easier for others to read I would like the diff command not to take the first line of Test1 into consideration. and thus have no differences.

I hope this makes sense

Thanks & Regards
Pragesan
# 4  
Old 09-12-2011
if you want to compare test1 and test2 but ignore ALL "BSC" from test1:
Code:
grep -v 'BSC' test1|diff - test2

if you want to compare test1 and test2 but ignore the 1st line from test1:
Code:
sed -n '2,$p' test1|diff - test2

# 5  
Old 09-13-2011
Hi sk1418

Thanks for your reply.Both of your commands work.I need the second command Smilie

I'm extremly new to unix.Please could you explain command 2.It will help me a lot going forward.

sed -n '2,$p' test1|diff - test2
Thank you once again.
.
# 6  
Old 09-13-2011
Quote:
Originally Posted by Prega
Hi sk1418

Thanks for your reply.Both of your commands work.I need the second command Smilie

I'm extremly new to unix.Please could you explain command 2.It will help me a lot going forward.

sed -n '2,$p' test1|diff - test2
Thank you once again.
.
sed command will cut the 1st line from your test1 file, and pass the result to diff command via pipe, to compare with test2 file.
# 7  
Old 09-13-2011
Since your new at unix, you could have a look at the man pages of tail and head commands, for you could have used e.g.
Code:
 tail -n +2 test1

maybe more accessible for a novice than having to assimilate sed (and awk) syntax, nevertheless you would have to use it in the same way as the bright example of sk1418 since diff wants 2 files in input you have to use "-" to say "Standard Input - to be used as file1"...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help with diff command

Platform :Oracle Linux 6.4 Shell : bash In the below sample, although the lines in a.txt and b.txt are jumbled up, there is only one difference : b.txt has an extra line NETHERLANDS $ cat a.txt SPAIN NORTH KOREA PORTUGAL GERMANY SYRIA $ $ $ cat b.txt GERMANY NORTH KOREA SPAIN... (6 Replies)
Discussion started by: John K
6 Replies

2. Shell Programming and Scripting

One-way diff command?

Hello, I am trying to find the different files between multiple directories in Linux, here is a small assumption of what is inside the directories dir1 dir2 dir3 1.txt 1.txt 1.txt 2.txt 3.txt 3.txt 5.txt 4.txt 5.txt 6.txt 7.txt 8.txt I am using the following... (4 Replies)
Discussion started by: Error404
4 Replies

3. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

4. UNIX for Dummies Questions & Answers

Re:using the diff command

Hi Guys I have a situation where I would like to use the diff command but I would like to see "number" of differences and than send it through and if statement and than view the difference if greater than 1. Eg. diff file1 file2 > than gives the "number" and I than say - if number >1... (3 Replies)
Discussion started by: Prega
3 Replies

5. Shell Programming and Scripting

diff command help

Hi all diff file1 file 2 command will give us op of diff between two file. But it aslo give its position and sign "<" or ">". I dont want position and sign in op. Only diff of content should be come as op. Kindly help me for this. Regards Jaydeep (1 Reply)
Discussion started by: jaydeep_sadaria
1 Replies

6. UNIX for Dummies Questions & Answers

diff command

Is there any option for the diff command (or maybe an entirely different command) that will give you only the text that differs between two files? When I use diff file1 file2, if any text on that line differs from one file to the next it'll print out the entire line. I'd like to see only the text... (2 Replies)
Discussion started by: red baron
2 Replies

7. AIX

diff command

hello i've two files. how i get the diff between the two files to new file. thanks best regards ariec (3 Replies)
Discussion started by: ariec
3 Replies

8. Shell Programming and Scripting

need help in diff command :

i have 2 file named test1,test2 contents of test1: 1 2 3 --------------------------- contents of test2: 1 2 3 4 5 -------------------------------------------------------- my desired o/p should be: diff test2 test1 4 (5 Replies)
Discussion started by: ali560045
5 Replies

9. Shell Programming and Scripting

diff command

All, How to exclude a directory while diff execution? For ex: To exclude file which we don't want to see diff, we have -x <filename>. Thanks in advance (1 Reply)
Discussion started by: Vichu
1 Replies

10. UNIX for Dummies Questions & Answers

diff command

Hi, I have 2 files i would like to have a DIFF command: 1.Marks differences between files or 2.Mentions just the differences Thanks :) (7 Replies)
Discussion started by: gilead29
7 Replies
Login or Register to Ask a Question