How to return program output intermittent into file?

How to return program output intermittent into file?

Post by J. S. Jense » Wed, 25 Nov 1998 04:00:00



I am attempting to pipe input between two range patterns of a file to a
process, then replace that text in the range pattern to the output of
the called process.

awk does not exactly work because the output of the process will attach
to the awk's stdout and output its data after the awk output.  Here's
what I want to do (let's just assume it is `cksum' as the external
process):

$cat foo
blah
blah2
%%{
    cksum data area
    data area line 2
%%}
blah5
...
blah799

the output should be

blah
blah2
3677025507 41
blah5
...

in awk, I could do the following:

/^%%{/,/^%%}/ {
    if ( ! (( $0 ~ /^%%{/ ) || ( $0 ~ /^%%}/ )) ) print |"cksum"

Quote:}

however, the cksum output takes in all disparate matching data lines and
outputs the data AFTER the awk output of the other lines.  (note that a
few flags must be set to make awk NOT output the range pattern lines)

I even attempted modifying GNU sed code and adding a function that would
do what vi's `!' does, but attaching and syncing stdin/out of another
process into stdin/out of the current process is not trivial and it was
core dumping all over the place.

I could use an ex script:

ex - foo <<EOF
/%%{/,/%%}/!cksum
w !cat
EOF

but this writes the ex output out in the stream.

What does one do?  None of these work.

--
J. S. Jensen

http://www.paramin.com

 
 
 

How to return program output intermittent into file?

Post by Icarus Spar » Tue, 01 Dec 1998 04:00:00



Quote:>I am attempting to pipe input between two range patterns of a file to a
>process, then replace that text in the range pattern to the output of
>the called process.

>awk does not exactly work because the output of the process will attach
>to the awk's stdout and output its data after the awk output.  Here's
>what I want to do (let's just assume it is `cksum' as the external
>process):

>$cat foo
>blah
>blah2
>%%{
>    cksum data area
>    data area line 2
>%%}
>blah5
>...
>blah799

>the output should be

>blah
>blah2
>3677025507 41
>blah5

Since this is comp.unix.shell, we use

#!/bin/ksh
while IFS="" read -r line
do
        if [[ "$line" == "%%{" ]] ;then break ; fi
        print -r -- "$line"
done
while IFS="" read -r line
do
        if [[ "$line" == "}%%" ]] ;then break ; fi
        print -r -- "$line"
done | cksum
cat

The first 'while .. read .. do .. done' loop reads the initial part of the
file, the second one passes the data to the cksum program and the final 'cat'
handles the last part.

Your 'awk' solution may well work better if you explicitly 'close' the pipe
to 'chsum' (requires nawk/gawk etc rather than traditional awk) when you
see the '}%%' line. Awk does not know if there is more than one section
in your file to be checksummed until it reaches the end of the file.

Icarus

 
 
 

1. Capturing return code while redirecting output to screen as well as file

Hi,

I need to display output of a command on the screen, as well as
redirect the output to a file.  I also need to capture the return code
of the command.

I tried:
<command> | tee <file_name>

Using this, I can achieve the redirection that I want, but I lose the
return code of "command".  The return code will now be that of "tee"
command.

Any clues on how I can meet all 3 requirements?  It needs to work for
the Bourne shell.

Thanks,
Pranav

2. FS: NeXTStep 3.2 and Solaris 2.4

3. Help: Returning a return code to a program

4. Solaris x86 Configuring PPP - Digest

5. VIA 8237 DMA

6. Program: Output protokoll to a serial interface and to a file ?

7. Recommend a good CDR/CDRW backup program?

8. C Programming Help Needed With text file data output..

9. Need a shell program to print return value of a program

10. How to write a C-shell program to print return value of a program

11. Program: Output protokoll to a serial interface and to a file ?

12. :C-shell program that prints return value of a program