LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-18-2007, 02:14 AM   #1
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Rep: Reputation: 30
c program help


hi all
i have to write some script in c program
i tried this
Code:
//cc simple.c -o simple
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{

char bb=system("cat file.txt | sed -n '/Underlying Value/,/]Top/p' | egrep -v 'Underlying Value|]Top' | sed 's/\[[0-9]\{1,\}\]//g' > /home/munna/Desktop/munna.txt");
printf("******** %s \n",bb);

  return 0;
}
and the file.txt is
Quote:
[1]Home > [2]F&O >
Watch

All contracts of NIFTY
As on Hours IST

Instrument Type Underlying Expiry Date Option Type High
Price Low Prev Close of contracts traded
Turnover Lakhs Underlying Value

[5]AAAAA [6]GGGGGG [7]25OC200 - - 5389.80 5203.10 5673.15 5255.00
89085 234115.83 5143.90
[8]AAAAA [9]GGGGGG [10]29NO2007 - - 5389.00 5211.00 5674.00 5269.95
5265 13869.14 5143.90
[11]BBBBBB [12]GGGGGG [13]25OC200 [14]CE [15]5600.00 74.00 15.00
168.60 34.05 810 2281.01 5143.90
[16]AAAAA [17]GGGGGG [18]27DE200 - - 5410.00 5125.00 5664.05 5265.00
694 1828.64 5143.90

[243]Top

[1]http://www.google.com/
[2]http://www.google.com/
[7]http://www.google.com/
[79]http://www.yahoo.com/
[2447]http://www.google.com/
[700]http://www.yahoo.com/
but this program saying some errors like
Quote:
simple.c:9: warning: unknown escape sequence '\}'
simple.c:9: warning: unknown escape sequence '\]'
i wanna output in a string "bb".

please help me

thank you in advance
 
Old 10-18-2007, 03:14 AM   #2
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
Since you just want to call other programs, why not just use shell scripting?

Also, im not sure you can pipe programs together like that, and im fairly sure that i see various shell commands within that command you are issuing, so i doubt it will ever run in C. Also, bb is not a string, but a single character.

And those errors seem to be coming from the compiler trying to compile that script, who knows what the compiler will make of it. Just use regular shell scripts instead, save yourself the trouble of debugging that.
 
Old 10-18-2007, 07:33 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You need to escape the escape chars: use \\[ instead of \[. This forces the compiler to use the \ character rather than interpreting it (such as \n.)

BTW, you aren't really writing a C program. It's shell wrapped with C, which is just sent to a shell anyway.
ta0kira
 
Old 10-18-2007, 09:17 AM   #4
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
If you wanted to be really hardcore, you could write out the function of each piped program you call in the system() function.

I have a copy of The C Programming Language by Kernighan & Ritchie, and over the first few chapters you learn how to make a program that does exactly this. Of course, it takes a bit of extra typing to get the functionality you are requesting of those other programs, but the fact is you learn HOW TO DO IT!

 
Old 10-18-2007, 12:16 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Well if you're going that far you might as well use forks and execs and avoid the shell altogether...
ta0kira
 
Old 10-19-2007, 11:59 PM   #6
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Which really makes me wonder why a script would be written in C, when the exact same script could be written in the locally-installed shell. Execution time shouldn't be anything to worry about when it comes to something simple like this.

If you're going to learn C (just saying, keep in mind), I found the best way was to mostly everything yourself, instead of relying on pre-written/compiled commands.

I remember reading somewhere (I believe on this very forum) that use of the system() call was greatly frowned upon. *shrug*
 
  


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
A program for FC4 to communicate with a Windows Hyperterminal Program at other end vhasun Linux - Software 2 05-19-2006 02:54 PM
Total recovery: Which program? ghost4linux, YaST2? Best drive imaging program? lagu2653 Linux - Software 1 06-20-2005 01:44 PM
Key bindings program (custom pasting, program execution, etc.) jrdioko Linux - Software 2 02-05-2005 09:09 PM
Stop java program(threaded program..should end cleanly) rmanocha Programming 4 11-09-2004 09:36 AM
Gtk-Warning but program still works... I close konsole, program closes Laptop2250 Linux - Software 2 11-14-2003 11:18 PM

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

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