|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
Quote:
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 |
|
#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? |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Search file for a string then get the next string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|