Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep with delimiters in shell script

Hi I have a log file and I am doing grep operations like this.

cat logfile | grep select

2018-03-20T15:26:34,397 INFO  [2da4e66f-6092-46a7-9542-60afc0611205 HiveServer2-Handler-Pool: Thread-32([])]: ql.Driver (Driver.java:compile(429)) - Compiling command(queryId=hive_20180320152634_a6ef02a1-e018-4085-8ceb-8a8d2733b427): select * from reportingperiod limit 5
2018-03-20T15:26:37,761 INFO  [HiveServer2-Background-Pool: Thread-35([])]: ql.Driver (Driver.java:execute(1735)) - Executing command(queryId=hive_20180320152634_a6ef02a1-e018-4085-8ceb-8a8d2733b427): select * from reportingperiod limit 5

I am trying to extract the query part based on the delimiter like this

delimiter based extract, first delimiter ': ' upto '\n'

So that I can get the expect output like this.

select * from reportingperiod limit 5

Initially the query extract I tried with regex and grep -OE methods. Right now I am trying to implement a generic method, So that any query will be captured.

I tried like this.

IFS=$': '
for i in `cat logfile`; do    echo $i; 
done

The above code is not working as expected, Since I don't know how to pass the second delimiter to extract the query. Any help will be appreciated.

like image 789
Teju Priya Avatar asked Feb 13 '26 14:02

Teju Priya


1 Answers

You could probably use cut as well.

My opinion is that you should use the most simple tool that satisfies your requirements, so this is probably a bit better.

print all, after first :

cut -d ':' -f 2- <file>

print stuff after first 2 fields.

echo xxx:yyy:zzz | cut -d ':' -f -2 --complement
like image 125
mjz19910 Avatar answered Feb 15 '26 04:02

mjz19910



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!