If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > sed results as a variable

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-21-03, 12:22
Douglas Anderso Douglas Anderso is offline
Registered User
 
Join Date: Jul 2003
Posts: 6
sed results as a variable

I need to scan a file and pull the first line of data. I need to use this result later in the shell script.

Right now I am using:

sed -n 1,1p invoice.tst

This works but I can't seem to redirect the output to a variable. I've tried....

sed -n 1,1p invoice.tst $VAR
and a few other formats....

any thoughts out there?

Thanks,
Doug.
Reply With Quote
  #2 (permalink)  
Old 07-21-03, 12:27
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
head -1 yourFile | read theVariable

or


theVariable=$(head -1 yourFile)

or

...


HTH
Reply With Quote
  #3 (permalink)  
Old 07-21-03, 13:48
Douglas Anderso Douglas Anderso is offline
Registered User
 
Join Date: Jul 2003
Posts: 6
Smile

Works great thanks,

Doug.
Reply With Quote
  #4 (permalink)  
Old 07-22-03, 16:34
chillies chillies is offline
Registered User
 
Join Date: Jul 2003
Location: Edinburgh
Posts: 35
Quote:
Originally posted by Damian Ibbotson
theVariable=$(head -1 yourFile)
alternatively with backquotes

theVariable=`head -1 yourFile`

I've been away from shell scripting for a while and now I'm seeing more constructs like your reply than the backquotes style. Is there any reason to favour one over the other? Does the former not have to spawn a sub-shell or something?

Thanks.
Reply With Quote
  #5 (permalink)  
Old 07-23-03, 05:07
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Quote:
Originally posted by chillies
alternatively with backquotes

theVariable=`head -1 yourFile`

I've been away from shell scripting for a while and now I'm seeing more constructs like your reply than the backquotes style. Is there any reason to favour one over the other? Does the former not have to spawn a sub-shell or something?

Thanks.
I guess it's mainly due to the rise and rise of Linux (and hence Bash). The clear advantage of $() over `` is that it can be nested (e.g. $( $() ) ), whereas backticks cannot.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On