×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

AWK with regular Expressions Maybe

AWK with regular Expressions Maybe

AWK with regular Expressions Maybe

(OP)
Hi!

I'm an AWK Beginner and I have no idea how to deal with my problem.

when i do a Ldap request for user "ur002" i get an output like this (example):

username='ur002'
TAG='0',
VENDOR='2352',
ATTRIBUTE='4',
VALUE_STR='local'},

The numbers behind the Parameters Vendor an Attribute are the values, which you can find in 2 tables AAADICTIONARYVENDOR and AAADICTIONARYATTR

The programm i need to build with awk should first search "2352" in AAADICTIONARYVENDOR. The Table looks like this:

0|MARKONE|6728
1|IKINCI|5
2|UEC|9
3|FEREI|61
4|OCOCU|166
5|ALPER|2352

it has to copy the Value "ALPER" and paste it into the first output next to VENDOR

in the second step we have to deal with the table AAADICTIONARYATTR. This one is a little bit more difficult. it looks like this (also an excerpt)

845|MARKONE-ALLOW-ALL-VR-ACCESS|19|1|6728
846|MARKONE-ALT-CLI-ACCESS-LEVEL|20|2|6728
847|MARKONE-ALT-CLI-VROUTER-NAME|21|2|6728
848|MARKONE-SA-VALIDATE|22|1|6728
849|MARKONE-IGMP-ENABLE|23|1|6728
850|UEC-USER-PRIV|1|1|9
851|UEC-SERVICE-PROFILE|2|2|9
852|UEC-VPN-NAME|3|2|9
853|UEC-ISOS-VERSION|4|1|9
854|UEC-L2TP-TUNSET|5|1|9
855|ALPER-CLIENT-DNS-PRI|1|2|2352
856|ALPER-CLIENT-DNS-SEC|2|2|2352
857|ALPER-DHCP-MAX-LEASES|3|1|2352
858|ALPER-CONTEXT-NAME|4|2|2352
859|ALPER-BRIDGE-GROUP|5|2|2352
860|ALPER-BG-AGING-TIME|6|2|2352

In the first Output we had the attribute value 4, and the vendor 2352. So we have to find in this table the value 4|X|2352 and copy the full name (858|ALPER-CONTEXT-NAME|4|2|2352) also in first output.


So if i do a request for user 'ur002' i need to get this output:


username='ur002'

TAG='0',
VENDOR='2352', ALPER
ATTRIBUTE='4', 858|ALPER-CONTEXT-NAME|4|2|2352
VALUE_STR='local'},

I will be grateful for any help!

Blessed Christmas Time!

RE: AWK with regular Expressions Maybe

Hello SweetyXOXO,

From data you posted I created these 3 files:
sx_user.txt

CODE

username='ur002'
TAG='0',
VENDOR='2352',
ATTRIBUTE='4',
VALUE_STR='local'}, 

sx_vendor.txt

CODE

0|MARKONE|6728
1|IKINCI|5
2|UEC|9
3|FEREI|61
4|OCOCU|166
5|ALPER|2352 

sx_attrib.txt

CODE

845|MARKONE-ALLOW-ALL-VR-ACCESS|19|1|6728
846|MARKONE-ALT-CLI-ACCESS-LEVEL|20|2|6728
847|MARKONE-ALT-CLI-VROUTER-NAME|21|2|6728
848|MARKONE-SA-VALIDATE|22|1|6728
849|MARKONE-IGMP-ENABLE|23|1|6728
850|UEC-USER-PRIV|1|1|9
851|UEC-SERVICE-PROFILE|2|2|9
852|UEC-VPN-NAME|3|2|9
853|UEC-ISOS-VERSION|4|1|9
854|UEC-L2TP-TUNSET|5|1|9
855|ALPER-CLIENT-DNS-PRI|1|2|2352
856|ALPER-CLIENT-DNS-SEC|2|2|2352
857|ALPER-DHCP-MAX-LEASES|3|1|2352
858|ALPER-CONTEXT-NAME|4|2|2352
859|ALPER-BRIDGE-GROUP|5|2|2352
860|ALPER-BG-AGING-TIME|6|2|2352 

