How to grep a portion of line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep a portion of line
# 1  
Old 02-08-2012
How to grep a portion of line

Mysql log has something like:

Quote:
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
1649560 Query select * from actions where uniqueid like 'ernie-1328697839.1233158-%-dequeue' and class='queue' and server='ernie' and time<='1328698322' and action='dequeue' limit 1
I want to grep only the portion "ernie-1328697839.1233158" from each line. How to do this?
# 2  
Old 02-08-2012
Suppose you have the log ina file called "file", here's the code you want to execute:
Code:
awk -F "'" '{ print $2 }' file | awk -F "-%" '{ print $1 }'

This User Gave Thanks to admin_xor For This Post:
# 3  
Old 02-08-2012
Now i want a script that will scan the mysql.log file and if it finds 5 consecutive instances of the same line then it would execute certain commands.
# 4  
Old 02-08-2012
Code:
if [ `sed "s/.*\(ernie-1328697839.1233158\).*/\1/" mysql.log | wc -l` -ge 5 ]; then echo yes; fi

This User Gave Thanks to balajesuri For This Post:
# 5  
Old 02-08-2012
To be more precise:

1) The script will scan the mysql.log file in real time(something like tail -F mysql.log)
2) If it encounters 5 consecutive identical lines then it would invoke some commands.

Any help will be highly appreciated.

Last edited by proactiveaditya; 02-08-2012 at 07:54 PM..
# 6  
Old 02-08-2012
1. Your requirements keep changing over the posts.
2. One way to achieve requirement mentioned in post #5 is to write a script that will keep scanning the file until a certain condition is met.
# 7  
Old 02-09-2012
Quote:
Originally Posted by balajesuri
1. Your requirements keep changing over the posts.
2. One way to achieve requirement mentioned in post #5 is to write a script that will keep scanning the file until a certain condition is met.
1) Extremely sorry for that.
2) How? Can you help with the basic logic.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I separate a portion of a file name to use grep on?

I'm trying to write a script that takes a file name in the form of Name_Num1_Num2.Extension and I want to separate the name portion and then use grep to see if that name part has any illegal characters in it. I already have my grep command written and it works, I'm not sure how to separate the... (5 Replies)
Discussion started by: steezuschrist96
5 Replies

2. Shell Programming and Scripting

Shell to display portion of a line

Thanks a lot for the code and the explanation. Now my final requirement. I have uploaded 3 files as attachment. Please open the files in Editplus or any other text editor which keeps the formatting. GMDCOM.27936.log.txt------I want to pick only Process request from this file.(Please check... (9 Replies)
Discussion started by: ghosh_tanmoy
9 Replies

3. UNIX for Dummies Questions & Answers

Grab Portion of Output Text (sed, grep, awk?)

Alright, here's the deal. I'm running the following ruby script (output follows): >> /Users/name/bin/acweather.rb -z 54321 -o /Users/name/bin -c Clouds AND Sun 57/33 - Mostly sunny and cool I want to just grab the "57/33" portion, but that's it. I don't want any other portion of the line. I... (5 Replies)
Discussion started by: compulsiveguile
5 Replies

4. Shell Programming and Scripting

Print portion line in SED

Hi, This is more a theoretical question, because I usually solved that with perl or even java, but I would like to know if it exists an easy way to do it with SED. Using regular expresions it's very easy to select an portion line. Does it exist an easy way for printing those portions in SED?... (1 Reply)
Discussion started by: islegmar
1 Replies

5. UNIX for Advanced & Expert Users

Grep a portion of the log file

I want to grep a portion of the log file. grepping a particular pattern and including 10 lines before that and after that occurence. grep -n "SomeString Pattern" filename 10 lines before this occurence and 10 lines after that. Please help. Need the simple script not in awk or sed. (9 Replies)
Discussion started by: sainipardeep
9 Replies

6. Shell Programming and Scripting

Grep certain portion from the file

Dear Friends, Here I am with another difficulty. I have a flat file from which I wanna grep following pattern. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Statement Date : Blah blah Blah blah Blah blah Blah blah... (1 Reply)
Discussion started by: anushree.a
1 Replies

7. Shell Programming and Scripting

Print a portion of a line

Hi, I am facing a little problem... I have a line like this : asdcvashfasashXXXXxxxzxcadd:sdcashjqdasdsmgdkdaxdsnd; I want to print just a portion of line i.e starting from left 5 characters from ":" and upto ";" i.e. in this case it would be "xcadd:sdcashjqdasdsmgdkdaxdsnd;" The length of... (2 Replies)
Discussion started by: vanand420
2 Replies

8. Shell Programming and Scripting

Delete a portion of a line using shell scripting

Hi all, I am new to awk programs.I have a file like this 1234567@2345||adcbdefhij: asgdfdasdfhhfd-asdfasd-dsfasdf |0.678|0.0|0.213 1234567@2345||adcbdefhij: ashhfd-asdfasd-dsfasdf |0.129|0.0|0.411 1234567@2345||adcbdefhij: asd-aasd-dasdf |0.223|0.0|0.276 I want to delete the text which... (3 Replies)
Discussion started by: Loy81
3 Replies

9. UNIX for Dummies Questions & Answers

erasing portion of line with sed (only once)

hi, I'm trying to use sed to erase everything, up to the first parenthesis. for example: input: blah blah blah (aldj) test (dafs) test test. output: (aldj) test (dafs) test test. how would i do this? I was fooling around with the parenthesis, and i only got it to apply on all parenthesis.... (1 Reply)
Discussion started by: gammaman
1 Replies

10. UNIX for Dummies Questions & Answers

erasing portion of line with sed

hi, I'm trying to use sed to erase everything, and including the ending parenthesis. For example: input: blah blah blah (12355)this is what i want. output: this is what i want. how would i do this? i found an example online that does the opposite: sed \"s|test.*||g\" file1 > file2"; ... (5 Replies)
Discussion started by: gammaman
5 Replies
Login or Register to Ask a Question