LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-22-2013, 11:14 AM   #16
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

Well as far as I can see I think the follow solutions work. I have tested for multiple findings but not for overlapping:

Awk:
Code:
#!/usr/bin/awk -f

$0 ~ search{
	while(++j <= i)
		print save[j]
	
	j=0
	x=1
}

x{
	if(x++ <= BandA+1)
		print
	else{
		x=0
		print "---------------------"
	}
}

{
	if(i<BandA)
		save[++i]=$0
	else{
		for(k=2;k<=BandA;k++)
			save[k-1]=save[k]
		
		save[BandA]=$0
	}
}
Usage:
Code:
./script.awk -vsearch="Audi" -vBandA=10 file
Ruby:
Code:
#!/usr/bin/env ruby

BEGIN{
	size = ARGV[0].to_i
	search = ARGV[1]
	fname = ARGV[2]

	save = Array.new(size)
	next_10 = false
	cnt = 1
}

File.open(fname,'r'){
	|file|

	while file.gets

		if next_10
			puts $_
			if (cnt += 1) > size
				next_10 = false
				cnt = 1
				puts '---------------'
			end
		end

		if $_.match(search)
			puts save.join + $_
			next_10 = true
		end

		save = save.drop(1) if save.length == size
		save<<$_

	end

}
Usage:
Code:
./script.rb 10 Audi file
Neither code has too much error checking so I will leave that to the OP.
Also the dashes (-----) are just to see where a record was ending
 
Old 07-23-2013, 02:23 AM   #17
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Original Poster
Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Hi grail,

Thanks for the helpful script but when I run the script it give an error:

Quote:
awk: syntax error near line 3
awk: bailing out near line 3
Please advice.
 
Old 07-23-2013, 02:29 AM   #18
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Original Poster
Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Quote:
I read your first post VERY carefully, three times.
Is there some hidden meaning?
Maybe I am just not intelligent enough to understand your needs.
I apologize for wasting your time.
I know about the grep command daniel. But the thing is -C switch which you mentioned in your one of the posts do not support on my server. Also we're not authorized to upgrade the package to latest version.
That's why I'm here to get some help. And I'm not that much proficient in shell scripting, that's why I'm seeking some help from you guys.
And nothing is hidden.
I know you're intelligent buddy...
And no need to apologize buddy.. It's ok!
 
Old 07-23-2013, 02:37 AM   #19
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by Satyaveer Arya View Post
Hi grail,

Thanks for the helpful script but when I run the script it give an error:
Quote:
awk: syntax error near line 3
awk: bailing out near line 3
Please advice.
I just tried grails awk script and it works nicely without throwing any errors.

- Did you copy paste it correctly (as a plain text file)?
- Which awk / awk version are you using (awk --version)
 
Old 07-23-2013, 02:40 AM   #20
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Original Poster
Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Hi Schniedz,

I used your mentioned script also:
Quote:
linenum=`grep -n <HandBagCnt>END MessageForwarder.log.2013-07-03 | awk -F : '{print $1}'`
lo=`expr $linenum - 10`; hi=`expr $linenum + 10`
sed -n "$lo","$hi"p MessageForwarder.log.2013-07-03
Here I'm searching for <HandBagCnt>END string in MessageForwarder.log.2013-07-03 file. And MessageForwarder.log.2013-07-03 log file is of 7.8GB.

I just tried to run this above script like this:
Code:
# ./script
And it gave me this error:
Quote:
./script: HandBagCnt: cannot open
awk: syntax error near line 1
awk: bailing out near line 1
sed: illegal option -- 1
Please advise.

Last edited by Satyaveer Arya; 07-23-2013 at 02:42 AM.
 
Old 07-23-2013, 02:55 AM   #21
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by Satyaveer Arya View Post
Hi Schniedz,

I used your mentioned script also:


Here I'm searching for <HandBagCnt>END string in MessageForwarder.log.2013-07-03 file. And MessageForwarder.log.2013-07-03 log file is of 7.8GB.

I just tried to run this above script like this:
Code:
# ./script
And it gave me this error:


Please advise.
Code:
linenum=`grep -n "<HandBagCnt>END" MessageForwarder.log.2013-07-03 | awk -F : '{print $1}'`
lo=`expr $linenum - 10`; hi=`expr $linenum + 10`
sed -n "$lo","$hi"p MessageForwarder.log.2013-07-03
you need to use quotes, < and > are 'special' to the shell
It is trying to use file HandBagCnt as input and send output to the file END

http://www.tldp.org/LDP/abs/html/io-redirection.html

Which is why you get the error
>> ./script: HandBagCnt: cannot open
 
Old 07-23-2013, 03:25 AM   #22
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Original Poster
Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Hi druuna,

Quote:
- Did you copy paste it correctly (as a plain text file)?
- Which awk / awk version are you using (awk --version)
Yes I used the same script as mentioned by him.
And I used many methods which I know so far to check the version of awk, but none of them worked.
Can you please tell me how to check the version of awk on Solaris 10 ?
 
Old 07-23-2013, 03:36 AM   #23
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by Satyaveer Arya View Post
And I used many methods which I know so far to check the version of awk, but none of them worked.
Can you please tell me how to check the version of awk on Solaris 10 ?
This is the first time you mention using Solaris, which is rather important information.

Out of the box Solaris doesn't use GNU Awk. There is an alternative awk present you should use: /usr/xpg4/bin/awk

Try changing the following in grails code:
Code:
#!/usr/bin/awk -f

# change the above line to

#!/usr/xpg4/bin/awk -f
Can I correctly assume you tried running awk --version to get the awk information?
 
Old 07-23-2013, 04:40 AM   #24
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Satyaveer Arya View Post
Hi grail,

Thanks for the helpful script but when I run the script it give an error:
awk: syntax error near line 3
awk: bailing out near line 3
Meaningful error messages is the main reason on SunOS for using nawk rather than awk.
 
  


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
Shell script to find a string and print x lines before and y lines after the string igorza Linux - Newbie 6 04-18-2013 04:31 PM
Scripting- sed command - replacing strings excluding the lines matching a pattern(s) msriram Linux - General 1 11-05-2012 09:29 AM
[SOLVED] Extracting Lines Which Ends With a Certain Pattern arnold.pietersen Programming 9 07-16-2012 03:25 AM
help extracting a matching pattern and next lines of match madvicious Programming 8 09-13-2009 01:01 AM
Unix command to delete the previous and next line of the searched pattern navin29 Linux - Newbie 7 03-14-2008 01:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:14 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