LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-25-2014, 11:42 PM   #16
Scottish_Jason
LQ Newbie
 
Registered: Jan 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled

why doesn't this just work.... added another grep to get everything between 'slsk' and '.mp3' instead of both lines ??


Code:
file=$(grep -B 1 -P 'Size: [\b,0-3][0-5][0-9][0-9][0-9]KB.*Bitrate: [34][28][0-9].*Length: (?!0:00)' result.txt | grep 'slsk' | grep '(?=slsk)(.*\n?)(?<=.mp3)' )
edit: Finally got it... not sure what fixed it to be honest, its really late here in scotland yawn

Last edited by Scottish_Jason; 11-25-2014 at 11:55 PM.
 
Old 11-26-2014, 12:10 AM   #17
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
have a look at this - you'll need to adjust the tests, I used values appropriate for testing
Code:
awk  '/slsk/ {sve=$0 ; next} ; {good=match($0, /.* ([[:digit:]]+)KB.*rate: ([[:digit:]]+) Length: ([[:digit:]]+):([[:digit:]]{2}).*/, a) ; if (good && a[1] > 3500 && a[1] < 10000 && a[2] > 100 && (a[3] && a[4])) {print sve"\n"$0}}' bitrate.txt
Edit: a[1] is size, a[2] is bitrate, a[3] and a[4] are length - leave those last 2 alone.

Last edited by syg00; 11-26-2014 at 12:18 AM.
 
1 members found this post helpful.
Old 11-26-2014, 01:30 AM   #18
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
May I suggest a slight tweak to the awk solution:
Code:
awk  '/slsk/ {sve=$0 ; next}
            sve && match($0, /([[:digit:]]+)KB.*rate: ([[:digit:]]+) Length: ([[:digit:]]+):([[:digit:]]{2})/, a){
              if(a[1] > 3500 && a[1] < 10000 && a[2] > 100 && (a[3] || a[4]))
                print sve
            }sve = ""' file
Now there are some issues surrounding Length and your criteria. As a[3] and a[4] are the portions of the time / length element, you could have values of a[3] = 1 and a[4] = 00.
Based on the criteria, in total 1:00 is greater than 0, however the logic here wants them both to be not 0. So I changed that part to be an OR

Other changes are cosmetic at some level but may also help speed up process:

1. By using 'sve' in line 2 as part of the test it will only start looking for values should the previous line contain 'slsk'

2. I removed the superfluous '.*' from the start and the end ... no real value added

3. By adding match out the front we remove the need to assign to variable 'good'. The return value is greater than 0 only if it finds a match

4. I wasn't sure with your new grep as to which part of the original line you needed. If you could demonstrate we could us a simple substr / gensub to get the data required

5. 'sve' is reset to blank so as to not start match if not needed
 
1 members found this post helpful.
Old 11-26-2014, 01:38 AM   #19
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Always happy to be corrected.

I tend to not reset saved portions to acccommodate possible multiple following entries per "saved" - seems the (more) common case. As for "good", that's just habit ...
 
Old 11-26-2014, 01:56 AM   #20
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@syg00 - all good I do see your point on the resetting ... this would of course lead to the OP supplying example data as opposed to only solution data
 
Old 11-26-2014, 04:30 PM   #21
Scottish_Jason
LQ Newbie
 
Registered: Jan 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
@syg00 - all good I do see your point on the resetting ... this would of course lead to the OP supplying example data as opposed to only solution data
Guys thanks for the suggestions, a lot of it is over my head as I am quite new to this all.
What do you mean by example data instead of solution data?

the data supplied is what I get when piping the output of an application (to results.txt) that does a search and supplies the data as shown.
 
Old 11-26-2014, 06:00 PM   #22
Scottish_Jason
LQ Newbie
 
