Need help using awk or sed.

Hi All,
Is there a way of comparing two columns in the same file and deleting the row if the values of the columns match.

I have the sample data file as below.

M024900|175309.00|968.00|17 
M025001|19861.79|97.90|148 
M025002|431.70|159.00|3 
M025003|912.30|159.90|6 
M025800|3465.62|89.84|21 
M024574|4644.23|4644.23|121

For eg : here the last line should be deleted since column2 and column 3 match.

Any ideas would be helpful.

Hi nua7,

Try with:

awk -F"|" '{if($2!=$3) print}' inputfile

Regards

1 Like

Well actually this isn't helping.Not sure why.I still get the values in the files where $2 and $3 match.

See the sample below :

(rk3388) mpdev1=> more mpcashqc.btot.errors
M025001|19861.79|19861.79|148 
M025002|431.70|431.70|3 
M025800|3465.62|3465.62|21 
(rk3388) mpdev1=> awk -F"|" '{if($2!=$3) print}' mpcashqc.btot.errors
(rk3388) mpdev1=> more mpcashqc.btot.errors
M025001|19861.79|19861.79|148 
M025002|431.70|431.70|3 
M025800|3465.62|3465.62|21 
$ cat tst
M024900|175309.00|968.00|17
M025001|19861.79|97.90|148
M025002|431.70|159.00|3
M025003|912.30|159.90|6
M025800|3465.62|89.84|21
M024574|4644.23|4644.23|121
$ awk -F\| '$2!=$3' tst
M024900|175309.00|968.00|17
M025001|19861.79|97.90|148
M025002|431.70|159.00|3
M025003|912.30|159.90|6
M025800|3465.62|89.84|21
$ awk -F\| '$2==$3' tst
M024574|4644.23|4644.23|121

If you are on Solaris /SunOS, use nawk instead of awk

Strange! I am working on HP-Unix and I am pretty sure the solution should work this way, but for some reason it is not!

---------- Post updated at 01:18 AM ---------- Previous update was at 01:16 AM ----------

oh ok.. I know now.I thought it was actually deleting it from the main file , but it was just printing.Is there a way out for the same or I have to put it another temp file and rename it.

Another tempfile.
then cat the tmp file into the original file instead renaming it, so that it will not be "inode destructive"
(with mv command, you would loose the inode of the target file)

Through sed..

sed '/.*[^|]|\([^|]*\)|\1|.*/d' inputfile > outfile