Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Grep Information and Results to be in Columns(Linux)

Tags:

linux

grep

I have the below data to process such that each each block of data that starts with <SUBBEGIN and ends with <SUBEND will be processed.

<SUBBEGIN
    AARB:28E5A4ERR
    SUBSCRIBERIDENTIFIER:234810128
    GBRUL:0
    GBRDL:0
    SUBSCRIPTION:5093201&733D8E01CA&1&FFFFFFFFFFFFFF&20160729121530&FFFFFFFFFFFFFF&2&1&FFFFFFFFFFFFFFFF&255&0&255&256&FFFFFFFFFFFFFFFF&0&0&128&1
    SUBSCRIPTION:5233555&733D8E01CA&1&FFFFFFFFFFFFFF&20160730091530&FFFFFFFFFFFFFF&2&1&FFFFFFFFFFFFFFFF&255&0&255&256&FFFFFFFFFFFFFFFF&0&0&128&1
    QUOTA:5093201&733D8201D10D270D&1&1&0&1&20171121043000&20171221043000&0&255&0&0&0&0&0&0&0&0&0
    QUOTA:5233555&633D8201D10D270D&1&1&0&1&20171121043000&20171221043000&0&255&0&0&0&0&0&0&0&0&0
    ONLINESTATUS:2
    LATEST_ONLINE_TIME:20170330191209
    CREATETYPE:0
    MAXOFFLINEDAYS:0
<SUBEND
<SUBBEGIN
    BBRB:28E5A4ETT
    SUBSCRIBERIDENTIFIER:234825528
    GBRUL:0
    GBRDL:0
    SUBSCRIPTION:5093333&733D8E01CA&1&FFFFFFFFFFFFFF&20160729121530&FFFFFFFFFFFFFF&2&1&FFFFFFFFFFFFFFFF&255&0&255&256&FFFFFFFFFFFFFFFF&0&0&128&1
    QUOTA:5093264&733D8201D10D270D&1&1&0&1&20171121043000&20171221043000&0&255&0&0&0&0&0&0&0&0&0
    ONLINESTATUS:2
    LATEST_ONLINE_TIME:20170330191209
    CREATETYPE:0
    MAXOFFLINEDAYS:0
<SUBEND
<SUBBEGIN
    CCRB:28E5A4ESS
    SUBSCRIBERIDENTIFIER:234993527
    GBRUL:0
    GBRDL:0
    SUBSCRIPTION:5093264&733D8E01MB&1&FFFFFFFFFFFFFF&20160729121530&FFFFFFFFFFFFFF&2&1&FFFFFFFFFFFFFFFF&255&0&255&256&FFFFFFFFFFFFFFFF&0&0&128&1
    SUBSCRIPTION:9093201&733D8E01CA&1&FFFFFFFFFFFFFF&20160729121530&FFFFFFFFFFFFFF&2&1&FFFFFFFFFFFFFFFF&255&0&255&256&FFFFFFFFFFFFFFFF&0&0&128&1
    SUBSCRIPTION:6598777&733D8E01CA&1&FFFFFFFFFFFFFF&20160730091530&FFFFFFFFFFFFFF&2&1&FFFFFFFFFFFFFFFF&255&0&255&256&FFFFFFFFFFFFFFFF&0&0&128&1
    QUOTA:5093264&733D8201D10D270D&1&1&0&1&20171121043000&20171221043000&0&255&0&0&0&0&0&0&0&0&0
    QUOTA:9093201&733D8201D10D270D&1&1&0&1&20171121043000&20171221043000&0&255&0&0&0&0&0&0&0&0&0
    QUOTA:6598777&633D8201D10D270D&1&1&0&1&20171121043000&20171221043000&0&255&0&0&0&0&0&0&0&0&0
    ONLINESTATUS:2
    LATEST_ONLINE_TIME:20170330191209
    CREATETYPE:0
    MAXOFFLINEDAYS:0
<SUBEND

I have used this linux command already: grep -e SUBSCRIBERIDENTIFIER -e SUBSCRIPTION -e QUOTA But I got result in rows, whereas I need it to be in columns as below(please copy the below expectation to ultredit program to see the full length):

