LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 08-25-2010, 09:39 AM   #1
Feynman
Member
 
Registered: Aug 2010
Distribution: XUbuntu
Posts: 71

Rep: Reputation: 15
Cuting/executing text from a file


I need a way to read and delete text found before a unique string, then delete the string. I would like to have the text stored in a variable--or better yet--executed it as a shell script directly.

I intend to execute the stored text in bash. It does not matter much what language the program is it, however efficiency is desirable. This script is not going to be used on large files, but it may be executed many times sequentially so the faster the script works the better.

Thanks

Here is an example:

exclamation-"It worked!"
echo $exclamation
cd
end of script
blah blah
blah blah blah
blah blah blah

I would like to execute everything before "end of script" as if it were a shell script

Last edited by Feynman; 08-25-2010 at 10:30 AM.
 
Old 08-25-2010, 09:42 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Feynman View Post
I need a way to read and delete text found before a unique string, then delete the string. I would like to have the text stored in a variable--or better yet--executed it as a shell script directly.

I intend to execute the stored text in bash. It does not matter much what language the program is it, however efficiency is desirable. This script is not going to be used on large files, but it may be executed many times sequentially so the faster the script works the better.

Thanks

Here is an example:

exclamation-"It worked!"
echo $exclamation
cd
end of script

blah blah blah
blah blah blah
http://www.linuxquestions.org/questi...ml#post4076233

; this is yet another indication that input data is poorly organized - the piece to be executed should have been in a separate file in the first place.

Last edited by Sergei Steshenko; 08-25-2010 at 10:42 AM.
 
Old 08-25-2010, 09:48 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Also:

perldoc perlopentut
perldoc -f open
perldoc -f chmod
perldoc -f system
.
 
Old 08-25-2010, 09:59 AM   #4
Feynman
Member
 
Registered: Aug 2010
Distribution: XUbuntu
Posts: 71

Original Poster
Rep: Reputation: 15
This is actually not part of data extracting. See, the other part of the program is to make input files. I have a method where I make a template that is very similar to the input file for the actual program, except certain numbers replaced with parameters. I have the script fill in parameters (e.g. $1, $2 ...) and create the input file.
However, different programs have different syntax for execution. I would like to have both the template for the input and the script to execute it and parse the output all in one file--for easy use. So if I made the script to execute the input and parse the output the first part of the file, then separate it by some string that is not a command for any widely used language. Then I just have the template part of the file follow.

I cannot help that the output of quantum chemistry packages is poorly organized. My task is to graph the potential energy of water as a function of bond length. I have some good tools that can get me pretty far, and I am not about to rewrite Hartree-Fock algorithms when I can parse text.
 
Old 08-25-2010, 10:13 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Feynman View Post
I need a way to read and delete text found before a unique string, then delete the string. I would like to have the text stored in a variable--or better yet--executed it as a shell script directly.
seriously, give a better example next time. Show what your desired output is.

Code:
$ more file
blah blah junk
blah blah myunique string
this is the end

$ awk '/myunique/{gsub(/.*myunique/,"");f=1}f' file
 string
this is the end
 
Old 08-25-2010, 10:31 AM   #6
Feynman
Member
 
Registered: Aug 2010
Distribution: XUbuntu
Posts: 71

Original Poster
Rep: Reputation: 15
Sorry, I was trying to execute everything before "end of script"--I edited the question. I am sorry for the confusion.
 
Old 08-25-2010, 10:51 AM   #7
Feynman
Member
 
Registered: Aug 2010
Distribution: XUbuntu
Posts: 71

Original Poster
Rep: Reputation: 15
Sergei Steshenko-- I tried

#!/bin/perl
while (<$ARGV[0]>) {
eval if $1 .. /$ARGV[1]/
}

can you help me fix this?
 
Old 08-25-2010, 10:58 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Feynman View Post
Sergei Steshenko-- I tried

#!/bin/perl
while (<$ARGV[0]>) {
eval if $1 .. /$ARGV[1]/
}

can you help me fix this?
Why 'eval' ? I.e. are you going to extract Perl code ?

Reread my post #3 in the thread ( http://www.linuxquestions.org/questi...0/#post4077249 ) - it suggests the functions to be used and their order.

Also, dealing with Perl first and foremost put in the beginning of your script, just after the '#!/bin/perl',

Code:
use strict;
use warnings;
and make sure the script compiles and issues no runtime warnings.

Last edited by Sergei Steshenko; 08-25-2010 at 11:01 AM.
 
  


Reply

Tags
cut, execute, parse, string


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
executing a text file one line at a time with bash? mivison Programming 18 05-24-2009 01:23 PM
Remove lines in a text file based on another text file asiandude Programming 10 01-29-2009 10:59 AM
Linux Text File convert to Windows/Notepad Text File = Wrapped? backroger Linux - Software 4 01-18-2009 05:54 AM
text match pipe to file then delete from original text file create new dir automatic tr1px Linux - Newbie 6 09-10-2008 09:40 PM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM

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

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