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:
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  
Old November 4th, 2004, 02:47 AM
IcedEarth IcedEarth is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 IcedEarth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old November 4th, 2004, 09:21 AM
andyb1ack andyb1ack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 60 andyb1ack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 14 sec
Reputation Power: 4
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?

Reply With Quote
  #3  
Old November 4th, 2004, 09:24 AM
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
try backticks
Code:
f=`grep PATH directory.file|cut -d= -f2`

Reply With Quote
  #4  
Old November 5th, 2004, 12:57 AM
IcedEarth IcedEarth is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 IcedEarth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.)

Reply With Quote
  #5  
Old November 5th, 2004, 05:48 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
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

Reply With Quote
  #6  
Old November 5th, 2004, 06:29 AM
IcedEarth IcedEarth is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 IcedEarth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #7  
Old November 5th, 2004, 06:59 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
yes a typo
sed -n 's/and-the-the-rest....
dont forget SP is a SPACE followed by a TAB char LITTERALLY
MUST WORK!

Reply With Quote
  #8  
Old November 8th, 2004, 01:48 AM
IcedEarth IcedEarth is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 13 IcedEarth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #9  
Old November 8th, 2004, 03:02 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > sed/grep/have no idea


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 | 
  
 





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