awk/grep type question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk/grep type question
# 1  
Old 12-12-2009
awk/grep type question

Hi there, I have a Solaris machine with an ifconfig output similar to below

Code:
lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        inet 127.0.0.1 netmask ff000000 
e1000g0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        inet 10.0.0.1 netmask ffff0000 broadcast 10.0.255.255
        ether 0:14:4f:1f:xx:xx 
e1000g0:1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        inet 10.0.0.2 netmask ffff0000 broadcast 10.0.255.255
e1000g0:2: flags=201000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4,CoS> mtu 1500 index 2
        zone virtual-server67
        inet 10.0.0.3 netmask ffff0000 broadcast 10.0.255.255
e1000g0:3: flags=201000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4,CoS> mtu 1500 index 2
        zone virtual-server68
        inet 10.0.0.4 netmask ffff0000 broadcast 10.0.255.255

As you can see I have one physical interface called e1000g0 and three subsequent virtual interfaces on e1000g0. The first one is just a regular secondary virtual interface, but the second and third are attached to Solaris Zones (virtual servers) and subsequently have an extra line in the output defining the zone it is attahced to (in red above). I have to write a process that will cycle through theses 4 interfaces (in this case 1 physical and 3 virtual) populating a variable, lets call it VAR, with the virtual server name found on the lines highlighted in red above (additional example below)

Code:
zone virtual-server68

But where there is no "zone <virtualserver name>" line for an interface, be it Physical or Virtual (as with e1000g0 and e1000g0:1) I want to just poulate VAR with a "-"

My question is really, whilst I am happy using awk to set VAR with the value of that line if it existed on ALL interfaces, I am a little unsure as to how I use it to make the determination of whether the line exists for a interface then either grab the value or set it to "-" accordingly ?

would i need to use a mixture of tools to achieve this or can it all be done within awk?

any help on this would be greatly appreciated
# 2  
Old 12-13-2009
Code:
$ cat Test
VAR=( $(awk '
 /^e10/ { Z[++C] = "-"; next }
 /zone/ { Z[C] = $2 }
 END { for( D = 1; D <= C; D++ )
         printf "%s ", Z[D]
 }
' file1))

echo ${VAR[0]}
echo ${VAR[1]}
echo ${VAR[2]}
echo ${VAR[3]}

$ ./Test
-
-
virtual-server67
virtual-server68

A little simpler
Code:
awk '
 /^e10/ { if( C++ ) print Z?Z:"-"; Z = "" }
 /zone/ { Z=$2 }
 END { print Z?Z:"-" }
'


Last edited by Scott; 12-13-2009 at 08:45 AM..
# 3  
Old 12-13-2009
thats great, helps me out wit a problem I have too

Last edited by rethink; 12-13-2009 at 08:10 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Count occurrence of string (based on type) in a column using awk

Hello, I have a table that looks like what is shown below: AA BB CC XY PQ RS AA BB CC XY RS I would like the total counts depending on the set they belong to: if search pattern is in {AA, BB, CC} --> count them as Type1 | wc -l (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

2. Shell Programming and Scripting

Grep / awk /sed question

Hi All, I have a file with following sample data. I am also attaching the file. (Est) (Est) Jobs Sch Workstation Job Stream SchedTime State Pr Start Elapse # OK Lim POOL #ACR_BILLING 0005 08/15 ... (2 Replies)
Discussion started by: Kevin Tivoli
2 Replies

3. Shell Programming and Scripting

regex, awk, grep question "how to"

I've been working on this for 2 days and I'm not getting far. It is time to turn to you guys. With the data below, I am trying to create a file that looks like this: I'd like to use some form of egrep I think. AY#box#P04prod_to_contingency s AY#cmd#P04dump_cont_db s AY#cmd#P04get_on_ice_job s... (2 Replies)
Discussion started by: rawbi01
2 Replies

4. Shell Programming and Scripting

awk vs grep question

Hello, I am trying to use awk instead of grep command. Is there a way I can search for a pattern and show 10 lines before it (grep -B) using awk ot nawk? (5 Replies)
Discussion started by: alekkz
5 Replies

5. Shell Programming and Scripting

Grep to find single instances of each ERROR type

i have a file that contents multiple instances of the same ERROR.Below the content of the file ERROR_FILE.txt Archiver6.log:2009-05-25 17:58:44,385 ERROR - CleanLPDataMessage: Missing Intervals: 2 Archiver6.log:2009-05-25 18:27:36,056 ERROR - CleanLPDataMessage: Missing Intervals: 5... (5 Replies)
Discussion started by: ali560045
5 Replies

6. Shell Programming and Scripting

General question about the relationship between KSH and sed/grep/awk etc

Greetings all, Unix rookie here, just diving into ksh scripting for the first time. My question may seem confusing but please bear with me: If I'm understanding everything I'm reading properly, it seems like the ksh language itself doesn't have a lot of string manipulation functions of... (2 Replies)
Discussion started by: DalTXColtsFan
2 Replies

7. Shell Programming and Scripting

Problem with type-casting in awk

Hi I'm trying to write a script which takes an integer as input and checks for files with size > input and displays it on screen. The code I've tried : echo 'Enter the min file size expected' read filesize du -ah <folder name> | awk -F: '$1>int(filesize) {print $1,$2)' is always... (28 Replies)
Discussion started by: vivek.bharadwaj
28 Replies

8. Shell Programming and Scripting

Awk type checking

Hello, How to check if two variables( fields in awk) have the same datatype ( INTEGER , REAL, TEXT ) ? (2 Replies)
Discussion started by: scotty_123
2 Replies

9. Shell Programming and Scripting

question on sed grep awk from variable

i am still confusing on how to use sed, grep and awk if the input is not a file but a variable. such as: a="hello world" b="how are you" c="best wish to you" d="222,333,444" what if i want to check which variable $a,$b,$c,$d have contain "you" what if i want to replace the word "you"... (9 Replies)
Discussion started by: 3Gmobile
9 Replies
Login or Register to Ask a Question