Problem with dots within a file (Please HELP ME 
Author Message
 Problem with dots within a file (Please HELP ME


Quote:

>I am treating this file:

>....

>lease 333.444.150.38 {
>      starts 6 1999/10/16 14:14:53;
>      ends 2 1999/10/19 14:14:53;
>      hardware ethernet 00:00:00:00:00:00;
>      uid 01:00:00:00:00:00:00;
>      client-hostname "dgdgff";
>      }
>lease 333.444.140.216 {
>      starts 6 1999/10/16 14:14:24;
>      ends 2 1999/10/19 14:14:24;
>      hardware ethernet 00:00:00:00:00:00;
>      uid 00:00:00:00:00:00;
>      client-hostname "fgfg";
>      }
>lease 333.444.14.247 {
>      starts 6 1999/10/16 14:08:25;
>      ends 2 1999/10/19 14:08:25;
>      hardware ethernet 00:00:00:00:00:00;
>      uid 01:00:00:00:00:00:00;
>      client-hostname "ddfg";
>      }
>          ....

> And this is a part of my ksh script:

>echo "Enter a number"
>read  d
>#d=`echo $d | sed 's/\./\\./g'`  "I was told to try this, but it
>#doesn't work either.
>cat file1|awk "/$d/{lt=3} lt>0{print;
>--lt}" >file2

>I'm trying to get every line matching $d and the following 2 lines
>which belong with it.
>It works alright if the input is "333.444.140" or "333.444.140." .
>But if the input is, say "333.444.14" or "333.444.14." I get
>wrong results because it greps also
>333.444.140
>333.444.141
>333.444.142
>and so on.
>I think the problem lies in this dot "." but I don't know how
>to solve it.Please help me find a simple solution in ksh with awk
>or gawk.
>Would really appreciate any advice.

The problem is that in awk or gawk one needs the right regular
expression, and dots can have special meaning, so, create a shell
variable from your number that is an appropriate regular expression.

Essentially, feed gawk the regular expression it needs.  :-)

I've done that in the following using sed to edit $d and create
a $re that should be right.  You could reassign $d, but using
$re let me show what was being used and for what.

Try this:

echo "Enter a number"
read  d
re=`echo $d |sed -e 's/[^.]$/&./' -e 's/\./[.]/g' -e 's/\[.]$/[. ]/`
# remove the following echo after testing
echo "Using this regular expression: $re for this number: $d"
gawk '/'"$re"'/ {pt=NR +3} NR<pt {print}' file1 > file2

This can be made more bulletproof with more effort.

In this line:

re=`echo $d |sed -e 's/[^.]$/&./' -e 's/\./[.]/g' -e 's/\[.]$/[. ]/`

sed first puts a dot on the end of the number if one isn't there,
then puts brackets around all dots, then changes the last [.]
to [. ]

dots inside brackets are not special regular expression characters.

This should get you started.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Fri, 05 Apr 2002 03:00:00 GMT  
 Problem with dots within a file (Please HELP ME
On Sun, 17 Oct 1999 21:53:13 GMT "Nicole Hamilton"

Quote:


> > cat file1|awk "/$d/{lt=3} lt>0{print;
> > --lt}" >file2

> > I'm trying to get every line matching $d and the following 2 lines
> > which belong with it.
> > It works alright if the input is "333.444.140" or "333.444.140." .
> > But if the input is, say "333.444.14" or "333.444.14."

> Try rewriting it as:

>    awk "/$d[. ]/ {lt = 3};  lt > 0 {print; --lt}" file1 > file2

> This requires that the string entered must be followed with either
> a dot or a space.  And btw, no need to cat file1 to awk; awk can
> read it itself.

> Nicki

Thank you very much,folks.
I tried this one and it works really fine.

Best regards

Ian Moltke
--
Posted via Talkway - http://www.talkway.com
Exchange ideas on practically anything (tm).



Fri, 05 Apr 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Problem with dots within a file (Please HELP ME

2. Problems seperating text within a string - Help Please!

3. dot-dot-dot

4. VSE 3.1 Repository Problem: Please help (please please?)

5. File Conversion Problem - Please Help

6. Please help for "File open problem"

7. Please help, DBT file problem

8. please, please, please, please, help

9. will someone please, please, please, please HELP me?!!

10. pb with proble-file and dot file

11. quick help needed: reading from a file within awk

12. Workaround for the problem with groups within a file definition

 

 
Powered by phpBB® Forum Software