0

I am looking for a little help, I am learning as I go. I have something similar to the following below. I am looking to display the occurrence of "Logged in" on Log1, and then the "Processed Bill" on Log1. Using the search value of - 44. This honestly looks like I am trying to cheat a test or something, but its not, I am new to breaking files down, and looking to learn.

file.txt

Log0 | 20191104 | 01 | Logged In - 55
Log1 | 20191104 | 04 | Logged In - 44
Log2 | 20191104 | 03 | Logged In - 33
Log1 | 20191104 | 02 | Received Bill
Log1 | 20191104 | 02 | Accepted Bill
Log2 | 20191104 | 05 | Logged Out - 33
Log1 | 20191104 | 33 | Processed Bill
Log0 | 20191104 | 44 | Broken Bill

Looking for desired output.

Log1 | 20191104 | 04 | Logged In - 44
Log1 | 20191104 | 33 | Processed Bill
  • What connects 44 with "Processed Bill"? – guillermo chamorro Nov 4 '19 at 18:22
  • log1 would be the key – user4618954 Nov 4 '19 at 18:25
  • So you don't know it's Log1 you're looking for, you just want to find the Logged In with the - 44 and the corresponding Processed Bill with the same value in the first column? – Kusalananda Nov 4 '19 at 18:50
  • yes exactly. Log1 could be anything, but it would be the same value for both the logged in and the processed bill. But would have to search upon the 44 value. – user4618954 Nov 4 '19 at 18:53
0

In fact very similar to @Choroba solution:

awk -F'|'   '/Logged In/                   { login[$1]=$0}     
             /Processed Bill/ && login[$1] { print login[$1]; print}' ex1

Where:

  • /Logged In/ { login[$1]=$0} -- saves the lines with "Logged in" associated to column 1 (ex "Log1")
  • /Processed Bil/ && login[$1] {print ...} -- prints when it finds a "Processed Bill" previously login

Correction: To print just 44:

awk -F'|'   '/Logged In - 44/              { cod=$1; line=$0}     
             /Processed Bill/ && $1==cod   { print line "\n" $0}' file
  • 1
    And what about the - 44 (assuming there may be other pairs of Logged In and Processed Bill that they don't want to see)? – Kusalananda Nov 4 '19 at 19:56
  • @Kusalananda, You are right. Although already in the initial PO statement, the clarifying explanations comments appeared after my answer. – JJoao Nov 5 '19 at 0:44
  • Thanks, this was helpful. – user4618954 Nov 5 '19 at 19:47
2

Perl solution:

perl -F'/\|/' -ne '$id = $F[0] if $F[3] =~ /- 44$/;
                   print if $F[0] eq $id && $F[3] =~ /Logged In|Processed Bill/;
                  ' -- file.txt
  • -n reads the input line by line
  • -F splits each input line on the given regex into the @F array
  • if $F[3], i.e. the fourth column, matches - 44, the first column is stored in $id.
  • the whole line is printed if the first column equals the $id and the fourth column matches Logged In or Processed Bill.
0

With Gnu sed in extended regex mode:

$ sed -re ':a
    /Logged In - 44/!d
    $d;h;N
    /^(\S+)\s*\|.*\n\1\s*\|.*Processed Bill/b
    g;ba
' file

Results

Log1 | 20191104 | 04 | Logged In - 44
Log1 | 20191104 | 33 | Processed Bill

How it works

° Skip lines till we meet "Logged In 44"
° Pick up the nexr line and if it is "Processed Bill" AND first field matches with the logged in line, then it's a GO.
° Otherwise, discard the second portion and redo this process. 
° Assumption is that logged in 44 line is unique. 
  • This one was very interesting, if you had to, how would you combined fields 1 and 2 as the key? – user4618954 Nov 5 '19 at 21:15
  • Then change the regex to this: /^(([^|]+[|]){2}).*\n\1.*Processed Bill/b – Rakesh Sharma Nov 6 '19 at 1:13
  • That is so interesting, this is very helpful. Sorry if I want to challenge more, but what if in the same file, Logged In - 44 and Processed Bill appears again, and I would want to display the additional occurrences, is this possible in sed? – user4618954 Nov 6 '19 at 14:42
  • Can you provide the input and the expected output for this scenario? – Rakesh Sharma Nov 7 '19 at 11:01

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.