SUBSCRIBERIDENTIFIER:234810128 SUBSCRIPTION:5093201 SUBSCRIPTION:5233555                        QUOTA:5093201 QUOTA:5233555
SUBSCRIBERIDENTIFIER:234825528 SUBSCRIPTION:5093333                                             QUOTA:5093264
SUBSCRIBERIDENTIFIER:234993527 SUBSCRIPTION:5093264 SUBSCRIPTION:9093201 SUBSCRIPTION:6598777   QUOTA:5093264 QUOTA:9093201 QUOTA:6598777

Kindly help out.

like image 249
Oluseyi Victor Avatar asked Dec 14 '25 18:12

Oluseyi Victor


2 Answers

using awk

$ awk -F"&" '/^<SUBBEGIN$/{a=1} a && /^[[:blank:]]+(SUBSCRIBERIDENTIFIER|SUBSCRIPTION|QUOTA)/{l=l OFS $1} a && /^<SUBEND$/ {print l; a=l=""}' f1
     SUBSCRIBERIDENTIFIER:234810128     SUBSCRIPTION:5093201     SUBSCRIPTION:5233555     QUOTA:5093201     QUOTA:5233555
     SUBSCRIBERIDENTIFIER:234825528     SUBSCRIPTION:5093333     QUOTA:5093264
     SUBSCRIBERIDENTIFIER:234993527     SUBSCRIPTION:5093264     SUBSCRIPTION:9093201     SUBSCRIPTION:6598777     QUOTA:5093264     QUOTA:9093201     QUOTA:6598777

/^<SUBBEGIN$/{a=1} : If record is <SUBBEGIN then set flag a which marks the beginning of the block

a && /^[[:blank:]]+(SUBSCRIBERIDENTIFIER|SUBSCRIPTION|QUOTA)/{l=l OFS $1} : If a is set and record matches one of the words you specified in your original question then append the record upto first field to variable l (Note : FS="&")

a && /^<SUBEND$/ {print l; a=l=""}' : If a is set and block end reached then reset all variables.

like image 142
Rahul Verma Avatar answered Dec 16 '25 14:12

Rahul Verma


Try:

$ awk -F'&' '{sub(/^[[:blank:]]+/,"")} /SUBSCRIBERIDENTIFIER/{id=$1} /SUBSCRIPTION/{s=s","$1} /QUOTA/{q=q","$1} /SUBEND/{print id,s,q; id=""; s=""; q=""}' data | column -t | tr , ' '
SUBSCRIBERIDENTIFIER:234810128   SUBSCRIPTION:5093201 SUBSCRIPTION:5233555                        QUOTA:5093201 QUOTA:5233555
SUBSCRIBERIDENTIFIER:234825528   SUBSCRIPTION:5093333                                             QUOTA:5093264
SUBSCRIBERIDENTIFIER:234993527   SUBSCRIPTION:5093264 SUBSCRIPTION:9093201 SUBSCRIPTION:6598777   QUOTA:5093264 QUOTA:9093201 QUOTA:6598777

How it works

  • -F'&'

    This tells awk to use & as the field separator.

  • sub(/^[[:blank:]]+/,"")

    This removes all leading blanks from each line.

  • /SUBSCRIBERIDENTIFIER/{id=$1}

    This saves the SUBSCRIBERIDENTIFIER information in variable id.

  • /SUBSCRIPTION/{s=s","$1}

    This saves all SUBSCRIPTION information in comma-separated format in the variable s.

  • /QUOTA/{q=q","$1}

    This saves all QUOTA information in comma-separated format in the variable q.

  • /SUBEND/{print id,s,q; id=""; s=""; q=""}

    Everytime we reach SUBEND, we print out a line of data and reset all variables

  • column -t

    This pretty-formats the output data into columns.

  • tr , ' '

    This removes the commas from the output.

Update

According to your comment, the name of the file you are using is UPCC_5_20171124144300_0_huabiao1_601.txt and you want to append the results in the file Result.txt. In that case, run:

awk -F'&' '{sub(/^[[:blank:]]+/,"")} /SUBSCRIBERIDENTIFIER/{id=$1} /SUBSCRIPTION/{s=s","$1} /QUOTA/{q=q","$1} /SUBEND/{print id,s,q; id=""; s=""; q=""}' UPCC_5_20171124144300_0_huabiao1_601.txt | column -t | tr , ' ' >>Result.txt

Note that the cat command is not needed here.

like image 40
John1024 Avatar answered Dec 16 '25 14:12

John1024



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!