1

So let's say I'm given the following lines

[duration] =>  92
[from] => 18884541236
[to] => 17841234568

What I want to be able to do is search the entire file to see any instances where the [from] number is the same as the [to] number.

So if I have an entry that looks like this

[duration] => 43
[from] =>  17873332222
[to] => 17873332222

I can search the file for where the [from] number is immediately followed in the next line by an identical [to] number.

2
  • Are you only interested in consecutive cases? What if the 1st entry matches the 12th entry? Should we look for those as well?
    – terdon
    Apr 18 '20 at 15:54
  • @terdon only consecutive cases Apr 18 '20 at 16:07
0

Judging by the sample (dangerous I know)

If [duration] is never going to be equal to [from] and [to] always follows immediately, then this could be as simple as...

awk '$3==last{ print NR, $0} {last=$3}' file
-1

This command will print the line number and the contents of any lines whose first field is [to] and whose third field is the same as the most recently seen [from] value:

awk '{ if($1=="[from]"){from=$3} } $1=="[to]" && $3==from{print NR,$0}' file 

Taking this file as input:

[duration] =>  92
[from] => 18884541236
[to] => 17841234568
[duration] => 43
[from] =>  17873332222
[to] => 17873332222

You get this output:

$ awk '{ if($1=="[from]"){from=$3} } $1=="[to]" && $3==from{print NR,$0}' file 
6 [to] => 17873332222
2
  • 1
    Doesn't appear to be working Apr 18 '20 at 16:06
  • @RyanMcMillan I can't help if you don't tell me how it is failing. Do you get an error message? No results? Wrong results? Please edit your question and show us an example file we can test on, and the output you expect from that example.
    – terdon
    Apr 18 '20 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.