Solved

echo & nawk

Posted on 2010-09-09
11
191 Views
Last Modified: 2012-05-10
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
Comment
Question by:d_asselin
  • 5
  • 4
  • 2
11 Comments
 
LVL 6

Expert Comment

by:DalHorinek
ID: 33637897
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
 

Author Comment

by:d_asselin
ID: 33638378
 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
 
LVL 6

Expert Comment

by:DalHorinek
ID: 33638494
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
 
LVL 84

Expert Comment

by:ozo
ID: 33638504
Did you mean
if ( substr($1,1,2) == ".:" ){
0
 

Author Comment

by:d_asselin
ID: 33639275
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 84

Expert Comment

by:ozo
ID: 33642617
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
 

Author Comment

by:d_asselin
ID: 33645644
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
 
LVL 84

Expert Comment

by:ozo
ID: 33647321
if ( $1 ~ /^\./ )
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 33647369
or
nawk -F'[/:]' -v HN=`hostname` '{if ( $1 == "." )
0
 

Author Comment

by:d_asselin
ID: 33647501
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
 

Author Comment

by:d_asselin
ID: 33647533
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now