Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

grep out lines from a file

Hi,

I am looking for a way to grep out stuff from this file called static.txt

It looks like:

host main-125-hp {
    hardware ethernet 08:00:09:c5:20:fb;
    option host-name "main-125-hp";
    fixed-address 10.6.3.34;
  }

  host main-124c-hp {
    hardware ethernet 00:11:0a:bb:1c:77;
    option host-name "main-124c-hp";
    fixed-address 10.6.3.35;
  }

  host bus5-hp {
    hardware ethernet 00:10:83:f4:d2:8e;
    option host-name "bus5-hp";
    fixed-address 10.6.3.36;


I want to grep a way and output to a file that looks like this

host main-125-hp 10.6.3.34:08:00:09:c5:20:fb

Thanks
0
richsark
Asked:
richsark
  • 9
  • 5
  • 5
  • +2
1 Solution
 
richsarkAuthor Commented:
I tried

grep -i "option host-name hardware ethernet" static.txt
grep -i "ethernet" static.txt
0
 
amit_gCommented:
awk 'BEGIN {FS=" |;";RS="}";} {print $2 " " $24 " " $11}' YourFileName
0
 
richsarkAuthor Commented:
Hello,

I ran it, does not look good

$ awk 'BEGIN {FS=" |;";RS="}";} {print $2 " " $24 " " $11}' dhcpd.staticleases.txt


Static is configuration
 10.101.16.37 00:1b:78:17:6e:9f

10.3.2.0/24

 10.101.8.38 00:14:38:d7:38:06
 10.101.8.37 00:14:38:d7:37:2c
 10.101.8.36 00:14:38:8f:bb:de
 10.101.8.35 00:02:02:1c:79:d6

10.3.3.0/24


10.6.3.0/24 host-name

 10.6.3.33 00:10:83:0a:9a:a2
 fixed-address 08:00:09:c5:20:fb
 10.6.3.35 00:11:0a:bb:1c:77
 10.6.3.36 00:10:83:f4:d2:8e
 10.6.3.37 00:14:38:91:c5:ae
  00:60:b0:53:fa:10
 10.6.3.39 00:21:5a:91:58:83
 10.6.3.40 00:30:c1:56:b1:e0
 10.6.3.41 00:30:c1:c2:2e:02
 10.6.3.43 00:10:83:ba:19:da
 10.6.3.44 00:10:83:f4:d2:96
 10.6.3.45 00:1f:29:24:54:ae
 10.6.3.46 00:14:38:90:a1:d8
 10.6.3.47 08:00:09:ca:bb:3b

I was hoping an output like this:

host main-125-hp 10.6.3.34:08:00:09:c5:20:fb
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
amit_gCommented:
Either your awk is behaving differently or the sample posted here is different than actual file content. What o/s are you using it? Also it would help if you posted the sample as attachment (not copy paste).
0
 
richsarkAuthor Commented:
I am using Windows 7 64 bit.  I ran this in cygwin

Attached is the whole file.
dhcpd.staticleases.txt
0
 
amit_gCommented:

awk '
        BEGIN {
                FS=" |;";

                host="";
                fixedaddress="";
                ethernet="";
        }
        /host / {
                host=$4;
        }
        /hardware ethernet / {
                ethernet=$7;
        }
        /fixed-address / {
                fixedaddress=$6;

                printf("%s %s:%s\n", host, fixedaddress, ethernet);

                host="";
                fixedaddress="";
                ethernet="";
        }
        ' YourFileName

Open in new window

0
 
point_pleasantCommented:
Perl script

perl prog_name.pl < infile > out_file


while (<>) {
        if (/^host main.*{/) {
                $host_main = $_;
                $host_main =~ s/\{//;
                chomp($host_main);
        }
        if (/^.*hardware ethernet/) {
                @mac_addr = split (/ethernet/,$_);
                $mac_addr[1] =~ s/\;//;

        }
        if (/^.*fixed-address/) {
                @fix_addr = split (/address/,$_);
                $fix_addr[1] =~ s/\;//;
                chomp($fix_addr[1]);
        }
        if (/^.*}/) {
                print "$host_main$fix_addr[1]$fix_addr[1]$mac_addr[1]";
        }
}
0
 
richsarkAuthor Commented:
ok, do I create an sh file with the above code and run it as:

./find-stuff.sh dhcpd.staticleases.txt
0
 
amit_gCommented:
You can do that. Change the YourFileName with $1 in the code if you want to pass the filename from command line.
0
 
richsarkAuthor Commented:
HI point plesent

I ran it, got:

$ perl find-stuff-2.pl dhcpd.staticleases.txt
 10.101.16.39 10.101.16.39 00:11:85:fa:20:13
 10.101.16.37 10.101.16.37 00:1b:78:17:6e:9f
 10.101.16.37 10.101.16.37 00:1b:78:17:6e:9f
 10.101.8.39 10.101.8.39 00:01:e6:4f:88:ff
 10.101.8.38 10.101.8.38 00:14:38:d7:38:06
 10.101.8.37 10.101.8.37 00:14:38:d7:37:2c
 10.101.8.36 10.101.8.36 00:14:38:8f:bb:de
 10.101.8.35 10.101.8.35 00:02:02:1c:79:d6
 10.101.8.35 10.101.8.35 00:02:02:1c:79:d6
 10.101.0.30 10.101.0.30 00:0d:02:00:9f:dc
 10.101.0.30 10.101.0.30 00:0d:02:00:9f:dc
 10.6.3.31 10.6.3.31 00:20:6b:53:bb:b3
 10.6.3.33 10.6.3.33 00:10:83:0a:9a:a2

Looking for more towards the line of:


host main-125-hp 10.6.3.34:08:00:09:c5:20:fb
0
 
käµfm³d 👽Commented:
See if this one-liner suits you. I have included two possibilities--one for unix and one for Windows. I've never used cygwin before, so I'm not sure which quotes would be appropriate for it.
// Windows
perl -e "$lines = do { local $/ = <> }; print \"host $2 $1:$3\n\" while ($lines =~ /.*?hardware.*?((?:[a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}).*?host-name \"([a-zA-Z0-9-]+)\".*?fixed-address\s*((?:\d{1,3}\.)\d{1,3})/gs);" dhcpd.staticleases.txt

// Unix
perl -e '$lines = do { local $/ = <> }; print "host $2 $1:$3\n" while ($lines =~ /.*?hardware.*?((?:[a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}).*?host-name "([a-zA-Z0-9-]+)".*?fixed-address\s*((?:\d{1,3}\.)\d{1,3})/gs);' dhcpd.staticleases.txt

Open in new window

0
 
point_pleasantCommented:
runs fine on my machine with the sameple you gave initially.  can you post a test file with the lines?
0
 
point_pleasantCommented:
never mind saw the txt file.  I was using the lines youprinted in the question which is some what different that the txt file.  i will fix in the morning
0
 
point_pleasantCommented:
if you run
$ perl find-stuff-2.pl dhcpd.staticleases.txt
on the below test it will work fine.  I will fix to work on the txt file in the morning.  I am going to assume that all you are interested in aare the lines starting with

host .............. {
.......
.......
.......
}


host main-125-hp {
    hardware ethernet 08:00:09:c5:20:fb;
    option host-name "main-125-hp";
    fixed-address 10.6.3.34;
  }

  host main-124c-hp {
    hardware ethernet 00:11:0a:bb:1c:77;
    option host-name "main-124c-hp";
    fixed-address 10.6.3.35;
  }

  host bus5-hp {
    hardware ethernet 00:10:83:f4:d2:8e;
    option host-name "bus5-hp";
    fixed-address 10.6.3.36;
}

0
 
richsarkAuthor Commented:
Hi point_pleasant:

Yes, but

host main-125-hp {
option host-name "main-125-hp";

are the same, so just need it once to produce the output I need of

host main-125-hp 10.6.3.34:08:00:09:c5:20:fb
0
 
amit_gCommented:
Have you tried href:#a35010986 ? I ran it on CygWin with your sample and it seems to be fine. Obviously you need to do more testing and may be tweak it even further.
0
 
point_pleasantCommented:
will fix in morning.  did not use your samole file just txt typed in the question ...... my bad
0
 
tel2Commented:
Hi richsark,

Have you tried kaufmed's one-liners?  Why don't you use one of them?

Here's a more concise (UNIX) version:

    perl -0ne 'print "host $2 $1:$3\n" while (/.*?hardware.*?((?:[a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}).*?host-name "([a-zA-Z0-9-]+)".*?fixed-address\s*((?:\d{1,3}\.)\d{1,3})/gs);' dhcpd.staticleases.txt

Or if it is possible that the input file could contain a null character (ASCII 0), change the beginning to this, instead:

    perl -0777ne 'print ...
0
 
richsarkAuthor Commented:
Hello, point_pleasant

Just touching base if you had fixed the issue as you stated above 35013760

Just curiuos...
0
 
käµfm³d 👽Commented:
I guess I'm not getting any love on this question...   *sigh*
0
 
richsarkAuthor Commented:
Hello kaufmed,  I will let you know in a few. Hang in there :)
0
 
richsarkAuthor Commented:
This code worked just as I need.

Thanks for everones help !
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 5
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now