Saturday, February 5, 2022

[SOLVED] print only the pattern in the whole line using grep

Issue

As i know to print only the pattern using command grep -o. i have a file like this:

Job <472971> Job Name <aaaaaaaaaaa> User <bbbbbbbbb> Project <cccccc> Status <RUN> Queue <AL_Ptime>Interactive pseudo-terminal shell mode Submitted from host <lsf_login07> CWD </asdfghjklll/dsadasd/asda>

i want to print out all the pattern inside < > , my desire output look like this:

<472971> <aaaaaaaaaaa> <bbbbbbbbb> <cccccc> <RUN> <AL_Ptime> <lsf_login07> </asdfghjklll/dsadasd/asda>

I tried a lot but i can print out exactly pattern < >.

Thanks for your help!


Solution

Something like grep -oE '<[^>]+>':

$ echo "Job <472971> Job Name <aaaaaaaaaaa> User <bbbbbbbbb> Project <cccccc> Status <RUN> Queue <AL_Ptime>Interactive pseudo-terminal shell mode Submitted from host <lsf_login07> CWD </asdfghjklll/dsadasd/asda>" \
| grep -oE '<[^>]+>'
<472971>
<aaaaaaaaaaa>
<bbbbbbbbb>
<cccccc>
<RUN>
<AL_Ptime>
<lsf_login07>
</asdfghjklll/dsadasd/asda>

If you want to get rid of the newlines and have spaces in between, you can pipe this into a simple awk like this:

grep -oE '<[^>]+>' |  awk '{printf "%s%s", (NR==1?"":" "), $0}'

or with tr:

grep -oE '<[^>]+>' | tr "\n" " "

EDIT: Suppose OP uses an input file with a number of these lines, one could turn to GNU awk:

$ cat tst.awk
BEGIN { FPAT="<[^>]+>" }
{ for (i=1; i<=NF; i++) printf "%s%s", (i==1?"":" "), $i
  printf "\n"
}

Using it:

awk -f tst.awk file

where file is:

$ cat file
Job <472971> Job Name <aaaaaaaaaaa> User <bbbbbbbbb> Project <cccccc> Status <RUN> Queue <AL_Ptime>Interactive pseudo-terminal shell mode Submitted from host <lsf_login07> CWD </asdfghjklll/dsadasd/asda>
Job <472971> Job Name <aaaaaaaaaaa> User <bbbbbbbbb> Project <cccccc> Status <RUN> Queue <AL_Ptime>Interactive pseudo-terminal shell mode Submitted from host <lsf_login07> CWD </asdfghjklll/dsadasd/asda>

or, as a one-liner:

$ awk 'BEGIN{FPAT="<[^>]+>"} \
        {for (i=1; i<=NF; i++) printf "%s%s", (i==1?"":" "), $i; printf "\n"}' file
<472971> <aaaaaaaaaaa> <bbbbbbbbb> <cccccc> <RUN> <AL_Ptime> <lsf_login07> </asdfghjklll/dsadasd/asda>
<472971> <aaaaaaaaaaa> <bbbbbbbbb> <cccccc> <RUN> <AL_Ptime> <lsf_login07> </asdfghjklll/dsadasd/asda>


Answered By - Marc Lambrichs
Answer Checked By - Marie Seifert (WPSolving Admin)