bash - awk, sed, grep, ... advice
[aix]
hi, i have a file like: Code:
===== for example: for file-id:500, i want the output Code:
===== is there a simple way of doing it ? |
How big is the input file? Will it easily fit in memory?
|
not terribly big:
Code:
wc -l file.in |
Code:
#!/bin/bash Dave |
thanks, this doesnt quite work for me:
Code:
schneidz@lq> cat ilikejam.lst | ilikejam.ksh 500 regards, |
Here is a quick and dirty solution (probably someone will find a better one), but it seems to work:
Code:
allez@home:~/tmp> cat file.in |
Code:
awk 'BEGIN {RS="===="} /file-id:500/' file |
Quote:
my only peculiarity i have is: Code:
schneidz@lq> h=42 regards, |
although slightly more complex a tweaked allez example allows for variable substitution:
Code:
schneidz@lq> cat allez.ksh __________ edit: and then maybe there's this: Code:
grep -p===== $h ilikejam.lst |
Quote:
Code:
$0 ~ h |
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:
Quote:
"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 :cool:. first line will be ===== . To optimize the code, I allowed it to be there in the output..... |
Funky Perl:
Code:
perl -e '$/="====="; map { s/^$//; printf "=====%s\n", $_ if (/file-id:500/); } <>' in_file |
Hi.
Windowing requirements seem to occur frequently. I often use cgrep for such tasks: Code:
#!/bin/bash - Code:
$ ./s1 |
All times are GMT -5. The time now is 01:15 AM. |