then I wrote following awk script, which processes all 3 files:
sx_ldap_user.awk

CODE

# Run:
#   awk -f sx_ldap_user.awk sx_user.txt sx_vendor.txt sx_attrib.txt
BEGIN {
  FS = "|"
  idx=0
}

# Line rules
{
  # chomp current line
  chomp_line()
}

FILENAME == ARGV[1] {
    # replace "=" with separator "|"
    sub(/=/, "|", $0)
    key = trim($1)
    val = trim($2)
    keys[idx++]=key
    user[key]=val
}

FILENAME == ARGV[2] {
  if (user["VENDOR"] ~ $3)  {
    vendor_val = $2
  }
}

FILENAME == ARGV[3] {
  if ($2 ~ vendor_val && user["ATTRIBUTE"] ~ $3)  {
    attrib_val = $0
  }
}

END {
  print "username" "=" user["username"]
  print "TAG" "=" user["TAG"]
  print "VENDOR" "=" user["VENDOR"] vendor_val
  print "ATTRIBUTE" "=" user["ATTRIBUTE"] attrib_val
  print "VALUE_STR" "=" user["VALUE_STR"]
}

# -- fuctions --
function chomp_line() {
  # strip out the carriage return or line feed at the end of current line
  # the function modifies global variable $0 (current line)
  sub(/\r$/, "", $0)
  sub(/\n$/, "", $0)
}

function strip() {
  # remove blanks from begin and end of line
  sub(/^[ \t]/, "", $0)
  sub(/[ \t]$/, "", $0)
}

function trim(fld) {
  # remove leading and trailing spaces from field
  #printf "*   '%s'\n", fld
  gsub(/^[ \t]+/,"",fld)
  #printf "**   '%s'\n", fld
  gsub(/[ \t]+$/,"",fld)
  #printf "**** '%s'\n", fld
  return fld
} 

Running the script delivers this result:

CODE

$ awk -f sx_ldap_user.awk sx_user.txt sx_vendor.txt sx_attrib.txt 
username='ur002'
TAG='0',
VENDOR='2352',ALPER
ATTRIBUTE='4',858|ALPER-CONTEXT-NAME|4|2|2352
VALUE_STR='local'}, 

I wish you nice Christmas.


RE: AWK with regular Expressions Maybe

(OP)
Hello mikrom,

thank you for your help, it is running well! (Y)

But there is a point i could not change.

In some user files there are some more Attributes or vendors. What can I do to catch all of them? btw the number of attributes and vendors is irregular.

I hope you can help me
thanks and regards

RE: AWK with regular Expressions Maybe

Hi SweetyXOXO,

First step would be to gather these information.
In the example above you had for the user one vendor and one attribute

CODE

VENDOR='2352',
ATTRIBUTE='4', 
I saved them in this array

CODE

user[VENDOR] = '2352'
user[ATTRIBUTE] = '4' 
If there would be more vendors and attributes e.g. like this

CODE

VENDOR='2352','2353'
ATTRIBUTE='4','5' 
or like this

CODE

VENDOR='2352',
ATTRIBUTE='4',
VENDOR='2353',
ATTRIBUTE='5' 
I would save them in the array with following values (which are again arrays)

CODE

user[VENDOR] = '2352,2353'
user[ATTRIBUTE] = '4,5' 
then in the further processing I could split the array value string in an array and process every element of this array.
Further processing would be dependent on the output you want to achieve.
(you didn't post any examples of the input files and the desired output)

RE: AWK with regular Expressions Maybe

(OP)
Hello mikrom,

thank you for your answer.
The programm should be able to handle input textfiles with any number of attributes and vendors.

The input file can look like this:

VENDOR='2352',
ATTRIBUTE='4',

and also look like this:

VENDOR='2352',
ATTRIBUTE='4',
VALUE_STR='local'},

