sed to read


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed to read
# 1  
Old 12-01-2009
sed to read

Hi all,
is it possible to pipe the output of sed to read?
I'm using bash I tried

Code:
sed -n '2p' file.dat | read var1 var2
echo $var1 $var2

without success

Code:
sed -n '2p' file.dat

gives
Code:
  5.43518733E+07          4.05545247E+13

as expected

thank you,
Sarah
# 2  
Old 12-01-2009
bash shell
Code:
exec 4<"file"
read line<&4; read line<&4
exec 4>&-
set -- $line
var1=$1
var2=$2
echo "$var1 $var2"

# 3  
Old 12-01-2009
Or

Code:
read var1 var2 junk << !
$(sed -n '2p' file1)
!

# 4  
Old 12-01-2009
thank you very much.

the solution

Code:
read var1 var2 junk << !
$(sed -n '2p' file1)
!

it works fine alone
I have it in a for loop and I get the error

Code:
line 54: syntax error: unexpected end of file

as soon as enter that code

Last edited by f_o_555; 12-01-2009 at 07:16 AM..
# 5  
Old 12-01-2009
Without seeing your code, I would have no idea why...
# 6  
Old 12-01-2009
Code:
sed -n '2p' file.dat | read var1 var2
echo $var1 $var2

will work as you would expect in ksh, but not in the other shells.

Otherwise, alternatives to Scottn' s solution :
Code:
sed -n '2p' file.dat | { read var1 var2
echo $var1 $var2 ;}

-or if your platform supports it
Code:
read var1 var2 < <(sed -n '2p' file.dat)
echo $var1 $var2

-or if your shell supports it:
Code:
read var1 var2 <<< $(sed -n '2p' file.dat)
echo $var1 $var2

The latter is small variation to scottn's code and work in bash and ksh93
# 7  
Old 12-01-2009
Quote:
Originally Posted by scottn
Without seeing your code, I would have no idea why...
this is the code

Code:
#!/bin/bash
TOT=`wc -l < $1`
PART=$(($TOT / 16))
echo $1 $TOT $PART

echo "FAKE 1" > xxx
echo "FAKE 1" >> xxx
echo "FAKE 1" >> xxx

for (( j=0;j<$PART;j++)); do
    for (( i=1;i<=15;i++)); do
         #skip header 1 line
         start=$(echo "scale=10; $j * 16 + $i + 1 ;" | bc)
         echo $start
    done
    read var1 var2 junk << !
    $(sed -n '2p' fit_par.dat)
    !
    echo $var1 $var2
done



---------- Post updated at 07:23 AM ---------- Previous update was at 07:20 AM ----------

Quote:
Originally Posted by Scrutinizer
Code:
sed -n '2p' file.dat | read var1 var2
echo $var1 $var2

will work as you would expect in ksh, but not in the other shells.

Otherwise, alternatives to Scottn' s solution :
Code:
sed -n '2p' file.dat | { read var1 var2
echo $var1 $var2 ;}

-or if your platform supports it
Code:
read var1 var2 < <(sed -n '2p' file.dat)
echo $var1 $var2

-or if your shell supports it:
Code:
read var1 var2 <<< $(sed -n '2p' file.dat)
echo $var1 $var2

The latter is small variation to scottn's code and work in bash and ksh93
I chose the last one
Thank you all
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed read X lines after match

I want to do something like sed -n '/PATTERN/,+10p' and get the ten lines following PATTERN. However, this throws an "expected context address" with the sed that comes with OSX Lion. If that + is a GNUism, can I do this, or do I have to find another tool? (2 Replies)
Discussion started by: jnojr
2 Replies

2. Shell Programming and Scripting

Having sed to read lines of a file with the use of a variable..Possible?

This i will print the second line of a file sed -n '2p' test2 The use of a variable is impossible here. a=1 while ; do line=`sed -n '$a p' test2` # do some things here with the line variable a=`expr $a + 1` done But the uotput of sed command is 'p'..... What can i do to use a variable... (2 Replies)
Discussion started by: hakermania
2 Replies

3. Shell Programming and Scripting

read file-print lines in sed

Hello! Im trying to read file contents. Then, print out every line that has "/bens/here" in the file that was read. cat /my/file.now | sed '/bens/here/p' I keep getting the error asking if I need to predeclare sed? What does predeclaring sed mean? Thanks! Ben (2 Replies)
Discussion started by: bigben1220
2 Replies

4. Shell Programming and Scripting

sed search and read the RHS information: Linux 2.6.9-89

Hi, I have a config_file.cfg with the content: FILE_ID_1=1 FILE_FTP_ID_1=<FTP_SERVER1.COM> .... FILE_ID_2=2 FILE_FTP_ID_2=<FTP_SERVER2.COM> .... so on for 28 times. As you might have guessed it; the script I have to write is to read this config file and get the FTP server... (3 Replies)
Discussion started by: dips_ag
3 Replies

5. Shell Programming and Scripting

Read a file content with awk and sed

Hello , I have huge file with below content. I need to read the numeric values with in the paranthesis after = sign. Please help me with awk and sed script for it. 11.10.2009 04:02:47 Customer login not found: identifier=(0748502889) prefix=(TEL) serviceCode=(). 11.10.2009 04:03:12... (13 Replies)
Discussion started by: rmv
13 Replies

6. Shell Programming and Scripting

need sed command to read a path and set to variable

I have a variable called PATH that contains a path example: /Users/rtipton/Desktop/testusers/test I need a sed command to set a variable called USER to the last directory name in that path PATH="/Users/rtipton/Desktop/testusers/test" and from that PATH i need USER to = test I know sed... (4 Replies)
Discussion started by: tret
4 Replies

7. Shell Programming and Scripting

SED command for read and write to different files

Hi I need some help on SED command I am writing a shell script which does the following: 1. Read one line at a time from a file abc.txt which has millions of lines 2. Prefix each line read with some text " 3. Post fix each line read with a quote " 4. Write the new modified... (11 Replies)
Discussion started by: gaurav_1711
11 Replies

8. Shell Programming and Scripting

Using Sed to read one line only

I have a bash script which produces the desired output (mostly). tempTxt=`sed -n '79,/^$/p' <sourceFile.txt | cut -c 13-19` What this produces is the text from the 79th line of the source file and cuts the text to the 13th through the 19th chars. Then the script does the same thing... (4 Replies)
Discussion started by: roninuta
4 Replies

9. Shell Programming and Scripting

SED and Read Help Needed

Not sure why my sed script is not working..need extra eyes..Any help would be aprreciated. I want to add this line into the 1rst line of the $EJECT FILE sed '/">>>>>>>>>>>>> TAPES TO BE EJECTED /a\<<<<<<<<<<<<<<<<<"'> $EJECT This is the error message I get: + sed /">>>>>>>>>>>>> TAPES TO... (1 Reply)
Discussion started by: gzs553
1 Replies

10. Shell Programming and Scripting

sed to read and write to very same file

This is likely to be a dumb one. How can I use sed to substitute string occurances having it read from an input file and write to this very same file ? I have a file with lots of occurances of '2006', I want to change it to '2007', but I'd like these changes to be saved on the input file. ... (5 Replies)
Discussion started by: 435 Gavea
5 Replies
Login or Register to Ask a Question