LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 12-31-2009, 12:59 AM   #1
manya
Member
 
Registered: Apr 2004
Posts: 194

Rep: Reputation: 15
How to filter out number of lines which are not constant in numbers.


Hi Friends,

I am writing a script where I need to grep out number of lines, which are not fixed in numbers but starting and end lines of that para are constant.

e.g

Please block below URLs
http://...........
http:...........
234.87.98.30
.
.
.
.
.

End of Urls

any one has any idea how can I filter out those URLs or number of lines between "please block" and "end"
 
Old 12-31-2009, 01:22 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
show a better example, and describe and show clearly what you want as output.
 
Old 12-31-2009, 01:30 AM   #3
manya
Member
 
Registered: Apr 2004
Posts: 194

Original Poster
Rep: Reputation: 15
Well, I would like filter out the lines which keeps on varying so i can not use -A or -B syntax with grep.
so looking for other better option by which I can grep out those lines. The one clue I can see is since these lines always starts and ends with certain fixed lines, I could use that criteria to extract the lines?

say for example

# Para Starts from below
Please block below URLS.
http://abc.com
123.23.24.45
http://xyz.com/aere.php
.
.
.
.
.
.

end

# para ends

So in the above example if I could filter out the lines between "Please block" and "end" I guess I could achieve the goal but not sure how to extract the same with the help of grep.

Do let me know if you have any other solution.
 
Old 12-31-2009, 01:41 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by manya View Post
Well, I would like filter out the lines which keeps on varying so i can not use -A or -B syntax with grep.
so looking for other better option by which I can grep out those lines. The one clue I can see is since these lines always starts and ends with certain fixed lines, I could use that criteria to extract the lines?

say for example

# Para Starts from below
Please block below URLS.
http://abc.com
123.23.24.45
http://xyz.com/aere.php
.
.
.
.
.
.

end

# para ends

So in the above example if I could filter out the lines between "Please block" and "end" I guess I could achieve the goal but not sure how to extract the same with the help of grep.

Do let me know if you have any other solution.
http://www.ibm.com/developerworks/li...GX03&S_CMP=EDU -> see "Listing 3: Printing a range of lines".
 
Old 12-31-2009, 02:07 AM   #5
manya
Member
 
Registered: Apr 2004
Posts: 194

Original Poster
Rep: Reputation: 15
Would it be possible to achieve the same in Bash Script?
Please let me know if you find any alternative.
 
Old 12-31-2009, 02:45 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,133

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
First you asked for grep, now you want bash ??? How about sed
Code:
sed '/Please block/,/^end/ d' somefile
 
Old 12-31-2009, 03:15 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk -vRS="end" '{gsub(/Please block.*/,"")}1' file

Last edited by ghostdog74; 12-31-2009 at 03:17 AM.
 
Old 12-31-2009, 06:25 PM   #8
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Using awk, if a line begins with Please block below URLs set a flag.

If the flag is set, then write subsequent lines to another file

If a line begins with End of Urls then unset the flag.

End
 
  


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
How to filter lines Nortekman Linux - Newbie 3 04-01-2009 10:35 AM
adding constant (number) to a number in all rows skuz_ball Programming 2 05-20-2008 09:57 PM
filter out lines containing specific keywords from output kpachopoulos Linux - General 2 03-12-2007 08:29 PM
Is there a way to filter out all the comment lines in a configuration file... Akhran Linux - Newbie 3 12-07-2005 11:07 AM
How to filter lines from a text file Rascale Linux - Software 1 10-28-2003 12:46 PM

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

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