LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-01-2006, 06:00 AM   #1
utopian_penguin
LQ Newbie
 
Registered: Nov 2006
Posts: 5

Rep: Reputation: 0
Bash Scripting Problem...


Dear Scripting Gurus,
I have a problem creating a new script, which gets some specific information from a logs file, the problem is that I want to grep all the lines in the file until a specific pattern is found.

so for example if the file has these lines:


Suse
Fedora
Ubunto
Gentoo
Mandrake
Freespire
Red hat

=================
I want the script to scan the file and print all the lines until it finds Mandrake and stops, which would give in the above example:

Suse
Fedora
Ubunto
Gentoo



Thank you for your help in advance.
 
Old 11-01-2006, 06:03 AM   #2
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
Hi,

This should work:

sed -n '1,/Gentoo/p' infile

Hope this helps.
 
Old 11-01-2006, 10:01 AM   #3
utopian_penguin
LQ Newbie
 
Registered: Nov 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks a million

It worked, thank you buddy, you dunno how much time you've saved me with this..

Thanks again

I should do more on awk and sed, they're miraculous!
 
Old 11-01-2006, 10:22 AM   #4
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
Hi,

You're welcome

Sed and awk are indeed very nice and powerful tools for everyday use and for using in shell scripts.
There's a lot of information on-line for both. If you are interested in printed information take a look at O'Reilly's sed&awk book.
 
Old 11-01-2006, 01:05 PM   #5
utopian_penguin
LQ Newbie
 
Registered: Nov 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Drunna,
can you explain the options and the syntax of your proposed command.

what are the -e -n used for,and /p!!
 
Old 11-01-2006, 01:48 PM   #6
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
Hi,

sed -n '1,/Gentoo/p' infile

The -n is needed to suppress the normal output of sed (try not using it), only what is found needs to be printed and that is done by the p at the end.

the 1,/Gentoo/ part is the address range. This states: start at line 1 all the way up to and including Gentoo. The last part must be between forward slashes to indicate that this is a regular expression, not an actual line number. A 'normal' address range looks like this: 3,12 which indicates lines 3 to 12 (including).

The main thing is that you can use regular expression(s) as address indicators. The next example will print all between (and including) Ubunto and Freespire:

sed -n '/Ubunto/,/Freespire/p' infile

The -e option is not used in my example, but you can string sed statements together:

sed -e 's/o/XX/' -e 's/a/YY/' infile is the same as sed 's/o/XX/' infile | sed 's/a/YY/'. The first one being better, it's a lot more resource friendly.

Here's an on-line tutorial for sed.

Hope this clears things up a bit.
 
Old 11-01-2006, 02:14 PM   #7
DotHQ
Member
 
Registered: Mar 2006
Location: Ohio, USA
Distribution: Red Hat, Fedora, Knoppix,
Posts: 548

Rep: Reputation: 33
Thanks for the explanation, and good link to the tutorial!
 
Old 11-01-2006, 02:29 PM   #8
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


Always nice to know that others find it helpful too.
 
Old 11-07-2006, 03:46 PM   #9
utopian_penguin
LQ Newbie
 
Registered: Nov 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Another Challenge

Hi Drunna,
How are you buddy, here's another challenge I found creating my script, my log file contains Timestamps, what I want to do is to grep a specific number of lines around the - for example - Gentoo line,

In another way, I want to have the chunk of lines around the line I want (which is included of course) , a period of time in my case ...

Please, Help
 
Old 11-08-2006, 12:55 AM   #10
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
Hi,

This is indeed a challenge, but no fear sed is here

Code:
#!/bin/bash
# -------------------------------------------------------------------------- #
# prints out 4 lines around pattern
# -------------------------------------------------------------------------- #

case $# in
  2);;
  *|1) echo "Usage: $0 pattern <infile>";exit;;
esac;

infile=$2

sed -n '
'/$1/' !{
  # does not match - add this line to the hold space
  H
  # bring it back into the pattern space
  x
  # Two lines would look like .*\n.*
  # Three lines look like .*\n.*\n.*
  # Delete extra lines - keep two
  s/^.*\n\(.*\n.*\)$/\1/
  # now put the two lines (at most) into
  # the hold buffer again
  x
}
'/$1/' {
  # matches - append the current line
  H
  # get the next line
  n
  # matches - append the current line
  H
  # get the next line
  n
  # append that one also
  H
  # bring it back, but keep the current line in
  # the hold buffer. This is the line after the pattern,
  # and we want to place it in hold in case the next line
  # has the desired pattern
  x
  # print the all lines
  p
  # add the mark
  a\
---
}' $infile

# -------------------------------------------------------------------------- #
# End
There will be a mark (---) printed at the end of each hit. Just remove the a\ and the --- from the script if that is not needed.

A sample run:
Code:
./blaat Gentoo infile
Fedora
Ubunto
Gentoo
Mandrake
FreeSpire
---
I included as much comments as possible. Here's the same script, without all the comments:
Code:
#!/bin/bash
# -------------------------------------------------------------------------- #
# Prints out 4 lines around pattern.
# -------------------------------------------------------------------------- #

case $# in
  2);;
  *|1) echo "Usage: $0 pattern <infile>";exit;;
esac;

infile=$2

sed -n '
'/$1/' !{
  H
  x
  H
  x
  s/^.*\n\(.*\n.*\)$/\1/
  x
}
'/$1/' {
  H
  n
  H
  x
  p
  a\
---
}' $infile

# -------------------------------------------------------------------------- #
# End
Hope this helps.

Last edited by druuna; 11-08-2006 at 12:56 AM.
 
Old 11-08-2006, 01:31 PM   #11
utopian_penguin
LQ Newbie
 
Registered: Nov 2006
Posts: 5

Original Poster
Rep: Reputation: 0
Thumbs up

CooooooooooooL , You Rock dude , you always have the answers present!!!!!!!!!!!

Thanks a zillion
 
Old 11-08-2006, 01:39 PM   #12
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
Hi,

You're welcome
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH scripting problem danreed007 Programming 5 04-24-2006 05:02 AM
bash scripting problem sutley Linux - Software 1 12-17-2004 11:33 AM
bash scripting problem Sammy2ooo Linux - General 3 12-13-2004 12:31 PM
BASH scripting problem deadlock Programming 5 08-15-2003 04:33 AM
bash scripting problem raven Programming 7 03-10-2003 05:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:04 AM.

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