Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

assigning variable in pipeline. Help!!!!

Status
Not open for further replies.

suhaimi

Technical User
Aug 3, 2001
71
US
Hi,
How do I assign a variable in a pipeline? example:
file.txt content
aa
bb
cc

set var=" "
cat file.txt|tail -l|<do something so that var=&quot;cc&quot;>
echo $var
cc

I KNOW this method and DONOT want to use it for certain reasons:
set var=`cat file.txt|tail -l`


The variable assignment MUST BE in PIPELINE

Thanks,
Suhaimi

 
In ksh (or sh probably), you can do:

cat file.txt | tail -1 | read var

As an aside, cou can also shorten the pipleine by using:

tail -1 < file.txt | read var
 

use unix basic commands:

var=`tail -1 filename`
-----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Hi sampsonr,
Thanks for the info. You're the only one who really know what I need. Unfortunately, it does not seem to be working.
Try this simple script

#!/bin/sh
echo &quot;test&quot;|read var
echo $var
It will return nothing! Please help!!!

Suhaimi
 
Try:

cat filename | tail -1 | while read var
do
echo ${var}
done
Regards,
Chuck
 
I don't know why but the sample script you cited works under /bin/ksh, but not under /bin/sh.
 
Hi,
How to do it in C shell??? Anybody???

Suhaimi
 
Hi,
In your original post you say....

cat file.txt|tail -l|<do something so that var=&quot;cc&quot;>

The variable assignment MUST BE in PIPELINE

Why can't you use

set var=`cat file.txt|tail -l`

I mean what comes after the

|<do something so that var=&quot;cc&quot;> | whats next?

maybe if we knew why you needed this we could come up with a solution.


Does The the line have more than one word? If so you need to add parenthesis in CSH.

set var= ( `cat file.txt|tail -l` )

another alternative is to

foreach i ( `cat file.txt|tail -l` )
.
.
.
end

which will in turn set $i to each word of the line. I tried the CSH equivlent


cat file.txt | tail -1 | set var = $<

but $< won't read from a PIPE and insists on reading from the keyboard.


Sorry I couldn't be of any help.

 


Hi,
I should point out what I like to do is

set var=`cat file.txt|tail -l`

Then on subsequent lines

echo $var | .....
echo $var | .....

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top