The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare XML/flat file with UNIX file system structure shafi2all High Level Programming 6 08-15-2008 03:15 AM
Awk to convert a flat file to CSV file rkumudha Shell Programming and Scripting 14 07-17-2008 10:48 PM
match string in a file to file in a directory Jae Shell Programming and Scripting 5 01-19-2008 01:11 AM
Remove 5th character from Field1 & Print Raynon Shell Programming and Scripting 9 12-14-2007 05:57 AM
Converting Pivot file to flat file vskr72 Shell Programming and Scripting 2 10-18-2005 05:41 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-03-2009
frankie_konin frankie_konin is offline VIP Member  
VIP Member
  
 

Join Date: Feb 2009
Posts: 12
Very weird. Checked, proof:

cat fran4.awk

Code:
BEGIN {
  FScsv=","
  FScfg=FS

  if (sep=="") sep="."

  qq=sprintf("%c", 034)
}
FNR==NR && FNR==1 {FS=FScsv;$1=$1}
FNR!=NR && FNR==1 {FS=FScfg; $1=$1}
FNR==NR {gsub(qq, "", $1);gsub(qq, "", $2); csv[$1]=$2; next}

!/{/ {
    n=split($2, _a,sep)
    for(i=1; i<=n; i++)
      if (_a[i] in csv) _a[i]=csv[_a[i]]
    for(i=1; i<=n; i++)
      $2=(i==1) ? _a[i] : $2 sep _a[i]
}
1

cat test.csv


Code:
"hello","byebye"
"other","data"
"hello2","byebye2"
"hello3","byebye3"
"other","data"

cat services.txt


Code:
define service{
        use                             generic-service
        service_description             TCP_80
        host_name                       hello,hello2,hello3,nochangematch
        contact_groups                  
        check_command                
}

Running code:

nawk -v sep=',' -f fran4.awk test.csv services.txt


Code:
define service{
use generic-service
service_description TCP_80
host_name hello,byebye2,byebye3,nochangematch
        contact_groups                  
        check_command                
}

Didn't change first hello

cat nagios.cfg.txt


Code:
define host{
        use                          generic-host
        host_name                    hello
        alias                        hello.hello
        address                      127.0.0.1
        contact_groups               mymail
        parents                      hello3
}

Running code:

nawk -v sep='.' -f fran4.awk test.csv nagios.cfg.txt


Code:
define host{
use generic-host
host_name hello
alias hello.hello
address 127.0.0.1
contact_groups mymail
parents byebye3
}

Changed only parents field.

Is fran4.awk latest code, matches yours?

Just in case did for all files dos2unix as well and:

file -i fran4.awk test.csv nagios.cfg.txt services.txt


Code:
fran4.awk:      text/plain charset=us-ascii
test.csv:       text/plain charset=us-ascii
nagios.cfg.txt: text/plain charset=us-ascii
services.txt:   text/plain charset=us-ascii

  #2 (permalink)  
Old 02-03-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
strange, your awk code is the same as mine.
hmmm... - it must be something in the data files.
couxld you 'cat -vet nagios.cfg.txt' and 'cat -vet test.csv' and post the results here within the code tags, pls.
  #3 (permalink)  
Old 02-03-2009
frankie_konin frankie_konin is offline VIP Member  
VIP Member
  
 

Join Date: Feb 2009
Posts: 12
cat -vet nagios.cfg.txt


Code:
define host{$
        use                          generic-host$
        host_name                    hello$
        alias                        hello.hello$
        address                      127.0.0.1$
        contact_groups               mymail$
        parents                      hello3$
}$

cat -vet test.csv


Code:
"hello","byebye"$
"other","data"$
"hello2","byebye2"$
"hello3","byebye3"$
"other","data"$

cat -vet services.txt


Code:
 
define service{$
        use                             generic-service$
        service_description             TCP_80$
        host_name                       hello,hello2,hello3,nochangematch$
        contact_groups                  $
        check_command                $
}$

cat -vet fran4.awk


Code:
 
BEGIN {$
  FScsv=","$
  FScfg=FS$
$
  if (sep=="") sep="."$
$
  qq=sprintf("%c", 034)$
}$
FNR==NR && FNR==1 {FS=FScsv;$1=$1}$
FNR!=NR && FNR==1 {FS=FScfg; $1=$1}$
FNR==NR {gsub(qq, "", $1);gsub(qq, "", $2); csv[$1]=$2; next}$
$
!/{/ {$
    n=split($2, _a,sep)$
    for(i=1; i<=n; i++)$
      if (_a[i] in csv) _a[i]=csv[_a[i]]$
    for(i=1; i<=n; i++)$
      $2=(i==1) ? _a[i] : $2 sep _a[i]$
}$
1$

  #4 (permalink)  
Old 02-03-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
hmm...... this is bizarre - works just fine here. Looks like you're having issues with the lookup of "hello" in the test.csv file.
could change nagios.cfg.txt like so:

Code:
define host{
        use                          generic-host
        host_name                    other
        alias                        other.other
        address                      127.0.0.1
        contact_groups               mymail
        parents                      hello3
}

and:
nawk -v sep='.' -f fran4.awk test.csv nagios.cfg.txt
  #5 (permalink)  
Old 02-03-2009
frankie_konin frankie_konin is offline VIP Member  
VIP Member
  
 

Join Date: Feb 2009
Posts: 12
Yep, if changing to "other" then works

cat nagios.cfg.txt

Code:
define host{
        use                          generic-host
        host_name                    other
        alias                        other.other
        address                      127.0.0.1
        contact_groups               mymail
        parents                      hello3
}

nawk -v sep='.' -f fran4.awk test.csv nagios.cfg.txt


Code:
define host{
use generic-host
host_name data
alias data.data
address 127.0.0.1
contact_groups mymail
parents byebye3
}

Works, like a champ

Then again with this concept:

cat nagios.cfg.txt


Code:
define host{
        use                          generic-host
        host_name                    hello
        alias                        other.hello2
        address                      127.0.0.1
        contact_groups               mymail
        parents                      hello3
}

nawk -v sep='.' -f fran4.awk test.csv nagios.cfg.txt


Code:
define host{
use generic-host
host_name hello
alias data.byebye2
address 127.0.0.1
contact_groups mymail
parents byebye3
}

You see, hello in host_name field didn't change.
all other fields okey.

This is like my awk hates "hello". It want's it to be "hell'
Just joking.. but seriously.. Very weird case.
  #6 (permalink)  
Old 02-03-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
heh
try this test.csv:

Code:
"other","data"
"hello","byebye"
"hello2","byebye2"
"hello3","byebye3"
"other","data"

  #7 (permalink)  
Old 02-03-2009
frankie_konin frankie_konin is offline VIP Member  
VIP Member
  
 

Join Date: Feb 2009
Posts: 12
Quote:
Originally Posted by vgersh99 View Post
heh
try this test.csv:

Code:
"other","data"
"hello","byebye"
"hello2","byebye2"
"hello3","byebye3"
"other","data"
cat nagios.cfg.txt


Code:
define host{
        use                          generic-host
        host_name                    hello
        alias                        other.hello2
        address                      127.0.0.1
        contact_groups               mymail
        parents                      hello3
}

cat test2.csv


Code:
"other","data"
"hello","byebye"
"hello2","byebye2"
"hello3","byebye3"
"other","data"

nawk -v sep='.' -f fran4.awk test2.csv nagios.cfg.txt


Code:
define host{
use generic-host
host_name byebye
alias data.byebye2
address 127.0.0.1
contact_groups mymail
parents byebye3
}

So now it changed. Hmm whats the case with your given test.csv
then?

diff test.csv test2.csv


Code:
1d0
< "hello","byebye"
2a2
> "hello","byebye"

Closed Thread

Bookmarks

Tags
host_name, nagios, update

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0