LinuxQuestions.org
Help answer threads with 0 replies.
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 09-06-2007, 05:21 AM   #1
LinuxLala
Senior Member
 
Registered: Aug 2003
Location: New Delhi, India
Distribution: Fedora 7
Posts: 1,305

Rep: Reputation: 45
need help with a script that involves awk and sed


Hi guys,

I need some assistance in writing a small script.

I have some IRC logs that I need the script to go through and print out the lines in which a URL is written.

I can currently do 'less logfile | grep 'http' > links' to get all the URLs from the particular log.

What I want to do is make the script print out 5 lines before and after the URL is mentioned.

So I'm thinking that I can use sed to number all the lines in the log file, use awk to print out the lines where a URL is mentioned, somehow store each line number into a variable and feed each variable to sed again to print out the 5 preceding and following lines.

from my research:

sed '$!N;s/\n/ /' logfile > logfile1 will number all the lines of the log file. I can then do:
less logfile1 | grep 'http' | awk {print '$1'}

This will print out the lines (numbers) where a URL is mentioned. I also know that I can use "sed -n '45,50p' filename" to print lines numbered 45-50. So, I'm thinking if I can store each number as a variable, I can have the script print the 5 lines before and after that line into a new file.

Can anything like this be done?
 
Old 09-06-2007, 05:44 AM   #2
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Assuming GNU grep:

Code:
grep -F -B5 -A5 http logifle
 
Old 09-06-2007, 09:10 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by LinuxLala View Post
Can anything like this be done?
show sample input, and expected output, and the code you have so far.
 
  


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
sed and awk in shell script bondoq Linux - Newbie 14 07-27-2007 01:52 AM
awk and/or sed linux2man Linux - General 7 01-22-2007 10:02 AM
ssimple shell script to parse a file ~sed or awk stevie_velvet Programming 7 07-14-2006 03:41 AM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 08:24 PM
awk/sed help pantera Programming 1 05-13-2004 11:59 PM

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

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