LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-28-2019, 10:36 PM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Rep: Reputation: 177Reputation: 177
How to sed a continuous stream


I am able to get a stream of log information from a firewall device using:
Code:
nc -lu -p 514 | tee mylogfile
I can redirect that output to a file, as shown, without problem. This stream has no newlines, but each new "event" begins with "<13". What I want to do is to 'sed' that to insert newlines so I can do some downstream processing. if I cat the redirected file to a sed command:
Code:
cat mylogfile | sed -e $'s/<13.> */\\n&/g'
it works fine. What is not working is:
Code:
nc -lu -p 514 | sed -e $'s/<13.> */\\n&/g'
The command above produces absolutely nothing even after letting it run for 7 hours, whereas the nc command in the first example produces a continuous stream of data.

I've found other postings on the Internet which suggest using unbuffer and stdbuf (e.g. unbuffer nc -lu -p 514), neither of those ideas produce any output either.

I'm sure this must be related to the fact that there are no newlines in the stream, but even if the output buffer gets full, nothing comes out of the sed.

Is there a way I can do this?

Last edited by mfoley; 03-28-2019 at 10:43 PM.
 
Old 03-29-2019, 02:34 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Code:
$ man sed
(...)
       -u, --unbuffered

              load  minimal  amounts of data from the input files and flush the output buffers more
              often
 
Old 03-31-2019, 01:05 AM   #3
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Original Poster
Rep: Reputation: 177Reputation: 177
Quote:
Originally Posted by ondoho View Post
Code:
$ man sed
(...)
       -u, --unbuffered

              load  minimal  amounts of data from the input files and flush the output buffers more often
That didn't work either. This is the strangest thing I've ever seen. If I just do 'nc -lu -p 514', data streams immediately. If I do 'nc -lu -p 514 | tee myfile', data streams immediately AND goes to myfile. I can then post-process that file with 'sed -e $'s/<13.> */\\n&/g'' to add newlines. However, If I pipe the nc into sed ... ABSOLUTELY NOTHING! I've used your -u option which has been running for about 5 hours -- no output. I've tried using unbuffer and stdbuf, nothing seems to work.

Need more ideas on a solution!
 
Old 03-31-2019, 01:26 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Just some guesses:

Have you tried a different pattern? You are looking to remove octal 15 aka decimal 13 aka Carriage Return?

Code:
stdbuf -o0 nc -lu -p 514 | sed --unbuffered -e 's/<\o015.> */\\n&/g'

# or

unbuffer nc -lu -p 514 | sed --unbuffered -e 's/<\o015.> */\\n&/g'
But wouldn't sed be looking for a line break or a null, depending on your settings, anyway?

If you are aiming to replace a single character, try with tr instead.

Code:
nc -lu -p 514 | tr '\015' '\n'
 
Old 03-31-2019, 02:21 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Port 514 is syslog.
Configure your syslogd to listen to it. Maybe configure it to send it to a dedicated file. This file can be processed with sed, grep, awk, etc.
 
Old 03-31-2019, 09:02 AM   #6
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
If there are no new lines, then sed will wait till the stream is closed. e.g.
Code:
for j in {1..2}; do for i in {1..10}; do echo -n "<13i= $i j= $j"; sleep 1; done; done | sed 's/i/I/g'
but awk works if you use the record separator variable.
Code:
for j in {1..2}; do for i in {1..10}; do echo -n "<13i= $i j= $j"; sleep 1; done; done | awk '1{print}' RS="<13"
 
1 members found this post helpful.
Old 03-31-2019, 11:46 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
the sed [expression] is incorrect (I think). Furthermore, if there are no newlines in the stream sed probably cannot handle it, because it wanted to read a full line (=needs to read the full stream).
So better to do/use something else.
For example in awk you can use "<13.>" as input record separator and newline for output record separator.
 
1 members found this post helpful.
Old 03-31-2019, 12:40 PM   #8
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,555

Original Poster
Rep: Reputation: 177Reputation: 177
Quote:
Originally Posted by MadeInGermany View Post
Port 514 is syslog.
Configure your syslogd to listen to it. Maybe configure it to send it to a dedicated file. This file can be processed with sed, grep, awk, etc.
Yes, this is the syslog port. At the moment, I'm just trying to see what's coming in and what I can filter out or keep. I'll need to do some research on exactly how to get syslogd to listen and send to another file. That will possible be the next step. Or, maybe I don't need to even bother with syslogd if I can do what I want.
Quote:
Originally Posted by allend View Post
If there are no new lines, then sed will wait till the stream is closed. e.g.
Code:
for j in {1..2}; do for i in {1..10}; do echo -n "<13i= $i j= $j"; sleep 1; done; done | sed 's/i/I/g'
but awk works if you use the record separator variable.
Code:
for j in {1..2}; do for i in {1..10}; do echo -n "<13i= $i j= $j"; sleep 1; done; done | awk '1{print}' RS="<13"
Aswsome! Yes! awk works:
Code:
nc -lu -p 514 | awk 'BEGIN{RS="<13"}{print "<13" $0}'
Output shows immediately and I can either send it to a file or pipe to a script to futher process. I'll experiment with this a bit, but I think this might do the trick!

Last edited by mfoley; 03-31-2019 at 12:41 PM.
 
  


Reply

Tags
buffer flush, sed



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
LXer: Zuul: Proven open-source continuous integration/continuous delivery LXer Syndicated Linux News 0 05-23-2018 08:31 PM
LXer: Perfecting DevOps Continuous Integration and Continuous Delivery with Kayenta LXer Syndicated Linux News 0 04-11-2018 12:42 AM
Using Tar utility to untar(extract) from a continuous input stream sajalmalhotra Linux - General 4 01-29-2014 02:12 AM
How to record a stream and start a new outputXXX.avi/mp3 for each new stream title ? frenchn00b Linux - General 4 08-04-2008 05:40 AM
Howto transcode & relay a MPEG stream to a WMV stream?? crazyivan Linux - Software 0 06-15-2007 03:18 AM

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

All times are GMT -5. The time now is 02:48 PM.

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