LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-01-2011, 05:22 PM   #1
texasone
Member
 
Registered: Jun 2008
Location: /home/lorax
Distribution: Debian Testing
Posts: 141

Rep: Reputation: Disabled
sed to strip out chat log time index


I'm using weechat-curses as my irc client and I'm trying to create a way to alert me to when something is said. This is what I have as my script:
Code:
#!/bin/bash 
tail -0f /home/user/.weechat/logs/irc.server.##channel.weechatlog | while read LINE 
do
  echo $LINE
  echo $LINE | festival --tts 
  sleep 0.75 
done
This would be an example of the log output:
Code:
2011-10-01 18:04:53         @nick   aight
The problem is that the "2011-10-01 18:04:53" gets spoken and really draws away from the purpose of using festival. I tried to strip it out with the sed code:
Code:
sed "s/^[0-9]{4}-[0-9]{2}-[0-9]{2}[ ]*[0-9]{2}:[0-9]{2}:[0-9]{2}//"
The problem is that this doesn't give me any output. Anyone have any ideas?
 
Old 10-01-2011, 05:37 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

this can be done easily with sed. Even easier with awk:
Code:
awk '{$1="";$2=""}1'
If you are limited to sed:
Code:
sed 's/[^[:blank:]]* *[^[:blank:]]* *//'
Hope this helps.

[EDIT]
BTW, the reason why your solution is not working is because you have to either use escapes like '\{' and '\}' or you need to invoke 'sed' with the '-r' option to enable extended RegEx:
Code:
sed -r 's/<your RegEx>//'

Last edited by crts; 10-01-2011 at 07:58 PM. Reason: corrected typos
 
Old 10-01-2011, 06:47 PM   #3
texasone
Member
 
Registered: Jun 2008
Location: /home/lorax
Distribution: Debian Testing
Posts: 141

Original Poster
Rep: Reputation: Disabled
Okay, when I use
Code:
tail /path/to/log/file |awk '$1="";$2="";$3""'|xargs echo
It works perfectly, however, it fails within the code of the script
Code:
tail -0f /home/user/.weechat/logs/irc.server.##channel.weechatlog | awk '$1="";$2="";$3""' | while read LINE
is the use of either sed or awk before piping in the "while read LINE" preventing it from reading the line? OR is "tail -0f" causing the issue?

Last edited by texasone; 10-01-2011 at 07:02 PM.
 
Old 10-01-2011, 07:57 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by texasone View Post
Okay, when I use
Code:
tail /path/to/log/file |awk '$1="";$2="";$3""'|xargs echo
It works perfectly, however, it fails within the code of the script
Code:
tail -0f /home/user/.weechat/logs/irc.server.##channel.weechatlog | awk '$1="";$2="";$3""' | while read LINE
is the use of either sed or awk before piping in the "while read LINE" preventing it from reading the line? OR is "tail -0f" causing the issue?
I made a typo in my post. Try this awk instead:
Code:
awk '{$1="";$2=""}1'
I tested it with the following little script:
Code:
#!/bin/bash
# file: sh.sh
{ sleep 1;for i in {1..5};do echo "2011-10-01 18:04:53         @nick   aight${i}">>file; sleep 1; done } &
tail -0f file |awk '{$1="";$2="";}1'
and the output was:
Code:
$ ./sh.sh
  @nick aight1
  @nick aight2
  @nick aight3
  @nick aight4
  @nick aight5
This looks fine so far but I also could not make it work with espeak (I am not familiar with festival), yet.
 
Old 10-01-2011, 08:30 PM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Duh!

Well, I found the *error*. The awk waits to process the complete file before it flushes its output through the pipe. Since there is no EOF the input is stuck in the middle of the pipe. Try this instead:
Code:
#!/bin/bash
{ sleep 1;for i in {1..5};do echo "2011-10-01 18:04:53         @nick   aight${i}">>file; sleep 1; done } & # simulate input

while read line;do
      echo "$line"|awk '{$1="";$2="";}1' |festival --tts
done < <(tail -0f file)
 
Old 10-01-2011, 08:51 PM   #6
texasone
Member
 
Registered: Jun 2008
Location: /home/lorax
Distribution: Debian Testing
Posts: 141

Original Poster
Rep: Reputation: Disabled
ctrs: I'll have to give that code a try. I ended up resorting to using "script -c". It works but I went back to the sed statement because of my laziness.
I'm probably going to spend the next little while cleaning it up to get rid of logins, logouts, and shorten those long paragraphs down to like 14 or so words.


Code:
script -c "tail -0f /home/user/.weechat/logs/irc.server.#channel.weechatlog  | sed 's/[^[:blank:]]* *[^[:blank:]]* *//'" /dev/null | while read LINE


do 
   echo $LINE
   echo $LINE | festival --tts
   sleep 0.75
done

Thanks for the help
 
Old 10-01-2011, 10:34 PM   #7
texasone
Member
 
Registered: Jun 2008
Location: /home/lorax
Distribution: Debian Testing
Posts: 141

Original Poster
Rep: Reputation: Disabled
Stupid question. had nothing to do with the Original Topic

Last edited by texasone; 10-01-2011 at 11:54 PM. Reason: Stupid question. had nothing to do with the Original Topic
 
Old 10-02-2011, 12:30 AM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by texasone View Post
ctrs: I'll have to give that code a try. I ended up resorting to using "script -c". It works but I went back to the sed statement because of my laziness.
I'm probably going to spend the next little while cleaning it up to get rid of logins, logouts, and shorten those long paragraphs down to like 14 or so words.


Code:
script -c "tail -0f /home/user/.weechat/logs/irc.server.#channel.weechatlog  | sed 's/[^[:blank:]]* *[^[:blank:]]* *//'" /dev/null | while read LINE


do 
   echo $LINE
   echo $LINE | festival --tts
   sleep 0.75
done

Thanks for the help
Ok, don't mean to sound harsh but this is bad. I am not familiar with script but from what I have read so far in the man-page this is not its main purpose. Have another look at post #5. The solution is to not pipe 'tail -0f' into awk. Instead let 'tail -0f' feed the loop only and then modify the variable with awk.

If you want to use sed and are willing to replace festival with espeak then you could do something like this:
Code:
tail -0f file  | sed -u  's/[^[:blank:]]* *[^[:blank:]]* *//'|espeak
That's it. It doesn't need a loop. The -u option tells sed to output immediately; without buffering. 'espeak' also does process the input line by line rather than waiting for the whole input to finish like festival does. However, espeak's speech quality is not as good as festival's.
 
Old 10-02-2011, 04:06 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
What's with all this complicated messing around with sed and awk anyway?
Code:
echo "@${LINE##*@}"
String manipulation in bash
 
Old 10-02-2011, 08:41 AM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by David the H. View Post
What's with all this complicated messing around with sed and awk anyway?
Code:
echo "@${LINE##*@}"
String manipulation in bash
Yeah, I did not see that right at the beginning and then I got caught up with that pipe issue. That is, which programs can deal with neverending input and which cannot. So, apparently awk and festival cannot deal with it.
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using SED to strip multiple leading hash symbols from a file forrie Linux - Software 4 10-29-2010 01:35 PM
strip of blanks using sed. visitnag Linux - Desktop 1 02-13-2009 08:26 AM
sed newbie question - strip leading character only if 0 Frayed Programming 6 05-22-2008 10:30 AM
Need to strip words from front of line. sed/awk/grep? joadoor Linux - Software 6 08-28-2006 04:39 AM
How sed strip /* comments aaronzh Programming 1 06-05-2003 05:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:36 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