With perl:
#! /usr/bin/perl
use strict;
my @lines=();
while(<>) {
chomp;
s/#.*//g; # strip comments
s/^\s*|\s*$//g; # strip leading and trailing spaces
next if (/^$/); # skip blank lines
if (! scalar @lines) {
# store the first line of the file in the array
# we can't do anything else yet, so skip to the next line.
push @lines, $_;
next;
} else {
push @lines, $_;
# split both lines into space-separated fields.
my @a = split /\s+/, $lines[0];
my @b = split /\s+/, $lines[1];
# if 3rd fields are the same, change to "both"
if ($a[2] eq $b[2]) {
@lines = map { $_ =~ s/(source|destination)$/both/oi; $_} @lines;
}
}
print $lines[0],"\n";
shift @lines;
}
print $lines[0],"\n";
The idea here is to use an array (@lines) to hold the current line and the previous line. If the 3rd field (perl-arrays are zero-based) of both lines are the same then change the strings "source" or "destination" to "both".
Print the previous line, whether they were changed or not. Then delete the previous line from the array, using shift so that on the next pass through the loop, the current line will be the previous line.
Finally, after the loop has finished, print the last input line.
Output:
$ ./swatesh.pl <swatesh.txt
2015-11-24 12:59:37.112 128.206.6.136 source
2014-11-24 12:59:36.920 8.8.8.8 source
2014-11-24 14:59:38.112 23.234.22.106 both
2014-11-24 13:59:37.113 23.234.22.106 both
2014-11-24 12:59:29.047 74.125.198.141 source
2014-12-25 12:59:36.920 74.125.198.148 destination
Some Notes:
The sed script works well, so why might you choose to use perl instead? What are the differences?
@Costas' sed version is faster, which might be significant if you have millions of lines to process.
This perl version explicitly checks if field 3 on both lines is exactly the same, while the sed version only checks to see if a pattern that looks similar to an IP address is repeated later in the same joined double-line (which is not necessarily a problem - for your sample input, the sed version works perfectly. It's optimised for exactly your sample).
The perl version is probably easier to adapt to different input.
The code at the start of the loop is a useful bit of code I use in many perl scripts to skip blank lines and to support # comments in text files. I often do the same in sed scripts but the longer a sed script becomes, the less readable it is...and I like to write code that I can understand at a glance in 6 months time.
Aside from these relatively minor details, both scripts use a very similar algorithm.