Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegEx help

Status
Not open for further replies.

Mag0007

MIS
Feb 15, 2005
829
US
Hi All,

I have a file like this:

System Model: 7025-M80
Processor Type: Intel
Number Of Processors: 4
Memory Size: 1024MB
Good Memory Size: 1024MB
Firmware Version: IBM,RE
Console Login: enable
Auto Restart: false
Full Core: false

Network Information
Host Name: testbox
IP Address: 16.8.1.2
Sub Netmask: 255.255.255.0
Gateway: 16.8.1.1

Paging Space Information
Total Paging Space: 100MB
Percent Used: 26%

I would like to get the values of:
" Host Name : testbox" to "NodeName = testbox" (notice the spaces)
" Percent Used: 36%" to "Percent Used is 36%
 
something like this ?
sed 's! *Host Name : !NodeName = !;s! Percent Used: !Percent Used is !' input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
something like this, but it seems your solution is not working. I get the same result back...
 
I've copy/paste your posted data into a file named input.
I ran the following:
sed 's! *Host Name: !NodeName = !;s! Percent Used: !Percent Used is !' input > output
diff input output

The result is:
12c12
< Host Name: testbox
---
> NodeName = testbox
19c19
< Percent Used: 26%
---
> Percent Used is 26%


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

Just because we are in AWK forum :
Code:
awk '{gsub(/.*Host Name:/,"NodeName =");gsub(/Percent Used:/,"Percent Used is")}1' /input/file
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

Feherke.
 
Thanks everyone for your responses. How can I only print these selections (Hostname, Percent Used, etc..) ?

 
sed -n 's! *Host Name: !NodeName = !p;s! Percent Used: !Percent Used is !p' input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Following PHV's suit is this what you want?

awk '{gsub(/.*Host Name:/,"NodeName =");gsub(/Percent Used:/,"Percent Used is")}1' testfile|grep -i "node\|percent"

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
Actually, following from feherke ;)...

awk '{gsub(/.*Host Name:/,"NodeName =");gsub(/Percent Used:/,"Percent Used is")}1' testfile|grep -i "node\|percent"|sed 's/ //g;s/N/ N/g'

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
awk '{gsub(/.*Host Name:/,"NodeName =");gsub(/Percent Used:/,"Percent Used is");gsub(/NodeName =/," NodeName =");gsub(/ Percent Used is/,"Percent Used is")}1' testfile|grep -i "node\|percent"

awk only... Alot of typing as opposed to just throwing in sed...

awk '{gsub(/.*Host Name:/,"NodeName =");gsub(/Percent Used:/,"Percent Used is")}1' testfile|grep -i "node\|percent"|sed 's/ //g;s/N/ N/g'

206 chars vs. 152

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
weird... stupid dupes...

awk '{gsub(/.*Host Name:/,"NodeName =");gsub(/Percent Used:/,"Percent Used is")}1' testfile|grep -i "node\|percent"|sed 's/ //g;s/N/ N/g'|sed -n '3,4p' that works as far as I can tell

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
#!/usr/bin/perl
open (F, "file");
while ($row = <F>) {
$row =~ s/ Host Name: testbox/NodeName = testbox/g;
$row =~s/ Percent Used: 36%/Percent Used is 36%/g;
print $row;
}

close(F);

$row =~s/ Percent Used: 36%/Percent Used is 36%/g;

:( too long....

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
Hi

segment, I appreciate your efforts and do not want to criticize your suggestions... But please note that your solution is called UUOG. [tt]awk[/tt] itself can do the [tt]grep[/tt] job too :
Code:
awk '/Host Name|Percent Used/{gsub(/.*Host Name:/,"NodeName =");gsub(/Percent Used:/,"Percent Used is");print}'
 /input/file

[gray]# or[/gray]

awk '{gsub(/.*Host Name:/,"NodeName =");gsub(/Percent Used:/,"Percent Used is")}/NodeName|Percent Used is/' /input/file
Regarding your [tt]perl[/tt] code, I think would be shorter using the same approach as in the [tt]sed[/tt] and [tt]awk[/tt] codes :
Code:
perl -pe '!/Host Name|Percent Used/&&s/.*\n//;s/.*Host Name:/NodeName =/;s/Percent Used:/Percent Used is/' /input/file

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top