|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
sed/grep/have no idea
have been trying to find a solution where I can set paths in one file i.e. :
Directory.file [Directory] PATH=/u/data/tape etc. [EOF] for my customers so that all they have to do is edit the text following the = and then I read that out with a script with several lines. For instance: Script.file f=grep PATH directory.file|cut -d= -f2 etc.. [EOF] problem is $f isnt then = /u/data/tape... I need to make this variable in my script tho. I first of all dont know which would be better grep or sed ( I am guessing you guys are gonna say sed) and secondly have no idea how to go about it since someone ate our sed and awk booklet. the .files and [EOF]s are just for better understanding. |
|
#2
|
|||
|
|||
|
VARIABLE_NAME=`command`
These are back tics (to the left of number 1 on the main keyboard) e.g. f=`grep PATH directory.file|cut -d= -f2` What will happen if PATH appears more than once in directory.file though? |
|
#3
|
|||
|
|||
|
try backticks
Code:
f=`grep PATH directory.file|cut -d= -f2` |
|
#4
|
|||
|
|||
|
yep jim ...we got it figured out yesterday with your solution too.
Our two problems were no ` and we had left a space between the equals and the backtick. took us three days for that one. Thanks guys..good to see helpful folks out here. (Almost makes me feel like in eve.) |
|
#5
|
|||
|
|||
|
grep|cut
unix can it better sed -n '.*PATH=\(.*\)/\1/p' filename assumed this is a shell and shell have no spaces aroud '=' else sed -n '.*PATH[SP]*=[SP]*\(.*\)/\1/p' filename substitue SP by a SPACE and TAB char if you want put the result in a VAR follow jim's post, i mean XXX=`sed -n '.*PATH[SP]*=[SP]*\(.*\)/\1/p' filename` echo $XXX |
|
#6
|
|||
|
|||
|
XXX=`sed -n '.*PATH[SP]*=[SP]*\(.*\)/\1/p' filename`
echo $XXX guggach we are trying this and it isnt working for us. we always get an error .*PATH[SP]*=[SP]*\(.*\)/\1/p' filename` isnt recognized as a function. |
|
#7
|
|||
|
|||
|
yes a typo
sed -n 's/and-the-the-rest.... dont forget SP is a SPACE followed by a TAB char LITTERALLY MUST WORK! |
|
#8
|
|||
|
|||
|
our working solution
we got lucky friday and got a visit from a unix savvy customer on friday and showed him what we wanted. Inside of two minutes this is what he came up with :
XXX=`sed -n ' s/^PATH[SP]*=[SP]*\(.*\)$/\1/p' <filename echo $xxx not that I understand what everything does but it works. He did it so fast by looking at the example I had carried over from guggach (thanks again) and modifying it slightly. |
|
#9
|
|||
|
|||
|
fyi
it's not really hard
Code:
-n suppress default printing
s substitute cmd
/ trailer
^ at line begin
PATH a string followed by
[ST]* null or any SPACES or TABS, followed by
= a string followed by
[ST]* null or any SPACES or TABS, followed by
\(.*\) astring, null or any chars, remember it
$ at line end
\1 first remembered string
p print only if substitution was successfull
< input redirection
remarques: - the .* before TERM means: don't expect TERM is at line begin, you are reading a file, a space before TERM and the sedcmd no longer works. it's very easy to unintentionally put spaces|tabs editing files. - with the substitute cmd, / (slashes) are a little capricious, if you use|expect a / try an other trailer like , (comma). SUB=/usr/bin/xxx sed -n "s/$SUB//p" will give an error sed -n "s,$SUB,,p" works. - sed is able to open files, the redirect < in this case is superfluos. |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > sed/grep/have no idea |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|