SunQuest
           UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old July 21st, 2004, 02:09 PM
revilord revilord is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 4 revilord User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Search file for a string then get the next string

I am trying to search a file for a string and then setting a variable to the next word. It is a preferences file so, for example, I search for the string 'FONT' and the next word is 'TIMES'. I am using a ksh script. here is an example but the variable doesn't set. There is probably a better way but I wouldn't know and the output is "Current selection is : "

#!/bin/ksh
#
#
set nohup
set noclobber notify
#
echo "start"
set VERSA=grep VERSA $HOME/.vcomprc | awk '{print $2}'
echo "end"
echo "Current selection is $VERSA : "

#tput clear

echo " "
if (test ! -e $VERSA/versa) then
echo "Versa doesn't exist"

fi
exit 0


the .vcomprc file would look like this
VERSA /usr/mydir
EDITOR vi
PRNTR HP
PSOUT /output

Reply With Quote
  #2  
Old July 22nd, 2004, 12:57 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,307 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 28 m 57 sec
Reputation Power: 48
Here is a partial answer: assuming the file is made of space delimited string values
Code:
#!/bin/ksh
# $1 = file to search
# $2 string to look for
IFS=" "
grep "$1" $2 | read one two three four
echo $two
exit

Reply With Quote
  #3  
Old July 23rd, 2004, 09:02 AM
revilord revilord is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 4 revilord User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the response but it didn't work. but the problem was the set keyword. Once I removed set from

set VERSA=grep VERSA $HOME/.vcomprc | awk '{print $2}'

The program worked.

I am starting to dislike UNIX shell scripts.

Reply With Quote
  #4  
Old July 23rd, 2004, 10:04 AM
revilord revilord is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 4 revilord User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by revilord
Thanks for the response but it didn't work. but the problem was the set keyword. Once I removed set from

set VERSA=grep VERSA $HOME/.vcomprc | awk '{print $2}'

The program worked.

I am starting to dislike UNIX shell scripts.


spoke to soon. This only works for the first line. If I try to grep the second line awk still retruns the second string in the first line. I give up.

i.e.
...
EDITOR=`grep EDITOR $HOME/.vcomprc | awk '{print $2}'`
...

will return
/usr/mydir

where .vcomprc
VERSA /usr/mydir
EDITOR vi
PRNTR HP
PSOUT /output

Reply With Quote
  #5  
Old July 23rd, 2004, 01:41 PM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 5
That should have worked. So should Jim's code. From the comand line, try:
grep EDITOR $HOME/.vcomprc

Did that select the one appropriate line? Next from the command line, try:
grep EDITOR $HOME/.vcomprc | awk '{print $2'}

Did that display the correct value?

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Search file for a string then get the next string


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

IBM developerWorks




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway