LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-21-2006, 03:56 PM   #1
NNP
Member
 
Registered: Nov 2004
Distribution: Debian/Ubuntu
Posts: 156

Rep: Reputation: 30
sed -n question


Hey,
I have a script that does something similar to the following
Code:
while [ 1 ]; do echo 'something'; sleep 1; done | sed -n 's/something/somethingElse/p' > outputFile
For some reason though the resulting file is empty. If i remove the redirect e.g

Code:
while [ 1 ]; do echo 'something'; sleep 1; done | sed -n 's/something/somethingElse/p'
(I have trivialised the example here, I have a valid reason for using -n but the above example better illustrates the issue)

It outputs fine to the terminal but piping the result to another script reading on stdin or to a file doesn't work. Is there something I'm missing about sed -n or is it something other problem?

I am using the bash shell btw.

Thanks,
nnp

Last edited by NNP; 11-21-2006 at 03:57 PM.
 
Old 11-21-2006, 04:19 PM   #2
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,832

Rep: Reputation: 108Reputation: 108
Hi,

Code:
~$ while [ 1 ]; do echo 'something'; sleep 1; done | sed -n 's/something/somethingElse/p'
somethingElse
somethingElse
somethingElse
somethingElse
somethingElse
somethingElse
somethingElse
somethingElse
somethingElse
somethingElse
somethingElse
This is what I got on my penguin.

I think this is what you want.

Check your sed ???

Happy Penguins!!
 
Old 11-21-2006, 04:29 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
With output is sent to the terminal, the IO is line buffered, but when output is re-directed to a file, the IO is buffered differently - nothing will be written to disk until some buffer is filled. For me it appears to be 4096 bytes.

I don't know exactly where this number comes from - perhaps the shell itself, or perhaps the default for the C streams library. If someone can give the definitive answer, I'd like to know.

Anyhow, with the output string "somethingElse\n", and a sleep time of 1 second per output, it'll take almost 5 minutes before you get any output.
 
Old 11-21-2006, 04:38 PM   #4
NNP
Member
 
Registered: Nov 2004
Distribution: Debian/Ubuntu
Posts: 156

Original Poster
Rep: Reputation: 30
Thank you both for your answers.
@kaz2100: hehe, yup, thats the code fragment that works. Its the other one that doesnt

The main reason I am doing this is so I can pipe the results of the bash commands into a python script for parsing.

e.g to test my network

Code:
nmap -sS -p $PORTS $IPRANGE | grep -B 2 " open " | sed -n 's/\(Interesting ports on \)\(\([0-9]*\.\)\{3\}[0-9]\{1,3\}\):/\2/p' | ./somePythonScript.py
All the above should do is feed IP addresses to the python script as it scans. (Has to be done concurrently)

The python script would look somewhat similar to the following

Code:
#!/usr/bin/env python

import os, sys
ipaddress = sys.stdin.readline()

while ipaddress and ipaddress != '':
    print '[+] ip ' + ipaddress
# Do some stuff with the IP address here
    ipaddress = sys.stdin.readline()
But the python script doesn't appear to recieve the result from the sed command until the nmap scan is finished. Is this a result of the same buffering you mentioned earlier? I would have thought piping the output would avoid this kind of buffering. If not, is there any way to avoid the buffering or to implement what I'm trying to do?

edit: ok, curiously enough, there again seems to be some sort of buffering on the pipe. If I leave the above command run long enough e.g ensure nmap generates a lot of data it eventually works. I guess the buffering is something I'm going to have to live with.

Last edited by NNP; 11-21-2006 at 04:53 PM.
 
Old 11-21-2006, 04:48 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
use the -u option on the sed command, this activates unbuffered mode.
 
Old 11-21-2006, 04:59 PM   #6
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,832

Rep: Reputation: 108Reputation: 108
Hi,

Aiya....

I was careless......
 
Old 11-21-2006, 05:04 PM   #7
NNP
Member
 
Registered: Nov 2004
Distribution: Debian/Ubuntu
Posts: 156

Original Poster
Rep: Reputation: 30
Thanks guys.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed question? slack66 Linux - Newbie 7 07-26-2006 06:47 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
SED question zivota Linux - General 8 07-26-2005 06:07 PM
question on sed tifu Linux - Newbie 3 03-18-2005 04:38 PM
sed question zoomzoom Linux - Software 2 10-20-2003 04:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration