1

I have set an export X25DATAF=/home/Bob/X25/Datafiles in my profile and that's what is outputted when I do a echo $X25DATAF.

I want to replace /home/Bob/X25/Datafiles in the awk below with the $X25DATAF in the 2 places it's in the awk program. But, I can't get it to work (ie: $X25DATAF/ALL_SW_DB.txt). This way all I have to do is set the export to the path on whichever system I'm on to run this awk program (and others like it).

awk -F"\t" ' 
FILENAME=="Sites1" {
  cllina[$0]=$0
}
FILENAME=="/home/bob/X25/DataFiles/ALL_SW_DB.txt" {
  swclli=hostclli=""
  swclli=substr($7,1,8)
  hostclli=substr($3,1,8)
  fclli[hostclli]++
  fclli[swclli]++

  if(hostclli==cllina[hostclli]) {
    if(swclli!=hostclli) {
      printf"REM|%s|%s|%s|%s|%s|%s|%s|%s|%s-%s\n",hostclli,$4,$3,$6,$9,$7,$12,$13,$5,$14 >>"host_remote"
    }
   }

  if(swclli==cllina[swclli]) {
      printf"HOST|%s|%s|%s|%s|%s|%s|%s|%s|%s-%s\n",swclli,$4,$3,$6,$9,$7,$12,$13,$5,$14 >>"host_remote"
    }
 }
 END{
  for(x in cllina)
    if(fclli[x]=="") printf"%s\n",x >"site1.err"
}' Sites1 /home/bob/X25/DataFiles/ALL_SW_DB.txt
2
  • if you only have 2 files, why not simply use FILENAME!="Sites1". Or use the awk variable ARGIND which says which file you are looking at, 1 or 2.
    – meuh
    Sep 12, 2015 at 7:41
  • Not sure what you mean, but in your example, you can use ARGV[2] in awk program to get /home/bob/X25/DataFiles/ALL_SW_DB.txt, you passed it as second argument to your awk.
    – cuonglm
    Sep 12, 2015 at 7:42

2 Answers 2

0

As usual the construction FNR==NR (file row number is equal whole row number) is used for 1st file and next operation to skip 2nd file instructions:

awk -F'\t' -v OFS='|' '
    FNR==NR{
        cllina[$0]=1
        next
    }
    {
        swclli=hostclli=""
        …
        fclli[swclli]++
    }
    cllina[hostclli] && swclli!=hostclli{
        print "REM",hostclli,$4,$3,$6,$9,$7,$12,$13,$5,$14
    }
    cllina[swclli]{
        print "HOST",swclli,$4,$3,$6,$9,$7,$12,$13,$5,$14
    }
    …
    }' Sites1 /home/bob/X25/DataFiles/ALL_SW_DB.txt >>host_remote

or see comments to your question.

