2

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!

Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111
Lucil120
  • 63
  • 5

1 Answers1

1

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>
Marc Lambrichs
  • 2,864
  • 2
  • 13
  • 14
  • tks Marc it's work. But i have another problem i have many line that in file, which contain many Jobs so that output will have many line too, there for the awk command doesn't work well i wondering my output can have many line like my desire output? – Lucil120 Nov 03 '17 at 07:22
  • Added solution to my answer. – Marc Lambrichs Nov 03 '17 at 07:52
  • Thanks Marc if i using that awk file do i have to use grep command? Because if i just only use the awk file that the output just look like the input file. Or the input file of that awk file is the output file after using grep command? i tried both but it doesn't work as well – Lucil120 Nov 03 '17 at 08:08
  • No, you don't have to use grep with that. Check if you have GNU awk installed. – Marc Lambrichs Nov 03 '17 at 08:15
  • oh no i try it again with using my simple input on the question and your code work perfectly i just don't know why it doesn't work with my real input. – Lucil120 Nov 03 '17 at 08:24
  • Is that input file somehow different from the input line in the question? Just cut and paste 2 lines to show me. – Marc Lambrichs Nov 03 '17 at 08:27
  • to change multiple lines to single, use paste... for ex: `seq 5 | paste -sd' '` – Sundeep Nov 03 '17 at 08:36
  • Job <472971> Job Name User Project Status Queue Interactive pseudo-terminal shell mode Command Wed Nov 1 13:50:57: Submitted from host CWD 6 Processors Requested Job <456750> Job Name User Project Status Queue Interactive pseudo-terminal shell mode Command Tue Oct 31 18:03:38: Submitted from host CWD Requested Resources – Lucil120 Nov 03 '17 at 08:38
  • in the first line Interactive i think that is the problem. – Lucil120 Nov 03 '17 at 08:40
  • @Lucil120 I've put these 2 lines in a file2 and called `awk -f tst.awk file2`. Works perfectly. Output: `<472971> NEWLINE<456750> `. The only thing I can image is that your jobs are not printed each on 1 line. – Marc Lambrichs Nov 03 '17 at 08:41
  • @Marc i've put these 2 lines in another file and called awk file but it's doesn't work and i don't know why @@ – Lucil120 Nov 03 '17 at 08:47
  • what's your awk version? – Marc Lambrichs Nov 03 '17 at 08:48
  • Ow. That's pretty old. Can you upgrade? FPAT was introduced in 4.0. – Marc Lambrichs Nov 03 '17 at 08:53
  • oh no i can't upgrade it. the company doesn't let me upgrade anything btw thanks for your support Marc, it's help me alot. yeah minutes ago i wonder what is FPAT – Lucil120 Nov 03 '17 at 08:56