LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-06-2010, 02:35 PM   #1
TAPPM
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Rep: Reputation: 0
Text editor with search and capture


Hello all
i was wondering if in all the editors/tools in linux, if there was a prog
that can extract a portion of a text file, giving it a starting line of say
o as the first character on a line of say o35565 oxxxxx

then when it finds the next line down the file with the same thing
oxxxxwhatever

it could take those lines and all the lines between them and save it to another
file?
i dont know if you call this parsing? or what.
can anybody help?

Thank you

Tappm
 
Old 12-06-2010, 02:38 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Sounds like what you are wanting is a tool such as AWK or SED, rather than a text editor. If you have no need to actually open the file and look at it yourself, or edit it a typical "text editor" context, then please (use Google) read about GNU Sed, and GNU AWK (on Linux, this is called "gawk"), and decide if either of these tools sounds like the thing you need.
Let us know what you learn.

Kind regards.
 
Old 12-06-2010, 03:38 PM   #3
TAPPM
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks for the input

i'll look at those.
what the problem is exactly is on my CNC machines at work i have the ability to save all my part cutting programs into one large file, because saving them
individually would take along time.
so the single file appends on after the other into a large txt file, at the beginning of a program the first line is the prog no. with the format

o354444(description of program)
course the o is the only constant there as the prog files all have a different no. Then at the start of the next file is the same thing

o354433(description of program)
so if the needed program could search and output these into individual files, it would be a great tool.

Thankyou again
 
Old 12-06-2010, 03:44 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Gotcha now, I understand. These are a whole whack of NC programs, all concatenated into one long file. You want to use a tool to split the big file, so that each invidual NC program is its own file. Geez, are CNC machines still that frikkin slow at uploading their data? Sounds like it hasn't improved since ~1996 when I was doing the same thing. Or is it just the time to locate and save each file individually by hand, that takes up the time?

Anyhow, yes, AWK would be a good tool. Or, since the task is not super complicated, you could likely do this using just bash shell too.

Either way, you'll want to tell us what sort of PC you will be running this program on - Windows or Linux? If Linux, which Distro and version is it? Sometimes this makes a difference.

Also, if you will be needing help writing a bash script or AWK program to do this job,, LQ members will expect a couple things:

1) Post (inside code tags please) a chunk of the long file so we can see precisely what the data looks like. The chunk you show should include at least one whole NC program, and a couple lines above and below it.

2) members will generally expect you to do research and development of your own code, on your own, to some extent. We love to help, but we also really like when the helpee takes an active role.

3) This *sounds* like an easy task, and if so, a lot of R+D will not be required. If it's as easy as I hope and believe it will be, it should take no more than a few to maybe a dozen lines of code. BUT - until we see the data, I am speculating a lot here..

Last edited by GrapefruiTgirl; 12-06-2010 at 03:51 PM.
 
Old 12-06-2010, 04:17 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
With luck, this could be a simple one-liner. Look in the file for character sequences that delimit the sections you want to break out of the overall file. These can be record delimiters for things like AWK and Perl. If you can concisely define record delimiters, then you can get Perl or AWK write out each record to a file.
--- rod.
 
Old 12-06-2010, 04:38 PM   #6
TAPPM
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks alot for your help
it would be more a bit easier to use the windows xp box I use at work
but if i need to i could bring my laptop SUSE v6/windows dual boot box to do it with
Here is an old == small file of a complete all files save, couple years old
the newest ones are a couple megs


seeing as how ive never used awk, yes i could use some help with wielding that prog

thank you again
Attached Files
File Type: txt example of all save.txt (187.3 KB, 17 views)

Last edited by TAPPM; 12-06-2010 at 04:48 PM.
 
Old 12-06-2010, 04:51 PM   #7
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello TAPPM and welcome to LQ (EDIT: oh sorry you're not new here... ),

I don't know if this helps, the editor vim has a feature named "folding". It is useful to hide parts of a file.
Vim is available for both, Linux and Windows and I'm using it very often with Windows. You can install it as an executable on a USB-drive and use it with every Windows-PC.... well, enough of the Windows stuff

You may look here: http://vimdoc.sourceforge.net/htmldo...l#fold-methods or here http://vim.wikia.com/wiki/Folding but there is much further documentation.

Markus

Last edited by markush; 12-06-2010 at 04:52 PM.
 
Old 12-06-2010, 06:26 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Something like this may (closely enough) resemble what you're after?
Code:
awk 'BEGIN{
  out="default"
}
{
  if($0 ~ /^O[0-9]+/){
    out=$1
  }
}
{ 
  print $0 >"prog_"out
}' /tmp/nc.txt