Registered: Jan 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
May I suggest a slight tweak to the awk solution:
Code:
awk  '/slsk/ {sve=$0 ; next}
            sve && match($0, /([[:digit:]]+)KB.*rate: ([[:digit:]]+) Length: ([[:digit:]]+):([[:digit:]]{2})/, a){
              if(a[1] > 3500 && a[1] < 10000 && a[2] > 100 && (a[3] || a[4]))
                print sve
            }sve = ""' file
Now there are some issues surrounding Length and your criteria. As a[3] and a[4] are the portions of the time / length element, you could have values of a[3] = 1 and a[4] = 00.
Based on the criteria, in total 1:00 is greater than 0, however the logic here wants them both to be not 0. So I changed that part to be an OR

Other changes are cosmetic at some level but may also help speed up process:

1. By using 'sve' in line 2 as part of the test it will only start looking for values should the previous line contain 'slsk'

2. I removed the superfluous '.*' from the start and the end ... no real value added

3. By adding match out the front we remove the need to assign to variable 'good'. The return value is greater than 0 only if it finds a match

4. I wasn't sure with your new grep as to which part of the original line you needed. If you could demonstrate we could us a simple substr / gensub to get the data required

5. 'sve' is reset to blank so as to not start match if not needed
thanks for the solution!

Last edited by Scottish_Jason; 11-26-2014 at 06:26 PM.
 
Old 11-26-2014, 08:20 PM   #23
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by Scottish_Jason View Post
Guys thanks for the suggestions, a lot of it is over my head as I am quite new to this all.
What do you mean by example data instead of solution data?

the data supplied is what I get when piping the output of an application (to results.txt) that does a search and supplies the data as shown.
Hi Jason, I was referring to the examples you had provided and the fact that they all match out criteria. So we have no examples
that should be excluded by our code. So currently we are testing only for success and not seeing ones that might be close to matching but little
things should actually be dealt with.

If the solution is working though ... good work

Once you think you have a working solution, remember to mark your question as SOLVED.
 
Old 11-26-2014, 09:49 PM   #24
Scottish_Jason
LQ Newbie
 
Registered: Jan 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Hi Jason, I was referring to the examples you had provided and the fact that they all match out criteria. So we have no examples
that should be excluded by our code. So currently we are testing only for success and not seeing ones that might be close to matching but little
things should actually be dealt with.

If the solution is working though ... good work

Once you think you have a working solution, remember to mark your question as SOLVED.
Actually yes, it appears to be working well, I just need to somehow cut the number from the output so I'm only left with a result starting from "slsk" and not [67] slsk://blabla.mp3

any suggestions? This is what I currently have...... thanks to you guys

Code:
awk  '/slsk/ {sve=$0 ; next}
            sve && match($0, /([[:digit:]]+)KB.*rate: ([[:digit:]]+) Length: ([[:digit:]]+):([[:digit:]]{2})/, a){
              if(a[1] > 5000 && a[1] < 30000 && a[2] > 319 && (a[3] || a[4]))
                print sve
            }sve = ""' result.txt
 
Old 11-27-2014, 12:24 AM   #25
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Change first line, inside braces, to:
Code:
sve = gensub(/^[^ ]* /,"",1); next
 
1 members found this post helpful.
Old 11-27-2014, 12:29 AM   #26
Scottish_Jason
LQ Newbie
 
Registered: Jan 2014
Posts: 15

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Change first line, inside braces, to:
Code:
sve = gensub(/^[^ ]* /,"",1); next
Great, thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary codes in Bash regular expression. sanktwo Linux - Newbie 12 09-27-2012 07:39 AM
[SOLVED] BASH - regular expression elalexluna83 Programming 3 09-12-2012 10:23 AM
[SOLVED] bash script using regular expression edwardcode Programming 5 05-31-2012 02:07 AM
Bash Script / Regular Expression Problem rm_-rf_windows Linux - General 4 03-28-2012 01:05 PM
[SOLVED] [bash] rm regular expression help RaptorX Programming 26 08-01-2009 06:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration