Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

grep out lines from a file

Posted on 2011-03-01
22
Medium Priority
?
408 Views
Last Modified: 2012-05-11
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
Comment
Question by:richsark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
  • 5
  • +2
22 Comments
 
LVL 1

Author Comment

by:richsark
ID: 35009626
I tried

grep -i "option host-name hardware ethernet" static.txt
grep -i "ethernet" static.txt
0
 
LVL 58

Expert Comment

by:amit_g
ID: 35009713
awk 'BEGIN {FS=" |;";RS="}";} {print $2 " " $24 " " $11}' YourFileName
0
 
LVL 1

Author Comment

by:richsark
ID: 35009860
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 58

Expert Comment

by:amit_g
ID: 35010398
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
 
LVL 1

Author Comment

by:richsark
ID: 35010691
I am using Windows 7 64 bit.  I ran this in cygwin

Attached is the whole file.
dhcpd.staticleases.txt
0
 
LVL 58

Expert Comment

by:amit_g
ID: 35010986

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
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35011212
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
 
LVL 1

Author Comment

by:richsark
ID: 35011278
ok, do I create an sh file with the above code and run it as:

./find-stuff.sh dhcpd.staticleases.txt
0
 
LVL 58

Expert Comment

by:amit_g
ID: 35011305
You can do that. Change the YourFileName with $1 in the code if you want to pass the filename from command line.
0
 
LVL 1

Author Comment

by:richsark
ID: 35011327
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 35011461
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
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35012945
runs fine on my machine with the sameple you gave initially.  can you post a test file with the lines?
0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35012965
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
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35013006
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
 
LVL 1

Author Comment

by:richsark
ID: 35013347
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
 
LVL 58

Expert Comment

by:amit_g
ID: 35013402
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
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35013760
will fix in morning.  did not use your samole file just txt typed in the question ...... my bad
0
 
LVL 12

Expert Comment

by:tel2
ID: 35013800
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
 
LVL 1

Author Comment

by:richsark
ID: 35022625
Hello, point_pleasant

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

Just curiuos...
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35022776
I guess I'm not getting any love on this question...   *sigh*
0
 
LVL 1

Author Comment

by:richsark
ID: 35024478
Hello kaufmed,  I will let you know in a few. Hang in there :)
0
 
LVL 1

Author Closing Comment

by:richsark
ID: 35028933
This code worked just as I need.

Thanks for everones help !
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question