You are not logged in.

#1 2011-04-11 09:05:23

student975
Member
From: Russian Federation
Registered: 2011-03-05
Posts: 613

pipe in pipe

Say, I have an input stream (let it be  'cat file.txt'). file.txt consists of:

someText1
context1
someText2
context2
someText3

Also I already have a pipe - let's call it innerFunc - which understands

context1
someText2
context2

and replaces it with something useful - lets' name the result as text2replaced.

I need construct a pipe which, having file.txt as input, will produce

someText1
text2replaced
someText3

How to?

//-----------------------------------------------------------------------------------

The applying context.

Claws-Mail can not process digitally signed messages (at my use case), while gpg does. I'd want to insert a piping as Claws-Mail action.

context1
someText2
context2

is just a pgp-block, while the whole text is a mail message (in fact - stream), and 'gpg -d' is an inner pipe function.

At any case I suppose  these details don't influence to the task solving: context1, context2 and 'gpg -d'  can be just constants in bash scipt.

Last edited by student975 (2011-04-15 08:26:23)


"I exist" is the best myth I know..

Offline

#2 2011-04-11 09:21:07

Awebb
Member
Registered: 2010-05-06
Posts: 6,688

Re: pipe in pipe

Reconstruct your innerFunc to use proper sed regex?

But why all the piping? How about a bash script? You wouldn't need to pipe at all, working with variables/arrays in bash.

Offline

#3 2011-04-11 09:31:49

student975
Member
From: Russian Federation
Registered: 2011-03-05
Posts: 613

Re: pipe in pipe

Awebb wrote:

Reconstruct your innerFunc to use proper sed regex?

It's given already (and it is binary).

Awebb wrote:

But why all the piping? How about a bash script? You wouldn't need to pipe at all, working with variables/arrays in bash.

It can be a script, but it must be insertable in pipe (by use case). I hope it is possible to write such scipt, but not having bash knowledge it is rather difficult to write the script. So, have started the topic.


"I exist" is the best myth I know..

Offline

#4 2011-04-11 18:44:37

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: pipe in pipe

Something like this:

#!/bin/bash

inputFile="file.txt"

## Find the line number of the first context
firstContextLine=$(sed -n '/context1/=' $inputFile)

## Print the file file up until that point
amountOfLinesToPrint=$(($firstContextLine - 1))
head -$amountOfLinesToPrint $inputFile

## Print the new string
printf "My new string\n"

## Find the line number of context two
secondContextLine=$(sed -n '/context2/=' $inputFile)

## From this calculate the remaining lines
totalLines=$(wc -l $inputFile | sed -e 's/^\([0-9]*\).*/\1/')
remainingLines=$(($totalLines - $secondContextLine))
tail -$remainingLines $inputFile

It's a bit long winded but it does find your context and replace the string between them. Your example is a little vague though...

Last edited by BaconPie (2011-04-11 18:45:44)

Offline

#5 2011-04-11 19:54:32

student975
Member
From: Russian Federation
Registered: 2011-03-05
Posts: 613

Re: pipe in pipe

@BaconPie

As I have mentioned, I have input stream. 'cat file.txt' is just an example to explain the stream content.


"I exist" is the best myth I know..

Offline

#6 2011-04-11 20:52:42

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: pipe in pipe

How about this:

cat file.txt | tr "\n" "%%" | sed -e 's/context1.*context2/replacedtext/g' | tr "%%" "\n"

I used the percent as a delimiter, assuming your stream doesn't contain any percentages then this should work. Otherwise, change the percent symbol for something else. I originally had the bell (\a) but if you had it switched on then that would get annoying...

Offline

#7 2011-04-11 21:02:27

student975
Member
From: Russian Federation
Registered: 2011-03-05
Posts: 613

Re: pipe in pipe

I havn't 'replacedtext'. Rather I have another - inner pipe (innerFunc in first topic post).


"I exist" is the best myth I know..

Offline

#8 2011-04-11 21:14:40

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,324

Re: pipe in pipe

man awk ???


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#9 2011-04-11 21:34:42

BaconPie
Member
Registered: 2010-08-11
Posts: 209

Re: pipe in pipe

student975 wrote:

I havn't 'replacedtext'. Rather I have another - inner pipe (innerFunc in first topic post).

So, just:

cat file.txt | innerFunc

I think I've misunderstood. Sorry.

Offline

#10 2011-04-11 21:43:03

student975
Member
From: Russian Federation
Registered: 2011-03-05
Posts: 613

Re: pipe in pipe

BaconPie wrote:

So, just:

cat file.txt | innerFunc

innerFunc just eats (is not transparent for) prefix and suffix of known block.

Last edited by student975 (2011-04-11 21:43:20)


"I exist" is the best myth I know..

Offline

#11 2011-04-12 02:03:17

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: pipe in pipe

I'm closing this on the grounds that it looks oddly generic and without any context - suggesting it is a homework assignment.

https://wiki.archlinux.org/index.php/Fo … e#Homework

If you want to provide the details as to why you want this problem solved, the mods will consider re-opening the thread.

## Reopened with context supplied in first post...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#12 2011-04-15 08:29:06

student975
Member
From: Russian Federation
Registered: 2011-03-05
Posts: 613

Re: pipe in pipe

Have added applying context decription to the first topic message.


"I exist" is the best myth I know..

Offline

#13 2011-05-05 17:29:32

listdata
Member
Registered: 2008-12-23
Posts: 102
Website

Re: pipe in pipe

student975 wrote:

It can be a script, but it must be insertable in pipe (by use case). I hope it is possible to write such scipt, but not having bash knowledge it is rather difficult to write the script. So, have started the topic.

Shell scripts can act like pipes, too. All you have to do is capture the STDIN. Since your use case is simple (all STDIN will be processed at once), you can just write a timer in your script to check for any STDIN.

This is a working zsh code snippet (that I adopted from an online tutorial somewhere...) to capture the STDIN into a variable, stdinFiles:

...
# process STDIN, if any (timeout after 0.1 second)
stdinFiles=""
while read -s -t 0.1 stdinText; do
    stdinFiles="$stdinFiles '$stdinText'"
done

# if there was STDIN, process it (convert them to $@ arguments); otherwise, just interpret the
# arguments
if [[ -n $stdinFiles ]]; then
    eval set -- "$@" $stdinFiles
fi
...

I use the above logic to capture the STDOUT from a list of files "ls *.mp3", like this:

ls *.mp3 | myScript

The "read" command above is a zsh-specific builtin function (see "man zshbuiltins"). Bash has its own version with its own options, so look up the docs if needed.

Offline

Board footer

Powered by FluxBB