How to extract specific values/fields from the text file?
Using grep
$ grep -oE '(EventCorrelationId|CreationTime|SubscriberNumber)[^ ]*' textfile
EventCorrelationId="615-493|-1899671563||1550927718000"
CreationTime="20190225094504"
SubscriberNumber=92705073362
Using awk
Try:
$ awk -v RS=' ' '/^EventCorrelationId=/ || /^CreationTime=/ || /^SubscriberNumber=/' textfile
EventCorrelationId="615-493|-1899671563||1550927718000"
CreationTime="20190225094504"
SubscriberNumber=92705073362
How it works
-v RS=' '
This tells awk to use a blank as the record separator.
/^EventCorrelationId=/ || /^CreationTime=/ || /^SubscriberNumber=/
This tells awk to print the record if it matches any of these three regular expressions. Some notes:
In a regex,
^
means beginning-of-the-record. Thus/^CreationTime=/
means a record that starts withCreationTime=
In awk, like many languages,
||
means logical-or. Thus^EventCorrelationId=/ || /^CreationTime=/
is true if either regex is matched.
Using sed
$ sed -En 's/.*(EventCorrelationId=[^ ]*).*(CreationTime=[^ ]*).*(SubscriberNumber=[^ ]*).*/\1\n\2\n\3/p' textfile
EventCorrelationId="615-493|-1899671563||1550927718000"
CreationTime="20190225094504"
SubscriberNumber=92705073362
Using an alternate separator in the output
To use |
as the output separator in place of newline:
$ sed -En 's/.*(EventCorrelationId=[^ ]*).*(CreationTime=[^ ]*).*(SubscriberNumber=[^ ]*).*/\1|\2|\3/p' textfile
EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362
$ grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file
EventCorrelationId="615-493|-1899671563||1550927718000"
CreationTime="20190225094504"
SubscriberNumber=92705073362
grep -Eo
grep in extended regular expression mode (less escaping of special chars) and print only the matched parts(EventCorrelationId|CreationTime|SubscriberNumber)
match EventCorrelationId or CreationTime or SubscriberNumber=[^ ]+
followed by "=", followed by non-space-character one or more times
Edit1:
Now separated by "|":
$ echo $(grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file) | tr ' ' '|'
EventCorrelationId="615-493|-1899671563||1550927718000"|CreationTime="20190225094504"|SubscriberNumber=92705073362
Edit2:
Now separated by "|" in reversed order using tac
:
$ echo $(grep -Eo '(EventCorrelationId|CreationTime|SubscriberNumber)=[^ ]+' file | tac) | tr ' ' '|'
SubscriberNumber=92705073362|CreationTime="20190225094504"|EventCorrelationId="615-493|-1899671563||1550927718000"