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!

*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.

Jobs

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?

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!

Resources

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