Solved

grep out lines from a file

Posted on 2011-03-01
22
370 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
  • 9
  • 5
  • 5
  • +2
22 Comments
 
LVL 1

Author Comment

by:richsark
Comment Utility
I tried

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

Expert Comment

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

Author Comment

by:richsark
Comment Utility
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
 
LVL 58

Expert Comment

by:amit_g
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 8

Expert Comment

by:point_pleasant
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
will fix in morning.  did not use your samole file just txt typed in the question ...... my bad
0
 
LVL 11

Expert Comment

by:tel2
Comment Utility
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
Comment Utility
Hello, point_pleasant

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

Just curiuos...
0
 
LVL 74

Expert Comment

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

Author Comment

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

Author Closing Comment

by:richsark
Comment Utility
This code worked just as I need.

Thanks for everones help !
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now