Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

echo & nawk

Posted on 2010-09-09
11
Medium Priority
?
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
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
 
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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

609 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