-1

So I have a fie of logs like this:

20-03-2020 10:01 [LOG] davis /home/davis/fis1 ACCESS_WRITE_OK
20-03-2020 11:11 [LOG] davis /home/davis/Pictures/cat.png ACCESS_READ_OK
20-03-2020 12:22 [LOG] root /home/davis/hello ACCESS_READ_OK
20-03-2020 21:10 [ERROR] davis /root/secret.txt ACCESS_READ_DENY
20-03-2020 23:11 [LOG] davis /home/davis/secret ACCESS_READ_OK
20-03-2020 23:22 [ERROR] ralex /home/davis/secret ACCESS_WRITE_DENY
21-03-2020 06:00 [LOG] root /bin/bash ACCESS_READ_OK
21-03-2020 07:09 [LOG] ralex /home/ralex/secret ACCESS_WRITE_OK
21-03-2020 08:22 [ERROR] ralex /dev/sda1 ACCESS_READ_DENY
21-03-2020 14:12 [LOG] root /home/davis/.hidden/secret_root ACCESS_WRITE_OK
22-03-2020 07:09 [LOG] root /dev/sda ACCESS_READ_OK

And I need to extract only those logs where the user name is the same both in the path and the username column. After that i need to replace the absolute path with ~ And my file with logs shoul look like this:

20-03-2020 10:01 [LOG] davis ~/fis1 ACCESS_WRITE_OK
20-03-2020 11:11 [LOG] davis ~/Pictures/cat.png ACCESS_READ_OK
20-03-2020 12:22 [LOG] root /home/davis/hello ACCESS_READ_OK
20-03-2020 21:10 [ERROR] davis /root/secret.txt ACCESS_READ_DENY
20-03-2020 23:11 [LOG] davis ~/secret ACCESS_READ_OK
20-03-2020 23:22 [ERROR] ralex /home/davis/secret ACCESS_WRITE_DENY
21-03-2020 06:00 [LOG] root /bin/bash ACCESS_READ_OK
21-03-2020 07:09 [LOG] ralex ~/secret ACCESS_WRITE_OK
21-03-2020 08:22 [ERROR] ralex /dev/sda1 ACCESS_READ_DENY
21-03-2020 14:12 [LOG] root /home/davis/.hidden/secret_root ACCESS_WRITE_OK
22-03-2020 07:09 [LOG] root /dev/sda ACCESS_READ_OK

Can you help me with this problem? Thanks! :)

New contributor
an onim is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 2
    This looks like a typical homework problem. What have you tried already? What part are you stuck on? We won't do your homework for you, but we can help you understand. – user535733 Mar 27 at 2:11
  • I can't do the matching between the username and the name in the path. I think that is my biggest problem. I tried "grep" and "sed" but I have some issues. – an onim Mar 27 at 8:23
0

sed "s/\<\([a-z]\{1,\}\)\>.*\<\1\>/\1 ~/g" FILENAME

That is the code for what I needed. It searches for lines where the username is found twice and it replaces the matching words with the first group (the username) and ~ for the /home/username/ path. Not sure if its the best way to do it but id does the job :))

New contributor
an onim is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • The "best" way is the way that you understand, can maintain, and can explain to others. In real life, this is a good solution. Well done! – user535733 Mar 27 at 13:32

Your Answer

an onim is a new contributor. Be nice, and check out our Code of Conduct.

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.