You are not logged in.
Pages: 1
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
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
Reconstruct your innerFunc to use proper sed regex?
It's given already (and it is binary).
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
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
@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
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
I havn't 'replacedtext'. Rather I have another - inner pipe (innerFunc in first topic post).
"I exist" is the best myth I know..
Offline
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
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
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
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...
Offline
Have added applying context decription to the first topic message.
"I exist" is the best myth I know..
Offline
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
Pages: 1