Arun Kumar
Arun Kumar

Reputation: 35

extract the value from the log using shell script

Hi I am new to shell script. I am trying to extract the specific value from the log.

When I filter the data by using specific keyword. It looks like these.

cat hive-server2.log | grep user

The output of the data is

2018-01-18T16:20:39,464 WARN  [67272380-f3e9-40da-8e8e-a209c05eb4fe HiveServer2-Handler-Pool: Thread-37([])]: util.CurrentUserGroupInformation (CurrentUserGroupInformation.java:getGroupNameFromUser(52)) - user a8197zz (auth:PROXY) via hive (auth:SIMPLE) has no primary groupName, setting groupName to be a8197zz.

In the above data I want to extract the specific value for the user like this.

a8197zz

I tried like this.

awk 'BEGIN{ print "User" }
 /\<user\>/{ u=$10 }
 //{ print u }' OFS=',' hive-server2.log

It prints blank lines only. Any help will be appreciated.

Upvotes: 3

Views: 1228

Answers (3)

Cyrus
Cyrus

Reputation: 88563

With GNU grep:

grep -Po '[^ ]*(?=\.$)' file

or

grep -Po 'user \K[^ ]*' file

With awk:

awk -F "[. ]" '{print $(NF-1)}' file

or

awk -F "user " '{split($2,array," "); print array[1]}' file

or search for field with string user and print next field:

awk '{for(i=1; i<=NF; i++) if ($i=="user") print $(i+1) }' file

Output:

a8197zz

Upvotes: 2

Kalanidhi
Kalanidhi

Reputation: 5092

Try this sed command also

sed 's/.*user \([^ ]\+\).*/\1/' fileName

Output:

a8197zz

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133428

Following awk may help you in same.

awk '{sub(/\./,"",$NF);print $NF}'  Input_file

Output will be as follows.

a8197zz

Upvotes: 0

Related Questions