3

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.

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

Try this sed command also

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

Output:

a8197zz

2 Comments

I'd suggest sed -n '/user/s/.*user \([^ ]\+\).*/\1/p' fileName to avoid getting spammed with the lines not containing "user".
@daniu, Thanks for your suggestion
0

Following awk may help you in same.

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

Output will be as follows.

a8197zz

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.