• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

echo & nawk

Hi

 I’m trying   to find all occurrences of  .   in the PATH environment to
Make sure that PHAT is setup properly
 
I’m running  something like this
 echo $PATH | egrep .: | nawk -v HN=`hostname` '{if ( $1 == ".:" ){print "\""HN"\",\"""GSM-tab1-29""\",\""$1"\",\"" "No" "\",\""$1"\",\"" "Blanc field""\" "} else {print "\""HN"\",\""$1"\",\"Yes\",\""replace me"\" "} }'

But I’m getting nowhere with this it always comes back has  Yes regardless of the setting
 The actual out put of echo $PATH is
bash-3.00# echo $PATH
/usr/sbin:/usr/bin:/sbin:/usr/ccs/bin:/usr/sfw/bin:/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/autosys/bin:/usr/openwi

Or
.:/usr/sbin:/usr/bin:/sbin:/usr/ccs/bin:/usr/sfw/bin:/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/autosys/bin:/usr/openwi
  So I’m not capturing correctly the .:

I need a little Help
Dan  
0
d_asselin
Asked:
d_asselin
  • 5
  • 4
  • 2
1 Solution
 
DalHorinekCommented:
It's not exactly nawk way, but one solution might be this:

using perl:
echo $PATH | perl -ne 'foreach ( split ":" ) { if ( /^\./ ) { print "Found . "; }}';

using bash, sed and grep:
echo $PATH | sed -e 's/:/\n/g' | while read path; do if echo $path | grep -x '^.'; then echo "Dot found in: $path"; fi; done;
0
 
d_asselinAuthor Commented:
 Yes  that works but
I really need the rest of the line also
That  is why I wanted to use nawk

If it can be made to incorporate  the rest of the line it would be ok
But if not it won’t work  
The out put of that command is sent to a Mysql DB in separated field
format
0
 
DalHorinekCommented:
echo $PATH | sed -e 's/:/\n/g' | while read path; do
if echo $path | grep -x '^.'; then
   echo "\"`hostname`\", \"GSM-tab1-29\",\"$path\",\"No\",\"$path\",\"Blank field\"";
else
   echo "\"`hostname`\", \"$path\",\"Yes\",\"replace me\";
fi;
done

I'm not sure if there are all fields, check it out.
0
Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

 
ozoCommented:
Did you mean
if ( substr($1,1,2) == ".:" ){
0
 
d_asselinAuthor Commented:
Hi Ozo
 No I didnt try that substitution
 
   But I really which I could figure out why this line doesn’t work.  I realy need to respect
This formatted out put. There are many fields on this line  this is what the output looks
Look for another line in this script

cat /etc/default/login | egrep TIMEOUT=60 |nawk -v HN=`hostname` '{if ( $1 == "TIMEOUT=60" ){print "\""HN"\",\"""GSM-tab2-30""\",\""",\"""Set the TIMEOUT=60 parameter in /etc/default/login""\",\""$1"\",\"" "No" "\",\""$4"\",\"" "Blanc field""\" "} else {print "\""HN"\",\""$1, $2, $3, $4"\",\"Yes\",\""$5"\" "} }'        

     "servername","GSM-tab2-30",","Set the TIMEOUT=60 parameter in /etc/default/login","TIMEOUT=60","No","","Blanc field"                      


 I’m trying to achive the same result with line below
If the output of echo $PATH   begins with a .  then the answer is No

echo $PATH | egrep .: | nawk -v HN=`hostname` '{if ( $1 == ".:" ){print "\""HN"\",\"""GSM-tab1-29""\",\""$1"\",\"" "No" "\",\""$1"\",\"" "Blanc field""\" "} else {print "\""HN"\",\""$1"\",\"Yes\",\""replace me"\" "} }'

0
 
ozoCommented:
is the ".:" followed by the FS character?  if not $1 will contain other characters after the .:
also, egrep .: will match any character followed by : anywhere in the string
perhaps you meant egrep '\.:'  or fgrep .:
0
 
d_asselinAuthor Commented:
Good Morning Ozo
The command echo $PATH generates the following line.
echo $PATH
/usr/sbin:/usr/bin:/sbin:/usr/ccs/bin:/usr/sfw/bin:/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/autosys/bin:/usr/openwin/bin

  What I have to determine is, does it start with a (.) or
If the line generated by the command starst this way then it has to be flagged has No
 
./usr/sbin:/usr/bin:/sbin:/usr/ccs/bin:/usr/sfw/bin:/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/autosys/bin:/usr/openwin/bin

Dan
0
 
ozoCommented:
if ( $1 ~ /^\./ )
0
 
ozoCommented:
or
nawk -F'[/:]' -v HN=`hostname` '{if ( $1 == "." )
0
 
d_asselinAuthor Commented:
nawk -F'[/:]' -v HN=`hostname` '{if ( $1 == "." )

That Works perfectly
  Thank you very much Ozo
this is the complete line

echo $PATH | egrep .: | nawk  -F'[/:]' -v HN=`hostname` '{if ( $1 == "." ){print "\""HN"\",\"""GSM-tab1-29""\",\""$1"\",\"" "No" "\",\""Dot in PATH $1"\",\"" """\" "} else {print "\""HN"\",\""$1"\",\"Yes\",\""PATH is Complient\" "} }'
and This is the output

"servername","GSM-tab1-29","Dot in PATH .","No",".","Blanc field"


Dan
0
 
d_asselinAuthor Commented:
And if you lived in Montreal  I’d by you lunch
And if you ever drop by give me a buz I will by you lunch

Dan
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now