Cheers,
Tink
 
1 members found this post helpful.
Old 12-06-2010, 06:30 PM   #9
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
@ Tink,

It looks lovely.

One thing though: the ^O should be an ^o (lowercase oh) -- assuming not a typo on OP's part.
 
Old 12-06-2010, 06:39 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by GrapefruiTgirl View Post
@ Tink,

It looks lovely. :)

One thing though: the ^O should be an ^o (lowercase oh) -- assuming not a typo on OP's part.
I went w/ the sample data in his attached file ...

;)


Cheers,
Tink
 
Old 12-06-2010, 06:44 PM   #11
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Quote:
Originally Posted by Tinkster View Post
I went w/ the sample data in his attached file ...




Cheers,
Tink

Of course.. Why didn't I think of that? :/

Cheerios!

EDIT:

By the way, here's what I was initially thinking of, using bash rather than awk:
Code:
#!/bin/bash
while read var; do
        if echo "${var}" | grep -q '^o[0-9]*$'; then
                 filename="program_${var}"
                 [ -e "$filename" ] && rm "$filename"
        fi
echo "$var" >> "$filename"

done < input_file
The above can be saved as a script, in the same directory as the big input_file is located in, and executed. Or, just copy & paste it into a terminal while you are located in the directory where the input_file resides. Change input_file to the real input filename.

Note that the script will erase & re-create any output file that finds to be pre-existing.

Last edited by GrapefruiTgirl; 12-06-2010 at 09:10 PM.
 
Old 12-07-2010, 11:22 AM   #12
TAPPM
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Original Poster
Rep: Reputation: 0
downloaded a copy of gawk , it installed but will not run on this windows box
kinda strange, installed to all programs menu but only shows doc/pdf files no executable
tried to run it from a dos window, no luck.
any ideas?


Thank you
 
Old 12-07-2010, 11:41 AM   #13
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Chances are you'll need to modify the PATH to include its
directory, and run it from the command line (as there's
no GUI awk to the best of my knowledge).

And since this obviously isn't a Linux question after all
I'm moving it to <PROGRAMMING>.


Cheers,
Tink
 
Old 12-07-2010, 12:21 PM   #14
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Quote:
Originally Posted by TAPPM View Post
downloaded a copy of gawk , it installed but will not run on this windows box
kinda strange, installed to all programs menu but only shows doc/pdf files no executable
tried to run it from a dos window, no luck.
any ideas?


Thank you
I don't know what you downloaded nor why it doesn't seem to work. I don't use Windows. While a lot of other members do use Win, you may be waiting a little bit longer than usual for help that's specific to it, or to gawk on Windows.

In case it helps, it appears to me that you should have gotten your gawk for Windows from here:
http://sourceforge.net/projects/gnuwin32/files/gawk/
Is that what you used? If not, please link to where/what you downloaded.

Until you have a working gawk (or awk or bash) on your Win, that's as much as I can help. Maybe you could use a Linux liveCD if this Windows method continues to be pesky? Or, just get access to a Linux machine and do this there.
 
Old 12-07-2010, 12:40 PM   #15
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Or, use Cygwin, which should include AWK.

--- rod.
 
  


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
Inquiry:How to Search/Replace in vi text editor hadimotamedi Linux - Newbie 1 09-07-2009 11:57 PM
text selection question in text editor olin Linux - Software 1 09-25-2007 01:25 PM
Text Editor on Fedora Core 4 in Text Mode blong4life Linux - Software 5 07-31-2006 09:07 PM
Which light text editor can copy text from file and paste in browser? davidas Linux - Software 9 03-06-2006 11:28 AM
Java: Getting the contents of a search dialog box in text editor i am making ludeKing Programming 4 04-23-2004 08:46 PM

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

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