Score:0

How to combine grep command and sed command on the same text file and get output to another file?

mx flag

I copied the following log from grafana log explorer.

2021-06-12 21:59:41 
2021-06-12 20:59:41.118  INFO 1 --- [edElastic-14048] c.o.i.i.s.i.UserManagementServiceImpl    : BUNDLE_PURCHASE_FAILED || 213550040214 |  Failed to Do Simple Bundle Purchase  For Transaction 5001235315807102834, Error JSON decoding error: Cannot deserialize value of type

my expected output:

2021-06-12 21:59:41  213550040214 

I use command below to get output I expected:

grep -Eo "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}" bfailed.txt | grep -Eo "[0-9]{12}" | sed '/[a-zA-Z]/d' > b.txt

But this command not give any output. How I can get the expected output?

cn flag
You should check the grep man page to see what `-o` does.
cn flag
The key to debugging pipelines is to run the first command and check it's output, then add the next command in the pipeline, etc.
Score:2
cn flag

Your command gives no output because the second grep command doesn't match anything:

grep -Eo "[0-9]{12}" 

That looks for exactly 12 consecutive numbers but you never have 12 consecutive numbers because your first grep only prints out the date and time, so the rest of the line is already lost.

If your input really is just the two lines you show, then all you need is to print the 1st, 2nd and 11th field on lines that have at least 11 fields:

$ awk 'NF>10{print $1,$2,$11}' file
2021-06-12 20:59:41.118 213550040214

If you have more lines and really need to match the specific date and time format, you can try this instead:

$ sed -En 's/([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}).*([0-9]{12}) .*/\1 \2/p' file 
2021-06-12 20:59:41 213550040214

The -E enables extended regular expressions which simplify the syntax here and the -n tells sed not to print anything by default. Then, the substitution operator (s/old/new/) will try to match the formats you are looking for and capture them (the parentheses "capture" patterns) so we can replace everything with just the two matched sections (\1 \2).

Note that this will find the last stretch of 12 numbers, so it will fail if you have more than one such set. We could give you a more specific solution but you would have to give us more details about your file, what is variable and what never changes.

Mihiran Chathuranga avatar
mx flag
This is working.Acctually I am using grafana log explorer.But I do not how to write such complex Loki queries.That why I am copying logs to text file and do this way.Do you have any idea about Loki queries.
terdon avatar
cn flag
@MihiranChathuranga no. I don't know what Loki is. But that's why you need to give context in your questions.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.