LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Text editor with search and capture (https://www.linuxquestions.org/questions/programming-9/text-editor-with-search-and-capture-848712/)

TAPPM 12-06-2010 02:35 PM

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

GrapefruiTgirl 12-06-2010 02:38 PM

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.

TAPPM 12-06-2010 03:38 PM

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

GrapefruiTgirl 12-06-2010 03:44 PM

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..

theNbomr 12-06-2010 04:17 PM

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.

TAPPM 12-06-2010 04:38 PM

1 Attachment(s)
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

markush 12-06-2010 04:51 PM

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

Tinkster 12-06-2010 06:26 PM

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

GrapefruiTgirl 12-06-2010 06:30 PM

@ Tink,

It looks lovely. :)

One thing though: the ^O should be an ^o (lowercase oh) -- assuming not a typo on OP's part.

Tinkster 12-06-2010 06:39 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 4182877)
@ 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

GrapefruiTgirl 12-06-2010 06:44 PM

Quote:

Originally Posted by Tinkster (Post 4182885)
I went w/ the sample data in his attached file ...

;)


Cheers,
Tink

:doh:
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.

TAPPM 12-07-2010 11:22 AM

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

Tinkster 12-07-2010 11:41 AM

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

GrapefruiTgirl 12-07-2010 12:21 PM

Quote:

Originally Posted by TAPPM (Post 4183732)
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.

theNbomr 12-07-2010 12:40 PM

Or, use Cygwin, which should include AWK.

--- rod.


All times are GMT -5. The time now is 02:34 AM.