[Solved] Trimming out segment

I have a file with a combination of binary characters and words, and need to trim out the segment i.e.

SleeperThreadAborting {{username::RAB2002}} {{scriptname::scs_get_pending_by_loc}}

and put the lists into a text file.

Luckily, the segment I am looking for i.e. has readable words

SleeperThreadAborting^G{{username::RAB2002}}{{scriptname::scs_get_pendi
ng_by_loc}}

with an extra binary

^G

.

Please note that some have "carriage return" on i.e.

{{scriptname::scs_get_pendi
ng_by_loc}}

Please advise.

1_0//265088 �B*�0F�p0o�M-^@�QM-^IM-^H�^_�^P0o�00o� �A�<182071208/274991/273036�B*�01� 0o��^D^E
�M-^HU�^C�A�^C0o��SleeperThreadAborting^G{{username::RAB2002}}{{scriptname::scs_get_pendi
ng_by_loc}}0F�M-^H�A�^B�^F��0F�0M-^FM-^] 0o�`�QM-^E��^_� ^C�R    90F�x0o�`�R     �M-^E��0oذ0o�p�
^G(T0o�<^A�QM-^E��QM-^I�0B^P0o�<01�(0o� �^F�M-^H�M-^E��0o���^G(T�^_��^A^V0F�^D^A0o��^P  M-^H�0o�,0
o��^T^M^A^L^G^B^U[RAB2002] - [root]��R   galaxy/377126/5912�QM-^ISRV0051_10_0//265088 �B*�
0F�M-^@0pM-^GM-^@�QM-^IM-^H�^_�^P0pM-^G00pM-^G �A�<182071208/274991/273036�B*�01�M-^P0pM-^G�^D^E�M-^HU�^C�A�
^C0pM-^G�SleeperThreadAborting^G{{username::RAB2002}}{{scriptname::scs_get_pending_by_loc
}}0EM-^]��A�^B�^F��0u~�0�
^P0pM-^H`�QM-^E��^_� ^C�R        90F�M-^H0pM-^H`�R       �M-^E��0pM-^H�0pM-^Hp�^G(T0pM-^I<^A�QM-^E��QM-^I�0B^P0pM-^I<0
1�M-^X0pM-^H �^F�M-^H�M-^E��0pM-^H��^G(T�^_��^A^V0u~�^A0pM-^H�^P        M-^H�0pM-^I,0pM-^H�^T^M^A^L^G^B)[RAB20
02] - [root]��R  galaxy/1318948/5912�QM-^ISRV0051_02_0//265088 �B*�0FZ00pWM-^@�QM-^IM-^H�^_�^P0p
W00pW �A�<182071208/274991/273036�B*�01�^P0pW�^D^E�M-^HU�^C�A�^C0pW�SleeperThreadAborting�^G{{username::RAB2002}}{{scriptname::scs_get_pending_by_loc}}0Fh�A�^B�^F��0r�p0�> 0pX`
�QM-^E��^_� ^C�R         90FZ80pX`�R     �M-^E��0pX�0pXp�^G(T0pY<^A�QM-^E��QM-^I�0B^P0pY<01�^X0pX �^F�
 galaxy/889092/5912�QM-^ISRV0051_03_0//265088 �B*�01� 0pM-^GM-^@�QM-^IM-^H�^_�^P0pM-^G00pM-^G �A�<1820
71208/274991/273036�B*�01�0pM-^G�^D^E�M-^HU�^C�A�^C0pM-^G�SleeperThreadAborting^G{{username::
RAB2002}}{{scriptname::scs_get_pending_by_loc}}01�A�^B�^F��01�^P0v� 0pM-^H`�QM-^E��^_� ^C�R
         901�(0pM-^H`�R  �M-^E��0pM-^H�0pM-^Hp�^G(T0pM-^I<^A�QM-^E��QM-^I�0B^P0pM-^I<010pM-^H �^F�M-^H�M-^E
��0pM-^H��^G(T�^_��^A^V01�^T^A0pM-^H�^P M-^H�0pM-^I,0pM-^H�^T^M^A^L^G^C9[RAB2002] - [root]��R
 galaxy/1196224/5912�QM-^ISRV0051_09_0//265088 �B*�0X-00pWM-^@�QM-^IM-^H�^_�^P0pW00pW �A�<182071
208/274991/273036�B*�01�@0pW�^D^E�M-^HU�^C�A�^C0pW�SleeperThreadAborting^G{{username::RAB
2002}}{{scriptname::scs_get_pending_by_loc}}0RM-^Q��A�^B�^F��0f�`0M-^Pa@0pX`�QM-^E��^_� ^C�R
 90X-80pX`�R     �M-^E��0pX�0pXp�^G(T0pY<^A�QM-^E��QM-^I�0B^P0pY<01�H0pX �^F�M-^H�M-^E��0pX��^G(T�
^_��^A^V0f�d^A0pX�^P    M-^H�0pY,0pX�^T^M^A^L^G3^P[CAW9041] - [root]��R  galaxy/635000/5
912�QM-^ISRV0051_02_0//100040 �B*�00�00pwM-^@�QM-^IM-^H�^_�^P0pw00pw �A�8182075098/250005/100040

Ever considered using strings command?

Can you please give more hints?

strings will try to print whatever it believes is printable strings... in an true binary... If this does not work then your file is not binary or contains no true binary, but is more "data", in which case its a different story...

Do you have a sample script I can try, please?

---------- Post updated at 09:26 PM ---------- Previous update was at 12:21 PM ----------

There's a pattern of the segment I need. Is there a way to use pattern to extract them out?

What exactly are you trying to extract?

Here is a sample program which you can start working with:

awk '
        /Sleep/ {
                match ( $0, /Sleep.*/ )
                v = sprintf ( "%s", substr ( $0, RSTART, RLENGTH ) )
                getline;
                v = v $0
                sub ( /\}[^}]*$/, "}", v )
                sub ( /g[^{]*/, "g", v )
                print v
        }
' file

[quote=yoda;302888148]
What exactly are you trying to extract?

I need to extract this i.e.

SleeperThreadAborting {{username::RAB2002}} {{scriptname::scs_get_pending_by_loc}}

All of them begin with SleeperThreadAborting and end with }}.

If I can extract from SleeperThreadAborting ..... to }} , that will be perfect.
Please let me know if possible. Appreciate it.

Using strings and awk:

strings file | awk '
        /SleeperThreadAborting/ {
                v = $0
                if ( v !~ /\}\}$/ )
                {
                        getline
                        v = v $0
                        sub ( /\}[^}]*$/, "}", v )
                        sub ( /SleeperThreadAborting[^{]*/, "SleeperThreadAborting", v )
                }
                print v
        }
'
1 Like

I truly appreciate it! It works perfect!