uitem={
UITEMID='2',
VENDOR='2352',
ATTRIBUTE='5',
VALUE_STR='RESIDENTIAL'},


VENDOR='6728',
ATTRIBUTE='19',
VALUE_STR='local'},


the programm should be able to create an output file for the 2nd example like this:


VENDOR='2352', ALPER
ATTRIBUTE='4', 858|ALPER-CONTEXT-NAME|4|2|2352
VALUE_STR='local'},

uitem={
UITEMID='2',
VENDOR='2352', ALPER
ATTRIBUTE='5', 859|ALPER-BRIDGE-GROUP|5|2|2352
VALUE_STR='RESIDENTIAL'},


VENDOR='6728', MARKONE
ATTRIBUTE='19', 845|MARKONE-ALLOW-ALL-VR-ACCESS|19|1|6728
VALUE_STR='local'},




Im an absolute noob what awk is about and have no idea how to solve this. Therefore im very thankful for your help

thanks and regards


RE: AWK with regular Expressions Maybe

Hi SweetyXOXO,
and what have you tried so far ?

RE: AWK with regular Expressions Maybe

(OP)
Hi

now I had to upgrade from ldap to sql.

The tables with the dictionarys are the same.
The sx_user.txt looks like that know

CODE -->

USER_ID USER_NAME       USER_DIST       USER_ITEM_ID    TYPE    VALUE_SN        TAG     VENDOR  ATTRIBUTE
10000000123  ur002   1000    30000000028     1       1       0       2352    4
10000000123  ur002   1000    30000000038     1       2       0       2352    5
10000000123  ur002   1000    30000000048     1       3       0       2352    6 
unfortunately the columns are displaced, but im trying to solve this.

How can I change the awk script to get the wished result?
for example:

CODE -->

USER_ID         USER_NAME     USER_DIST      USER_ITEM_ID     VENDOR  ATTRIBUTE   ATTR_VALUE
10000000123     ur002         1000           30000000028      2352    4           858|ALPER-CONTEXT-NAME|4|2|2352
10000000123     ur002         1000           30000000038      2352    5           859|ALPER-BRIDGE-GROUP|5|2|2352
10000000123     ur002         1000           30000000048      2352    6           860|ALPER-BG-AGING-TIME|6|2|2352 


Can you help me?

RE: AWK with regular Expressions Maybe

You say, that sx_user.txt has changed from

CODE

username='ur002'
TAG='0',
VENDOR='2352',
ATTRIBUTE='4',
VALUE_STR='local'}, 
to

CODE

USER_ID      USER_NAME  USER_DIST   USER_ITEM_ID    TYPE VALUE_SN     TAG     VENDOR   ATTRIBUTE
10000000123  ur002      1000         30000000028       1        1       0       2352     4
10000000123  ur002      1000         30000000038       1        2       0       2352     5
10000000123  ur002      1000         30000000048       1        3       0       2352     6 
But what about the other files?
Didn't they changed ? Are they still as before, i.e. like this ?
sx_vendor.txt

CODE

0|MARKONE|6728
1|IKINCI|5
2|UEC|9
3|FEREI|61
4|OCOCU|166
5|ALPER|2352 

sx_attrib.txt

CODE

845|MARKONE-ALLOW-ALL-VR-ACCESS|19|1|6728
846|MARKONE-ALT-CLI-ACCESS-LEVEL|20|2|6728
847|MARKONE-ALT-CLI-VROUTER-NAME|21|2|6728
848|MARKONE-SA-VALIDATE|22|1|6728
849|MARKONE-IGMP-ENABLE|23|1|6728
850|UEC-USER-PRIV|1|1|9
851|UEC-SERVICE-PROFILE|2|2|9
852|UEC-VPN-NAME|3|2|9
853|UEC-ISOS-VERSION|4|1|9
854|UEC-L2TP-TUNSET|5|1|9
855|ALPER-CLIENT-DNS-PRI|1|2|2352
856|ALPER-CLIENT-DNS-SEC|2|2|2352
857|ALPER-DHCP-MAX-LEASES|3|1|2352
858|ALPER-CONTEXT-NAME|4|2|2352
859|ALPER-BRIDGE-GROUP|5|2|2352
860|ALPER-BG-AGING-TIME|6|2|2352 

