Not a CentOS specific question, although I am running grep on CentOS 4.3 but how would you grep out a series of lines in a file starting at a specific point. For instance, if I have a file named foo and I want to grep out the next 5 lines after the first and only instance of the string "bar" how could I pull that off? Thanks so much.
Not a CentOS specific question, although I am running grep on CentOS 4.3 but how would you grep out a series of lines in a file starting at a specific point. For instance, if I have a file named foo and I want to grep out the next 5 lines after the first and only instance of the string "bar" how could I pull that off? Thanks so much.
What do you mean by "grep out" ? Do you want to display those lines, or skip those lines? Do you want to see the "bar" line? Is that included in the 5 lines?
Anyway, you probably want to use "sed" here, rather than "grep".
On Tue, 2007-08-28 at 10:08 -0400, Stephen Harris wrote:
Not a CentOS specific question, although I am running grep on CentOS 4.3 but how would you grep out a series of lines in a file starting at a specific point. For instance, if I have a file named foo and I want to grep out the next 5 lines after the first and only instance of the string "bar" how could I pull that off? Thanks so much.
What do you mean by "grep out" ? Do you want to display those lines, or skip those lines? Do you want to see the "bar" line? Is that included in the 5 lines?
Anyway, you probably want to use "sed" here, rather than "grep".
I'd like to skip those lines. I'd like to skip the line with "bar" and the following five lines.
On Tue, Aug 28, 2007 at 10:13:00AM -0400, Scott McClanahan wrote:
On Tue, 2007-08-28 at 10:08 -0400, Stephen Harris wrote:
Not a CentOS specific question, although I am running grep on CentOS 4.3 but how would you grep out a series of lines in a file starting at a specific point. For instance, if I have a file named foo and I want to grep out the next 5 lines after the first and only instance of the string "bar" how could I pull that off? Thanks so much.
What do you mean by "grep out" ? Do you want to display those lines, or skip those lines? Do you want to see the "bar" line? Is that included in the 5 lines?
Anyway, you probably want to use "sed" here, rather than "grep".
I'd like to skip those lines. I'd like to skip the line with "bar" and the following five lines.
Like this?
$ cat xx line 1 line 2 line bar line after 1 line after 2 line after 3 line after 4 line after 5 line after 6 line after 7 $ sed '/bar/,+5d' xx line 1 line 2 line after 6 line after 7
On Tue, 2007-08-28 at 10:27 -0400, Stephen Harris wrote:
On Tue, Aug 28, 2007 at 10:13:00AM -0400, Scott McClanahan wrote:
On Tue, 2007-08-28 at 10:08 -0400, Stephen Harris wrote:
Not a CentOS specific question, although I am running grep on CentOS 4.3 but how would you grep out a series of lines in a file starting at a specific point. For instance, if I have a file named foo and I want to grep out the next 5 lines after the first and only instance of the string "bar" how could I pull that off? Thanks so much.
What do you mean by "grep out" ? Do you want to display those lines, or skip those lines? Do you want to see the "bar" line? Is that included in the 5 lines?
Anyway, you probably want to use "sed" here, rather than "grep".
I'd like to skip those lines. I'd like to skip the line with "bar" and the following five lines.
Like this?
$ cat xx line 1 line 2 line bar line after 1 line after 2 line after 3 line after 4 line after 5 line after 6 line after 7 $ sed '/bar/,+5d' xx line 1 line 2 line after 6 line after 7
Beautiful man! Hats off. I've never used sed like that but I'll surely remember that one. Thanks from everybody.
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Tue, Aug 28, 2007 at 10:31:08AM -0400, Scott McClanahan wrote:
On Tue, 2007-08-28 at 10:27 -0400, Stephen Harris wrote:
$ sed '/bar/,+5d' xx line 1 line 2 line after 6 line after 7
Beautiful man! Hats off. I've never used sed like that but I'll surely remember that one. Thanks from everybody.
"sed" is a very nice tool. You can do amazing things with it. I once did a XML to HTML (limited) parser in it. I know someone that even coded a "Sokoban"-like game with sed and nothing else.
Mastering sed really makes life much easier.
[]s
- -- Rodrigo Barbosa "Quid quid Latine dictum sit, altum viditur" "Be excellent to each other ..." - Bill & Ted (Wyld Stallyns)
On 8/28/07, Rodrigo Barbosa rodrigob@darkover.org wrote:
EDITED:
Mastering sed is a bit like mastering Latin..... backwards.
There. Fixed that for you. :-P
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Tue, Aug 28, 2007 at 12:12:52PM -0400, Jim Perrin wrote:
On 8/28/07, Rodrigo Barbosa rodrigob@darkover.org wrote:
EDITED:
Mastering sed is a bit like mastering Latin..... backwards.
There. Fixed that for you. :-P
I see you enjoyed my signature :)
- -- Rodrigo Barbosa "Quid quid Latine dictum sit, altum viditur" "Be excellent to each other ..." - Bill & Ted (Wyld Stallyns)
Rodrigo Barbosa wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Tue, Aug 28, 2007 at 10:31:08AM -0400, Scott McClanahan wrote:
On Tue, 2007-08-28 at 10:27 -0400, Stephen Harris wrote:
$ sed '/bar/,+5d' xx line 1 line 2 line after 6 line after 7
Beautiful man! Hats off. I've never used sed like that but I'll surely remember that one. Thanks from everybody.
"sed" is a very nice tool. You can do amazing things with it. I once did a XML to HTML (limited) parser in it. I know someone that even coded a "Sokoban"-like game with sed and nothing else.
Mastering sed really makes life much easier.
If sed had been invented first we wouldn't have needed grep. Then again, if perl had been invented first we wouldn't need either - or a few hundred other tools...
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Tue, Aug 28, 2007 at 12:17:01PM -0500, Les Mikesell wrote:
Rodrigo Barbosa wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Tue, Aug 28, 2007 at 10:31:08AM -0400, Scott McClanahan wrote:
On Tue, 2007-08-28 at 10:27 -0400, Stephen Harris wrote:
$ sed '/bar/,+5d' xx line 1 line 2 line after 6 line after 7
Beautiful man! Hats off. I've never used sed like that but I'll surely remember that one. Thanks from everybody.
"sed" is a very nice tool. You can do amazing things with it. I once did a XML to HTML (limited) parser in it. I know someone that even coded a "Sokoban"-like game with sed and nothing else. Mastering sed really makes life much easier.
If sed had been invented first we wouldn't have needed grep. Then again, if perl had been invented first we wouldn't need either - or a few hundred other tools...
Ick. I hate perl.
If I find something I can't do in bash/sed/awk, I just code it in C :)
[]s
- -- Rodrigo Barbosa "Quid quid Latine dictum sit, altum viditur" "Be excellent to each other ..." - Bill & Ted (Wyld Stallyns)
Rodrigo Barbosa wrote:
On Tue, Aug 28, 2007 at 10:31:08AM -0400, Scott McClanahan wrote:
On Tue, 2007-08-28 at 10:27 -0400, Stephen Harris wrote:
$ sed '/bar/,+5d' xx line 1 line 2 line after 6 line after 7
Beautiful man! Hats off. I've never used sed like that but I'll surely remember that one. Thanks from everybody.
"sed" is a very nice tool. You can do amazing things with it. I once did a XML to HTML (limited) parser in it. I know someone that even coded a "Sokoban"-like game with sed and nothing else. Mastering sed really makes life much easier.
If sed had been invented first we wouldn't have needed grep. Then again, if perl had been invented first we wouldn't need either - or a few hundred other tools...
Ick. I hate perl.
It's easy to hate perl that other people have written, but you can write your own in whatever style you like.
If I find something I can't do in bash/sed/awk, I just code it in C :)
You can write perl that looks like C - but if you aren't using hashes and regexps you'll do a lot more work. And pretty much everything that anyone might need to do has already been written and is available on CPAN.
On Tue, Aug 28, 2007 at 05:04:33PM -0500, Les Mikesell wrote:
It's easy to hate perl that other people have written, but you can write your own in whatever style you like.
6 years ago I wrote a perl regexp that did some magic. The comment before it... # It's lines like this that make people hate perl The 10 line afterwards explained the magic. After all, I would need to read it again a few years later, so I needed to give myself all the help I could :-)
(My best ever comment was "this came to me in a dream"; it was literally correct - the answer _did_ come to me in a dream - and it took me 3 days to work out _why_ that stuff worked! I finally replaced the comment with something more useful when I worked out what it did. After all, it was 10 times faster than my previous code).
The problem with perl isn't that you can write crap code in it; the problem is that people take the "hammer" approach ("If I have a hammer then everything looks like a nail"). The responses to the original question are a perfect example; people wrote multi-line perl scripts to do something that was possible in 1 line of sed (the people writing perl didn't even write _efficient_ perl; dammit, -p flag people!)
I'm probably fighting a losing battle; I was shell scripting 17 years ago when every fork/exec was expensive. I cry when I see people writing grep | awk type combinations (and don't start me on cat | grep).
Old geek statement: If you think perl is the answer to a simple filter question then think twice. You might be right, but it's likely smaller faster tools already exist. And I say this as someone who has written 1000 line shell scripts and even bigger perl scripts; perl is good for complicated tasks, but rarely required for simple stuff. Don't wield your hammer because that's all you know.
In this case, everyone who responded with a perl solution needs their hammer taken away.
Old geek statement: If you think perl is the answer to a simple filter question then think twice. You might be right, but it's likely smaller faster tools already exist. And I say this as someone who has written 1000 line shell scripts and even bigger perl scripts; perl is good for complicated tasks, but rarely required for simple stuff. Don't wield your hammer because that's all you know.
In this case, everyone who responded with a perl solution needs their hammer taken away.
Heh. The service provider where I worked was a perl shop. I hated perl but that was because I was not familiar with perl and it had way too many modules and what not.
What happens sometimes is that the perl chums will hammer out a solution in perl and use it. Later their perl solution gets converted to awk, bash or C by themselves or by me due to perl being way too expensive.
I say let them keep their hammer and then make them use a more efficient one when they prove their hammer does work.
On Tue, 2007-08-28 at 23:30 -0400, Stephen Harris wrote:
On Tue, Aug 28, 2007 at 05:04:33PM -0500, Les Mikesell wrote:
<snip>
I'm probably fighting a losing battle; I was shell scripting 17 years ago when every fork/exec was expensive. I cry when I see people writing grep | awk type combinations (and don't start me on cat | grep).
AMEN BROTHER! It's hard to shake the efficiency concerns that started ... oh well, why not ... in 1978 on PWB V7. I astounded the sysadmin with my multi-thousand line shell scripts. Having been programming, not administering, for some years prior to that, I saw nothing wrong with well structured code in large quantities when well commented.
The "cat | <your-util-of-choice>" that is seen so frequently is one of my pet peeves too. People need to be very familiar with I/O redirects provided as a *standard* feature of almost all *IX utilities.
Old geek statement: If you think perl is the answer to a simple filter question then think twice. You might be right, but it's likely smaller faster tools already exist. And I say this as someone who has written 1000 line shell scripts and even bigger perl scripts; perl is good for complicated tasks, but rarely required for simple stuff. Don't wield your hammer because that's all you know.
In this case, everyone who responded with a perl solution needs their hammer taken away.
As a minor disagreement with the above... I agree but the sad fact is that it is usually faster to use what you already know than to try to find one or more appropriate utilities and then learn how to use them. In a production environment, the time needed may be very dear.
I offer for your consideration that among the many good answers to the OP, csplit was never mentioned although it is certainly one of the useful options.
-- Bill
Scott McClanahan wrote:
I'd like to skip those lines. I'd like to skip the line with "bar" and the following five lines.
In that case, the perl code would be:
#!/usr/bin/perl $eat = 0; while (<>) { if (m/bar/) { $eat = 6; }
if ($eat) { --$eat; } else { print; } }
Again, not tested. Use at your own risk.
I got the have install a SATA HardDisk on the machine recently. But somethings the Disk cannot be accessed. And mesg tells that it is IO error. Is it a hardware problem or system setting problem.
Here is the message from dmesg.
sd 0:0:0:0: SCSI error: return code = 0x00040000
end_request: I/O error, dev sda, sector 27099199 EXT3-fs error (device sda1): ext3_readdir: directory #1687553 contains a hole at offset 0
Thank you.
Regards, Ming
Ming wrote:
I got the have install a SATA HardDisk on the machine recently. But somethings the Disk cannot be accessed. And mesg tells that it is IO error. Is it a hardware problem or system setting problem.
Here is the message from dmesg.
sd 0:0:0:0: SCSI error: return code = 0x00040000 end_request: I/O error, dev sda, sector 27099199 EXT3-fs error (device sda1): ext3_readdir: directory #1687553 contains a hole at offset 0
I'd get the standalone disk diagnostic software from the drive maker, and run that against this SATA drive.
Let's try to not hijack threads on the mailing list please.
On 8/28/07, Ming cyberming@gmail.com wrote:
I got the have install a SATA HardDisk on the machine recently. But somethings the Disk cannot be accessed. And mesg tells that it is IO error. Is it a hardware problem or system setting problem.
Here is the message from dmesg.
sd 0:0:0:0: SCSI error: return code = 0x00040000 end_request: I/O error, dev sda, sector 27099199 EXT3-fs error (device sda1): ext3_readdir: directory #1687553 contains a
hole at offset 0
Thank you.
Regards, Ming
CentOS mailing list CentOS@centos.org http://lists.centos.org/mailman/listinfo/centos
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Wed, Aug 29, 2007 at 11:54:42AM +0800, Ming wrote:
I got the have install a SATA HardDisk on the machine recently. But somethings the Disk cannot be accessed. And mesg tells that it is IO error. Is it a hardware problem or system setting problem.
Here is the message from dmesg.
sd 0:0:0:0: SCSI error: return code = 0x00040000 end_request: I/O error, dev sda, sector 27099199 EXT3-fs error (device sda1): ext3_readdir: directory #1687553 contains a hole at offset 0
Yes, and I'm sure "grep" is the culprid here. :)
- -- Rodrigo Barbosa "Quid quid Latine dictum sit, altum viditur" "Be excellent to each other ..." - Bill & Ted (Wyld Stallyns)
On 8/28/07, Scott McClanahan scott.mcclanahan@trnswrks.com wrote:
Not a CentOS specific question, although I am running grep on CentOS 4.3 but how would you grep out a series of lines in a file starting at a specific point. For instance, if I have a file named foo and I want to grep out the next 5 lines after the first and only instance of the string "bar" how could I pull that off? Thanks so much.
'grep -A 5 bar foo' should do the trick. From the grep manpage -> -A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines. Places a line containing -- between contiguous groups of matches.
On Aug 28, 2007, at 10:05 AM, Scott McClanahan wrote:
Not a CentOS specific question, although I am running grep on CentOS 4.3 but how would you grep out a series of lines in a file starting at a specific point. For instance, if I have a file named foo and I want to grep out the next 5 lines after the first and only instance of the string "bar" how could I pull that off? Thanks so much.
for the line matching "bar" and the next five lines after it:
$ grep -A 5 bar foo
if you don't want the line matching "bar":
$ grep -A 5 bar foo | tail -5
-steve
-- If this were played upon a stage now, I could condemn it as an improbable fiction. - Fabian, Twelfth Night, III,v
Not a CentOS specific question, although I am running grep on CentOS 4.3 but how would you grep out a series of lines in a file starting at a specific point. For instance, if I have a file named foo and I want to grep out the next 5 lines after the first and only instance of the string "bar" how could I pull that off? Thanks so much.
"man grep" is your friend here
Quoting (cause they explain it better than I): OPTIONS -A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines. Places a line containing -- between contiguous groups of matches.
. . . . -m NUM, --max-count=NUM Stop reading a file after NUM matching lines. If the input is standard input from a regular file, and NUM matching lines are output, grep ensures that the standard input is positioned to just after the last matching line before exiting, regardless of the presence of trailing context lines. This enables a calling process to resume a search. When grep stops after NUM matching lines, it outputs any trailing context lines. When the -c or --count option is also used, grep does not output a count greater than NUM. When the -v or --invert-match option is also used, grep stops after outputting NUM non-matching lines.
So your command would look something like this:
grep -m1 -A5 bar foo
This does not handle your case of "first and only" instance however. It will stop after the first instance of the word bar.
Scott McClanahan wrote:
grep out the next 5 lines after the first and only instance
The scope of grep's view of the world is a single line. At any one time, it knows nothing more.
If you need to deal with multiple lines, I suggest perl. Untested code:
#!/usr/bin/perl while (<>) { if (m/bar/) { for ($i = 0; $i < 5 and defined($_); ++$i) { print; $_ = <>; } last; } }
On Tue, 2007-08-28 at 10:05 -0400, Scott McClanahan wrote:
Not a CentOS specific question, although I am running grep on CentOS 4.3 but how would you grep out a series of lines in a file starting at a specific point. For instance, if I have a file named foo and I want to grep out the next 5 lines after the first and only instance of the string "bar" how could I pull that off? Thanks so much.
I know I'm late on this and there have been many fine replies. I prefer csplit for tasks like this. As always with *IX, many ways to skin the cat.
<snip>
-- Bill