Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-06-2007, 06:21 AM   #1
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, 06:44 AM   #2
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 37
Assuming GNU grep:

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

Rep: Reputation: 244Reputation: 244Reputation: 244
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

All times are GMT -5. The time now is 06:27 AM.

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