sumit gupta
sumit gupta

Reputation: 3

print two different fields of files using

I have a LOG:

100.100.100.100 - - [08/Mar/2016:07:53:33 +0100] GET /rbt/Utils.do?receiverID=665135085&action=send_sms&senderID=22077&dtocRequest=n&smsText=Tu+PIN+es+95934&isPostMethod=n&subscriberID=665135085&useSameResForConsent=n&mode=WEB

I need to display only two fields. Date and subscriberID field (only digits).

I am trying below commands:

cut -d" " -f4,7 input.txt | awk -F"subscriberID=" '{print $2}' input.txt | cut -c0-9

My output:

665135085

Required output:

08/Mar/2016:07:53:33 +0100 665135085

Kindly help.

Upvotes: 0

Views: 36

Answers (1)

alexis
alexis

Reputation: 50190

This is a job for sed:

sed -E 's/.*\[([^]]*)\].*subscriberID=([0-9]*).*/\1 \2/' input.txt

If you have a weird sed that doesn't support "extended" regular expressions (enabled by the -E flag), you have to add a \ before each opening and closing paren:

sed 's/.*\[\([^]]*\)\].*subscriberID=\([0-9]*\).*/\1 \2/' input.txt

Upvotes: 1

Related Questions