Search for all CAPITAL LETTER characters and date time and parse it

hi, literally new to shell scripting. i have this problem where i want to search for all capital letters, date and time in a logfile and parse it.

ex. more or less my log file looks like this.

USER1 - +6192345679 2020-06-01 14:31 [ACTIVE] Execute thread bla blah blah
supervisor of user1 cleared to change code...
USER2 - +6185880909 2020-06-01 07:30 [ACTIVE] Execute thread bla blah blah
supervisor of user2 cleared to change code...
USER3 - +3434343434 2020-05-01 13:30 [ACTIVE] Execute thread bla blah blah
supervisor of user3 needed more time to...

i needed help to make it display like this.

USER1 2020-06-01 14:31
USER2 2020-06-01  07:30
USER3 2020-05-01 13:30

hope you'll be able to help me. thanks

We encourage users to show their own attempts.
What have you tried ?

Regards
Peasant.

1 Like

A few questions to think about...
Is this a strict format for these records (ie. field position)?
Is there an identifier unique to these records (eg name of the logging method)?
Are those real phone numbers and should you anonymise them?

Take a look at the cut and grep commands and see if they could meet your needs.

1 Like

hi peasant!
i've actually have tried a number of commands but the result is not that desirable.
i've tried this grep -woE '[A-Z]{2,}|[0-9]+-[0-9]{2}-[0-9]{2} [0-9]+:[0-9]{2}' sys.log. however it gives me this format:

USER1
ACTIVE
2020-06-01
14:31
USER2
ACTIVE
2020-06-01
14:31

i've also tried this

sed -n '/^[A-Z][A-Z]* /p' sys.log

and it displays everything on the log file.

im sorry but i just couldn't figure out how to get the correct format like this

USER1 2020-06-01 14:31
USER2 2020-06-01  07:30
USER3 2020-05-01 13:30

thanks

hi skrynesaver,
its not that really a strict format but its desirable format. yeah, im still reading about cut. hope you guys be able to help. thanks

Hi dukot, please adapt accordingly.

root@host:~# cat file
USER1 - +6192345679 2020-06-01 14:31 [ACTIVE] Execute thread bla blah blah
supervisor of user1 cleared to change code...
USER2 - +6185880909 2020-06-01 07:30 [ACTIVE] Execute thread bla blah blah
supervisor of user2 cleared to change code...
USER3 - +3434343434 2020-05-01 13:30 [ACTIVE] Execute thread bla blah blah
supervisor of user3 needed more time to...
root@host:~#
root@host:~# awk '/^USER/ {print $1, $4, $5}' file
USER1 2020-06-01 14:31
USER2 2020-06-01 07:30
USER3 2020-05-01 13:30
root@host:~#

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.