7
  • This is the whole code I have been using for 2 years. Several other user want to run it on their systems and that's why I want to substitute my export of X25DATAF=/home/Bob/X25/DATAFILES to replace the path name in the AWK prog for the file ALL_SW_DB.txt. I have several other AWKs that use different files but they all are in that same directory.
    – BobEng
    Sep 12, 2015 at 17:45
  • The code is too long for the comment by about 10 lines. The main question is how do I replace the /home/Bob/X25/Datafiles (the path to the ALL_SW_DB.txt) with the $X25DATAF I exported from my profile. All the other files are in whichever Dir the AWK is running from.
    – BobEng
    Sep 12, 2015 at 17:50
  • This is the end of the full AWK program awk -F"|" ' BEGIN{ printf"H/R|BLDG_CLLI8|OLD_CMPY|HOST_CLLI|HOST_SW_TY|SWITCH_NAME|" printf"SW_TYPE|SW_CLLI|SWITCH_ADDRESS|CITY|ST-ZIP\n" } FILENAME=="site1.err" { serr[$0]=$0 } FILENAME=="/home/bob/X25/DataFiles/ALL_SW_DB.txt" { gsub("\\011","\174") clliswtyp[$7]=$9 } FILENAME=="host_remote" { printf"%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\n",$1,$2,$3,$4,clliswtyp[$4],$5,$6,$7,$8,$9,$10 } END{ for(y in serr) printf"HOST|%s|||||NONE||||\n",y }' site1.err /home/bob/X25/DataFiles/ALL_SW_DB.txt host_remote >host_remote1
    – BobEng
    Sep 12, 2015 at 18:30
  • This reads a little better awk -F"|" ' BEGIN{ printf"H/R|BLDG_CLLI8|OLD_CMPY|HOST_CLLI|HOST_SW_TY|SWITCH_NAME|" printf"SW_TYPE|SW_CLLI|SWITCH_ADDRESS|CITY|ST-ZIP\n" } FILENAME=="site1.err" { serr[$0]=$0 } FILENAME=="/home/bob/X25/DataFiles/ALL_SW_DB.txt" { gsub("\\011","\174") clliswtyp[$7]=$9 } FILENAME=="host_remote" { printf"%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\n",$1,$2,$3,$4,clliswtyp[$4],$5,$6,$7,$8,$9,$10 } END{ for(y in serr) printf"HOST|%s|||||NONE||||\n",y }' site1.err /home/bob/X25/DataFiles/ALL_SW_DB.txt host_remote >host_remote1
    – BobEng
    Sep 12, 2015 at 19:20
  • awk -F"|" ' BEGIN{ printf"H/R|BLDG_CLLI8|OLD_CMPY|HOST_CLLI|HOST_SW_TY|SWITCH_NAME|" printf"SW_TYPE|SW_CLLI|SWITCH_ADDRESS|CITY|ST-ZIP\n" } FILENAME=="site1.err" { serr[$0]=$0 } FILENAME=="/home/bob/X25/DataFiles/ALL_SW_DB.txt" { gsub("\\011","\174") clliswtyp[$7]=$9 } FILENAME=="host_remote" { printf"%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\n",$1,$2,$3,$4,clliswtyp[$4],$5,$6,$7,$8,$9,$10 } END{ for(y in serr) printf"HOST|%s|||||NONE||||\n",y }' site1.err /home/bob/X25/DataFiles/ALL_SW_DB.txt host_remote >host_remote1 Sorry, this one does.
    – BobEng
    Sep 12, 2015 at 19:22
0

This is a simpler example. I want to replace the 2 instances of /home/Bob/X25/DataFiles with the export of X25DATAF (export X25DATAF=/home/Bob/X25/DataFiles) I have in my profile. If I can get it to work in this simple awk, I can modify my real awk.

$ cat test1
test1_data
test1a_data
test1b_data

Bob@Bob-PC ~/X25/Scott/TEST/TEST1

$ cat /home/Bob/X25/DataFiles/test2
test2_data

Bob@Bob-PC ~/X25/Scott/TEST/TEST1

$ cat gettestfiles

File 1test11 has 3 records test1_data, test1a_data and test1b_data and is in same dir that awk is run in.

File test2 only has 1 record test2_data and is in the /home/Bob/X25/DataFiles directory.

awk -F"_" '
FILENAME=="test1" {
  print $1 " " $2
}
FILENAME=="/home/bob/X25/DataFiles/test2" {
print $0
}
END{ }' test1 /home/bob/X25/DataFiles/test2

Bob@Bob-PC ~/X25/Scott/TEST/TEST1

$ gettestfiles
test1 data
test1a data
test1b data
test2_data

Bob@Bob-PC ~/X25/Scott/TEST/TEST1

$
3
  • I can't see a problem. … FILENAME==ARGV[2]{…}' test1 "$X25DATAF/test2" Or what you want to change?
    – Costas
    Sep 14, 2015 at 9:41
  • works perfectly. I was trying to put the $X25DATAF after the filename== instead of using the ARGV. Thank you. How do I give you credit for setting me straight? I just joined this group.
    – BobEng
    Sep 14, 2015 at 14:50
  • But as stated in my answer FNR==NR{…,next}… is more suitable
    – Costas
    Sep 14, 2015 at 18:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .