ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
When faced with a problem like this, I immediately consider that awk was designed specifically to handle moderately-complex file processing jobs like this one; and that the entire perl programming language was, in one sense, "built on top of 'awk.'" Therefore, I have two heavyweight programming tools at my disposal.
It's important to consider solutions that are descriptive of the problem being solved: not merely "something that 'works now.'" It needs to work well in the general case.
The Perl community has a saying: "TMTOWTDI" = "There's More Than One Way To Do It." And that's very true.
Sometimes a good solution to a problem can be discovered by re-stating it, thusly:
Quote:
"Print all of the lines in the source-file thatare not equal to '====='."
If that is a valid definition of your problem, then grep could be used to "print all lines in this file which do not match the following regular-expression ('string pattern')."
Quote:
grep -v /^=====$/ filename
(The regular-expression uses the "^" and "$" anchors, which mark "start of line" and "end of line" respectively, to match lines that consist of five equals-signs. The "-v" command-line option inverts the test to print all non-matching lines.)
"TMTOWTDI!" "TMTOWTDI!" More than one 'solution' will 'work!' The one that you're looking for is the one that is easiest to implement and that solves the problem most-completely in the general case.
I have seen this thread on the first day of the posting itself. I thought solution exists using sed itself. ofcourse I am learning awk. I have to improve my skills in writing awk program. using awk may be better too.
but I have a solution using sed.
Code:
sed -n '/500/ {;H;b one;};/=====/ {;h;b;};H;b;:one;n;/=====/ {;g;p;b;};H;b one;' <inputfile>
I was not able to give time to this question. Today I did it. hope it will be useful to OP and others ofcourse .
first line will be ===== .
To optimize the code, I allowed it to be there in the output.....
Windowing requirements seem to occur frequently. I often use cgrep for such tasks:
Code:
#!/bin/bash -
# @(#) s1 Demonstrate windowing feature of cgrep.
# http://www.bell-labs.com/project/wwexptools/cgrep/
echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) cgrep
set -o nounset
echo
FILE=data1
pattern=${1-"file-id:500"}
echo " Results:"
cgrep -D -+w '^===' "$pattern" $FILE
exit 0
Producing:
Code:
$ ./s1
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
cgrep (local) - no version provided for ~/executable/cgrep.
Results:
=====
l33t
file-id:500
hax0rz
=====
See the URL for a source download. The cgrep utility as many more features than this, and, since you have the source, you can place it on other systems you use. On the one I used here, I have it installed in a private directory ... cheers, makyo
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.