RE: AWK with regular Expressions Maybe

(OP)
exactly, but i just managed to get the table in the right format so far.
now all the files are looking like your examples. but i have no idea how to change the awk script

RE: AWK with regular Expressions Maybe

Hi,

Because the master file changed the structure essentially, the awk script need to be reworked.

So, I named the file with the new structure sx_user2.txt:

CODE

USER_ID      USER_NAME  USER_DIST   USER_ITEM_ID    TYPE VALUE_SN     TAG     VENDOR   ATTRIBUTE
10000000123  ur002      1000         30000000028       1        1       0       2352     4
10000000123  ur002      1000         30000000038       1        2       0       2352     5
10000000123  ur002      1000         30000000048       1        3       0       2352     6 
The other 2 files sx_vendor.txt and sx_attrib.txt remain the same.

Then I tried this:
sx_ldap_user2.awk

CODE

# Run:
#   awk -f sx_ldap_user2.awk sx_user2.txt sx_vendor.txt sx_attrib.txt
BEGIN {
  FS = "|"
  # line number of the master file
  line_nr = 0
}

# Line rules

$0 ~ /^[ ]*$/ {
  # skip empty line
  next
}

{
  # chomp current line
  chomp_line()
}

FILENAME == ARGV[1] {
  # store header
  if (NR == 1) {
    header = $0
    header = header " ATTR_VALUE"
  }
  if (NR > 1 ){
    # store data lines into array
    data[line_nr] = $0
    # replace spaces with separator "|"
    gsub(/[ ]+/, "|", $0)
    # store VENDOR and ATTRIBUTE into arrays
    vendor[line_nr] = $8
    attribute[line_nr] = $9
    # 
    line_nr++
  }    
}

FILENAME == ARGV[2] {
  for (j = 0; j < line_nr; j++) {
    vendor_nr = vendor[j]
    if (vendor_nr == $3) {
       vendor_name[vendor_nr] = $2
    }
  }
}

FILENAME == ARGV[3] {
  for (j = 0; j < line_nr; j++) {
    if ($2 ~ vendor_name[vendor[j]] && attribute[j] == $3) {
      # add attribute value to data line
      data[j] = data[j] "       " $0
    }
  }
}


END {
  # print result
  print header
  for (j = 0; j < line_nr; j++) {
    print data[j] 
  }
}

# -- fuctions --
function chomp_line() {
  # strip out the carriage return or line feed at the end of current line
  # the function modifies global variable $0 (current line)
  sub(/\r$/, "", $0)
  sub(/\n$/, "", $0)
} 

Results

CODE

$ awk -f sx_ldap_user2.awk sx_user2.txt sx_vendor.txt sx_attrib.txt
USER_ID      USER_NAME  USER_DIST   USER_ITEM_ID    TYPE VALUE_SN     TAG     VENDOR   ATTRIBUTE ATTR_VALUE
10000000123  ur002      1000         30000000028       1        1       0       2352     4       858|ALPER-CONTEXT-NAME|4|2|2352
10000000123  ur002      1000         30000000038       1        2       0       2352     5       859|ALPER-BRIDGE-GROUP|5|2|2352
10000000123  ur002      1000         30000000048       1        3       0       2352     6       860|ALPER-BG-AGING-TIME|6|2|2352 

or you can redirect the output in a file sx_out.txt like this:

CODE

$ awk -f sx_ldap_user2.awk sx_user2.txt sx_vendor.txt sx_attrib.txt > sx_out.txt 

RE: AWK with regular Expressions Maybe

(OP)
Hello Mikrom,

wow, the code is looking pretty well, thank you very much!

But unfortunately it doesnt work :/

