Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a better way do this?

Status
Not open for further replies.

vuakhobo

Technical User
Apr 22, 2004
41
0
0
US
My grep commands are working but is there a better way to do this?

inputfile has
start3 start start123
start3 startuser changeme
start3 hello donut

start-id=$(grep start inputfile|grep -v startuser|grep -v hello|awk '{print $2}')

start-pass=$(grep start inputfile|grep -v startuser|grep -v hello|awk '{print $3}')


startuser-id=$(grep startuser inputfile|grep -v hello|awk '{print $2}')

startuser-pass=$(grep startuser inputfile|grep -v hello|awk '{print $3}')
 
awk can do pattern matching, so you shouldn't need grep at all, e.g.

Code:
start_id=$(awk '$2 !~ /startuser|hello/ { print $2 }')

startuser_pass=$(awk '$2 == "startuser" { print $3 }')

Notice that I'v echanged the - to a _ in the variable name; most shells don't support hyphens in variable names.

Annihilannic.
 
What about this ?
Code:
eval $(awk '/hello/{next}
/startuser/{printf "startuser_id=%s startuser_pass=%s ",$2,$3;next}
/start/{printf "start_id=%s start_pass=%s ",$2,$3}
' inputfile)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I found another solution
grep -w will work as well

start-id =$(grep -w start inputfile|awk '{print $1}')
 
Did you try my single command solution ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top