LinuxQuestions.org
Visit Jeremy's Blog.
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 11-07-2007, 02:16 PM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Rep: Reputation: 32
bash command redirecting output to file


Hi,

I am trying to do something 'simple'

I have a bash program that gives output in the commad prompt eg.
Code:
00 12 03 C2 EE ...
I want to grep a line containing value 'C2' and put the line into a file.

I tried:
Code:
$./serial 1 | grep C2 > data
but it doesn't work. I don't get any errors but there is no data in the file. BTW, serial is the program that monitors serial port 1 and spits it out on the command.

Anyone knows how to do it?

Thanks a million.

Indy
 
Old 11-07-2007, 03:09 PM   #2
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 281Reputation: 281Reputation: 281
I think you might want the command tee.
Code:
$./serial 1 | grep C2 | tee data

Last edited by pljvaldez; 11-07-2007 at 03:12 PM.
 
Old 11-07-2007, 03:46 PM   #3
WAJEDUR REHMAN
Member
 
Registered: Aug 2007
Posts: 43

Rep: Reputation: 15
I'm not on a machine try this

$./serial 1 | cut -f4 > data

Last edited by WAJEDUR REHMAN; 11-07-2007 at 04:40 PM. Reason: miss typed
 
Old 11-07-2007, 03:52 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Maybe this serial program is not printing to stdout, but instead stderr. When you do your command with grep, do you see the output of serial printed to the terminal in the same way that you would when you run without the grep?

If so, you should specify that the stderr should be sent to the stdout file handle:
Code:
$ ./serial 1 2>&1 | grep C2 > data
 
Old 11-07-2007, 08:25 PM   #5
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Original Poster
Rep: Reputation: 32
thanks lads but none of them work. There is no data put into the file...

I wrote the serial program in C and it prints out with printf command, if that is in any help.

Indy
 
Old 11-07-2007, 08:47 PM   #6
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
ignore the file part for a moment. What do you get with

$./serial 1 | grep C2

Last edited by billymayday; 11-08-2007 at 03:31 AM.
 
Old 11-08-2007, 03:05 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Using printf should print to standard output, unless you explicitly change the meaning of stdout in your program.

Then: what billymayday said.
 
Old 11-08-2007, 04:36 AM   #8
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Original Poster
Rep: Reputation: 32
Billymayday:
I get exactly what I want. Everything is ignored except the line containing 'C2'. But when added the last part "> data", nothing is written to it.

mathewg42: Ok. Thanks.

Indy
 
Old 11-08-2007, 04:40 AM   #9
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
do you have write permission where you are running it?

try touch data and then .. >> data
 
Old 11-08-2007, 05:18 AM   #10
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Original Poster
Rep: Reputation: 32
I beleive I have the permissions yes. I didn't understand your instuctions billymayday.

I tried "touch data". Nothing.
Then I also tried "./serial 1 | grep C2 >> data" and the file size is 0.

Indy
 
Old 11-08-2007, 05:48 AM   #11
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
>> appends to an existing file.
> truncates existing files.

Please can you enter these commands, and paste the results (with the commands too). pease everything.
Code:
echo $SHELL
id
ls -ld .
lsattr -d .
touch mytestfile
ls -l mytestfile
./serial 1 > mytestfile
cat mytestfile
./serial 1 |grep -i C2 > mytestfile
cat mytestfile
Please enclose your pasted results in [code] tags to aid readability. Don't omit anything. I'd like to see all output, and the commands as you typed them. If the un-filtered output of serial 1 is very long, please link to a nopaste posting for that output.

Last edited by matthewg42; 12-15-2007 at 06:18 PM.
 
  


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
Redirecting output of background processes after they've been started (BASH) DaneM Programming 18 08-08-2011 09:03 PM
redirecting output in bash scripts s_siouris Programming 3 05-13-2006 08:27 AM
redirecting the output of pwd into konqueror as a url. using bash fiery_ice Linux - Newbie 5 04-05-2006 03:13 PM
Redirecting output of running command tommyr1216 Linux - General 3 03-04-2005 08:20 PM
Redirecting output to a command-line argument of another command madiyaan Linux - Newbie 1 02-19-2005 04:35 PM

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

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