I checked all the files, they all look the same. The only difference is that i have column names above the columns in the attribute and vendor tables.
my result is looking like this

CODE -->

+----------------+-------------------+-----------+--------------+------+----------+------+--------+-----------+ ATTR_VALUE
| USER_ID        | USER_NAME         | USER_DIST | USER_ITEM_ID | TYPE | VALUE_SN | TAG  | VENDOR | ATTRIBUTE |
+----------------+-------------------+-----------+--------------+------+----------+------+--------+-----------+       +---------------------+-----------------------------------------+-------------------+-----------+--------+       +---------------------+-----------------------------------------+-------------------+-----------+--------+       +---------------------+-----------------------------------------+-------------------+-----------+--------+
| 10000000123  | ur002 |      1000 |  30000000048 |    1 |        1 |    0 |   2352 |        4|
| 10000000123  | uur02 |      1000 |  30000000048 |    1 |        2 |    0 |   2352 |        5|
| 10000000123  | uur02 |      1000 |  30000000048 |    1 |        3 |    0 |   2352 |        6|
| 10000000123  | ur002 |      1000 |  30000000048 |    1 |        4 |    0 |   2352 |        7|
+----------------+-------------------+-----------+--------------+------+----------+------+--------+-----------+       +---------------------+-----------------------------------------+-------------------+-----------+--------+       +---------------------+-----------------------------------------+-------------------+-----------+--------+       +---------------------+-----------------------------------------+-------------------+-----------+--------+ 

RE: AWK with regular Expressions Maybe

(OP)
oh okay i just noticed that the attr and vendor table files looks like this:

CODE -->

+---------------------+-----------------------------------------+-------------------+-----------+--------+
| AAADICTIONARYATTRID | ATTRIBUTENAME                           | ATTRIBUTEENCODING | VALUETYPE | VENDOR |
+---------------------+-----------------------------------------+-------------------+-----------+--------+
|                  845| MARKONE-ALLOW-ALL-VR-ACCESS             |                19 |         1 |   6728 |
|                  846| MARKONE-ALT-CLI-ACCESS-LEVEL            |                20 |         2 |   6728 |
|                  847| MARKONE-ALT-CLI-VROUTER-NAME            |                21 |         2 |   6728 |
|                  848| MARKONE-SA-VALIDATE                     |                22 |         1 |   6728 |
|                  849| MARKONE-IGMP-ENABLE                     |                23 |         1 |   6728 |
|                  850| UEC-USER-PRIV                           |                 1 |         1 |      9 |
|                  851| UEC-SERVICE-PROFILE                     |                 2 |         2 |      9 |
|                  852| UEC-VPN-NAME                            |                 3 |         2 |      9 | 
|                  853| UEC-ISOS-VERSION                        |                 4 |         1 |      9 |
|                  854| UEC-L2TP-TUNSET                         |                 5 |         1 |      9 |
|                  855| ALPER-CLIENT-DNS-PRI                    |                 1 |         2 |   2352 |
|                  856| ALPER-CLIENT-DNS-SEC                    |                 2 |         2 |   2352 |
|                  857| ALPER-DHCP-MAX-LEASES                   |                 3 |         1 |   2352 |
|                  858| ALPER-CONTEXT-NAME                      |                 4 |         2 |   2352 |
|                  859| ALPER-BRIDGE-GROUP                      |                 5 |         2 |   2352 |
|                  860| ALPER-BG-AGING-TIME                     |                 6 |         2 |   2352 | 

the vendor file like this

CODE -->

+-----------------------+------------+-------------------+
| AAADICTIONARYVENDORID | VENDORNAME | VENDORENCODINGINT |
+-----------------------+------------+-------------------+
|                     0 | MARKONE    |              6728 |
|                     1 | IKINCI     |                 5 |
|                     2 | UEC        |                 9 |
|                     3 | FEREI      |                61 |
|                     4 | OCOCU      |               166 |
|                     5 | ALPER      |              2352 | 

the user file like this

CODE -->

+----------------+-------------------+-----------+--------------+------+----------+------+--------+-----------+
| USER_ID        | USER_NAME         | USER_DIST | USER_ITEM_ID | TYPE | VALUE_SN | TAG  | VENDOR | ATTRIBUTE |
+----------------+-------------------+-----------+--------------+------+----------+------+--------+-----------+
| 10000000123    | ur002             |      1000 |  30000000048 |    1 |        1 |    0 |   2352 |        4|
| 10000000123    | uur02             |      1000 |  30000000048 |    1 |        2 |    0 |   2352 |        5|
| 10000000123    | uur02             |      1000 |  30000000048 |    1 |        3 |    0 |   2352 |        6|
| 10000000123    | ur002             |      1000 |  30000000048 |    1 |        4 |    0 |   2352 |        7|
+----------------+-------------------+-----------+--------------+------+----------+------+--------+-----------+ 
Im sorry, i thought it would not have any influence in awk :(

RE: AWK with regular Expressions Maybe

Hi SweetyXOXO,

Quote:


i thought it would not have any influence in awk

Naturally, changing the structure of the tables has influence on the awk-script smile
For example you have in all your newest files headers with frames which take first three lines - I had header only in the first file and this took only one line.
Then I had a file sx_user2.txt, where the columns were separated only by spaces, in your file they are separated by "|"
... all these little things have a big influence on the processing script.

You have 2 choices:
1. You need to transform your tables so they match the structure I used and then the awk script should work.
2. You have to slightly modify the script so it matches the structure of your tables.
IMO the second choice would be better for you - so you will learn a little bit about awk.

RE: AWK with regular Expressions Maybe

(OP)
Hello Mikrom,

you're right i decided for the second one.

I thought it would be enough if when I find out the right field numbers, I expectet at least a little change in the output

CODE -->

# Run:
#  awk -f sx_ldap_user2.awk sx_user2.txt sx_vendor.txt sx_attrib.txt
BEGIN {
  FS = "|"
  # line number of the master file
  line_nr = 0
}

# Line rules

$0 ~ /^[ ]*$/ {
  # skip empty line
  next
}

{
  # chomp current line
  chomp_line()
}

FILENAME == ARGV[1] {
  # store header
  if (NR == 1) {
#$20 because I thought it is the next empty field in the table with user. Also tried $0 $1 and $2
    header = $20
    header = header " ATTR_VALUE"
  }
  if (NR > 1 ){
    # store data lines into array
    data[line_nr] = $0
    # replace spaces with separator "|"
#i don't get why we are doing this |
    gsub(/[ ]+/, "|", $0)
    # store VENDOR and ATTRIBUTE into arrays
#the new field numbers
    vendor[line_nr] = $16
    attribute[line_nr] = $18
    # 
    line_nr++
  }    
}

FILENAME == ARGV[2] {
  for (j = 0; j < line_nr; j++) {
    vendor_nr = vendor[j]
    if (vendor_nr == $6) {
       vendor_name[vendor_nr] = $4
    }
  }
}

FILENAME == ARGV[3] {
  for (j = 0; j < line_nr; j++) {
    if ($4 ~ vendor_name[vendor[j]] && attribute[j] == $6) {
      # add attribute value to data line
      data[j] = data[j] "       " $0
    }
  }
}


END {
  # print result
  print header
  for (j = 0; j < line_nr; j++) {
    print data[j] 
  }
}

# -- fuctions --
function chomp_line() {
  # strip out the carriage return or line feed at the end of current line
  # the function modifies global variable $0 (current line)
# maybe i have to change something here?
  sub(/\r$/, "", $0)
  sub(/\n$/, "", $0)
} 

but there is nothing happening in the output .. what should I also change?

RE: AWK with regular Expressions Maybe

I see, you misunderstood the essential things about awk:
Please google and read something about awk - there are lots of tutorials and examples free available.
Please note, that without a little effort, there is no success.

For an gentle introduction:
awk processes a file line by line
So, $0 means the whole line.
i.e., if you have set the field separator FS equal to "|"
and you have a file with a lines like

CODE

foo | bar | baz | spam | eggs | foobar 
then $0 means the whole line (above)
$1 means the 1. column of the line i.e. "foo"
and $5 means the 5. column which contains "eggs"

IMO it's not complicated but simple, can you undestood this ?

About your other questions:
the functions sub() means substitute (only one) and gsub() global substitute, (i.e everything)

RE: AWK with regular Expressions Maybe

(OP)
Hi,

so i tried to fix the problem but i am an absolute looser in awk and i was never a good programmer and i really have to solve this problem.
Can you help me please?


regards

RE: AWK with regular Expressions Maybe

Quote (SweetyXOXO )



Can you help me please?

Hi SweetyXOXO,
Of course I can smile

What about this ?

sx_ldap_user3.awk

CODE

# Run:
#   awk -f sx_ldap_user3.awk sx_user3.txt sx_vendor3.txt sx_attrib3.txt
BEGIN {
  FS = "|"
  # line number of the master file
  line_nr = 0
}

# Line rules
{ 
  # chomp current line
  chomp_line()
}

{
  # remove spaces
  gsub(/[ ]+/, "", $0)
  # remove "|" characters from the beginning and the end of line
  gsub(/^\|/, "", $0)
  gsub(/\|$/, "", $0)
}

$0 ~ /^[ ]*$/ {
  # skip empty line
  next
}

$0 ~ /^[+]*[-]+/ {
  # skip frame line
  next
}

FILENAME == ARGV[1] {
  # store header
  if (FNR == 2) {
    header = $0  
    header = header "|ATTR_VALUE"
  }
  if (FNR > 3 ){
    # store VENDOR and ATTRIBUTE into arrays
    vendor[line_nr] = $8
    attribute[line_nr] = $9
    # replace "|" with space and store data line into array
    gsub(/\|/, " ", $0)
    data[line_nr] = $0
    # 
    line_nr++
  }    
}

FILENAME == ARGV[2] {
  if (FNR > 3 ){
    for (j = 0; j < line_nr; j++) {
      vendor_nr = vendor[j]
      if (vendor_nr == $3) {
        vendor_name[vendor_nr] = $2
      }
    }
  }
}

FILENAME == ARGV[3] {
  if (FNR > 3 ){
    for (j = 0; j < line_nr; j++) {
      if ($2 ~ vendor_name[vendor[j]] && attribute[j] == $3) {
        # add attribute value to data line
        data[j] = data[j] "       " $0
      }
    }  
  }
}

END {
  # *** print result ***
  # define format string
  format_string = "%-12s  %-9s  %-9s  %-12s  %-6s  %-10s  %s\n"

  # header
  # split header into array
  split(header, h, /\|/)
  # print only columns which are needed
  printf format_string, h[1], h[2], h[3], h[4], h[8], h[9], h[10]

  # data
  for (j = 0; j < line_nr; j++) {
    # split data into array
    #print data[j]  
    split(data[j], d, /[ ]+/)
    # print only columns which are needed
    printf format_string, d[1], d[2], d[3], d[4], d[8], d[9], d[10] 
  }
}

# -- fuctions --
function chomp_line() {
  # strip out the carriage return or line feed at the end of current line
  # the function modifies global variable $0 (current line)
  sub(/\r$/, "", $0)
  sub(/\n$/, "", $0)
} 

if I try it with your input files
sx_user3.txt

CODE

+----------------+-------------------+-----------+--------------+------+----------+------+--------+-----------+
| USER_ID        | USER_NAME         | USER_DIST | USER_ITEM_ID | TYPE | VALUE_SN | TAG  | VENDOR | ATTRIBUTE |
+----------------+-------------------+-----------+--------------+------+----------+------+--------+-----------+
| 10000000123    | ur002             |      1000 |  30000000048 |    1 |        1 |    0 |   2352 |          4|
| 10000000123    | uur02             |      1000 |  30000000048 |    1 |        2 |    0 |   2352 |          5|
| 10000000123    | uur02             |      1000 |  30000000048 |    1 |        3 |    0 |   2352 |          6|
| 10000000123    | ur002             |      1000 |  30000000048 |    1 |        4 |    0 |   2352 |          7|
+----------------+-------------------+-----------+--------------+------+----------+------+--------+-----------+ 

sx_vendor3.txt

CODE

+-----------------------+------------+-------------------+
| AAADICTIONARYVENDORID | VENDORNAME | VENDORENCODINGINT |
+-----------------------+------------+-------------------+
|                     0 | MARKONE    |              6728 |
|                     1 | IKINCI     |                 5 |
|                     2 | UEC        |                 9 |
|                     3 | FEREI      |                61 |
|                     4 | OCOCU      |               166 |
|                     5 | ALPER      |              2352 | 

sx_attrib3.txt

CODE

+---------------------+-----------------------------------------+-------------------+-----------+--------+
| AAADICTIONARYATTRID | ATTRIBUTENAME                           | ATTRIBUTEENCODING | VALUETYPE | VENDOR |
+---------------------+-----------------------------------------+-------------------+-----------+--------+
|                  845| MARKONE-ALLOW-ALL-VR-ACCESS             |                19 |         1 |   6728 |
|                  846| MARKONE-ALT-CLI-ACCESS-LEVEL            |                20 |         2 |   6728 |
|                  847| MARKONE-ALT-CLI-VROUTER-NAME            |                21 |         2 |   6728 |
|                  848| MARKONE-SA-VALIDATE                     |                22 |         1 |   6728 |
|                  849| MARKONE-IGMP-ENABLE                     |                23 |         1 |   6728 |
|                  850| UEC-USER-PRIV                           |                 1 |         1 |      9 |
|                  851| UEC-SERVICE-PROFILE                     |                 2 |         2 |      9 |
|                  852| UEC-VPN-NAME                            |                 3 |         2 |      9 |
|                  853| UEC-ISOS-VERSION                        |                 4 |         1 |      9 |
|                  854| UEC-L2TP-TUNSET                         |                 5 |         1 |      9 |
|                  855| ALPER-CLIENT-DNS-PRI                    |                 1 |         2 |   2352 |
|                  856| ALPER-CLIENT-DNS-SEC                    |                 2 |         2 |   2352 |
|                  857| ALPER-DHCP-MAX-LEASES                   |                 3 |         1 |   2352 |
|                  858| ALPER-CONTEXT-NAME                      |                 4 |         2 |   2352 |
|                  859| ALPER-BRIDGE-GROUP                      |                 5 |         2 |   2352 |
|                  860| ALPER-BG-AGING-TIME                     |                 6 |         2 |   2352 |
|                  860| ALPER-SWEETY-XOXO                       |                 7 |         1 |   2352 | 

I get this output

CODE

$ awk -f sx_ldap_user3.awk sx_user3.txt sx_vendor3.txt sx_attrib3.txt
USER_ID       USER_NAME  USER_DIST  USER_ITEM_ID  VENDOR  ATTRIBUTE   ATTR_VALUE
10000000123   ur002      1000       30000000048   2352    4           858|ALPER-CONTEXT-NAME|4|2|2352
10000000123   uur02      1000       30000000048   2352    5           859|ALPER-BRIDGE-GROUP|5|2|2352
10000000123   uur02      1000       30000000048   2352    6           860|ALPER-BG-AGING-TIME|6|2|2352
10000000123   ur002      1000       30000000048   2352    7           860|ALPER-SWEETY-XOXO|7|1|2352 

Say, is this what you need ?

RE: AWK with regular Expressions Maybe

(OP)
wow! you are a magician mikrom! *-*

thank you very much!
now i will try to get the output in a table form, and then it will be perfect!

xx

RE: AWK with regular Expressions Maybe

Hi SweetyXOXO,
You are welcome.
I hope it helped you a little bit to understand what about is awk.
Analyze the code, experiment with it, so you would be able to write a next little script self.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close