ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Well, if you HAVE to do this with awk, I am sure one of the 'awk gurus' (I am not a member of that specific clan) can help you out. But! If it were me, I would check out csplit, which ought to be able to do what you want.
Can't try it out for you right now since I am <rant>stuck behind one of those wondrous machines designed in Cupertino that ships with a version of csplit that lacks half of the funcionality one would EXPECT!!!</rant>
Well, if you HAVE to do this with awk, I am sure one of the 'awk gurus' (I am not a member of that specific clan) can help you out. But! If it were me, I would check out csplit, which ought to be able to do what you want.
Can't try it out for you right now since I am <rant>stuck behind one of those wondrous machines designed in Cupertino that ships with a version of csplit that lacks half of the funcionality one would EXPECT!!!</rant>
thanks, I would like to make these with AWK, meanwhile I'm trying your method:
Code:
csplit: unrecognized option '--suppress-matched'
Try `csplit --help' for more information.
[sp80439@oc6566503017 kk]$ csplit --help
Usage: csplit [OPTION]... FILE PATTERN...
Output pieces of FILE separated by PATTERN(s) to files `xx00', `xx01', ...,
and output byte counts of each piece to standard output.
Mandatory arguments to long options are mandatory for short options too.
-b, --suffix-format=FORMAT use sprintf FORMAT instead of %02d
-f, --prefix=PREFIX use PREFIX instead of `xx'
-k, --keep-files do not remove output files on errors
-n, --digits=DIGITS use specified number of digits instead of 2
-s, --quiet, --silent do not print counts of output file sizes
-z, --elide-empty-files remove empty output files
--help display this help and exit
--version output version information and exit
Read standard input if FILE is -. Each PATTERN may be:
INTEGER copy up to but not including specified line number
/REGEXP/[OFFSET] copy up to but not including a matching line
%REGEXP%[OFFSET] skip to, but not including a matching line
{INTEGER} repeat the previous pattern specified number of times
{*} repeat the previous pattern as many times as possible
A line OFFSET is a required `+' or `-' followed by a positive integer.
thanks, I would like to make these with AWK, meanwhile I'm trying your method:
Code:
csplit: unrecognized option '--suppress-matched'
Try `csplit --help' for more information.
[sp80439@oc6566503017 kk]$ csplit --help
Usage: csplit [OPTION]... FILE PATTERN...
Output pieces of FILE separated by PATTERN(s) to files `xx00', `xx01', ...,
and output byte counts of each piece to standard output.
Mandatory arguments to long options are mandatory for short options too.
-b, --suffix-format=FORMAT use sprintf FORMAT instead of %02d
-f, --prefix=PREFIX use PREFIX instead of `xx'
-k, --keep-files do not remove output files on errors
-n, --digits=DIGITS use specified number of digits instead of 2
-s, --quiet, --silent do not print counts of output file sizes
-z, --elide-empty-files remove empty output files
--help display this help and exit
--version output version information and exit
Read standard input if FILE is -. Each PATTERN may be:
INTEGER copy up to but not including specified line number
/REGEXP/[OFFSET] copy up to but not including a matching line
%REGEXP%[OFFSET] skip to, but not including a matching line
{INTEGER} repeat the previous pattern specified number of times
{*} repeat the previous pattern as many times as possible
A line OFFSET is a required `+' or `-' followed by a positive integer.
Strange, it works for me. My version of csplit (using Debian 8.1):
One of the hard things I found with learning awk (and perl) is to not try and code like you would for a "traditional" language.
awk offers a lot of facilities that help (and hide) with the mundane work that has to be done. You would do well to examine grails post - I imagine understanding it will be difficult without having the documentation handy.
One of the hard things I found with learning awk (and perl) is to not try and code like you would for a "traditional" language.
awk offers a lot of facilities that help (and hide) with the mundane work that has to be done. You would do well to examine grails post - I imagine understanding it will be difficult without having the documentation handy.
Nice use of RT there grail.
I have the O'Reilly sed & awk 2nd ed in hand and still had to break it down before understanding it. This is the first actual use of RT that I recall seeing and had to refer to it (page 266) twice before it sunk in! Very nice - I'll "steal" (i.e. learn from) this one - thanks!
Note: RT appears to be a gawk-ism so may not work on other awks.
I have books on everything - except awk. I didn't see the need initially, as I thought it was something I wouldn't get to use much.
Wrong.
I make do with the manual - people like grail keep teaching me new things all the time.
For some reason awk has never stuck, in the sense that I know when to use it but I rarely see the intuitive path to a good awk solution quickly. As a result, I end up doing many things with sed and shell scripts that would be better done with awk. I then see an effective awk one-liner from someone like grail and cry...
I have recently been following and now participating in awk problem threads to try to remedy that.
Starting premis looks OK but there are problems if you test it...
Not "all other lines get redirected", only those between "kk" lines. The leading lines cause a problem with the redirect.
You are missing a closing parenthesis on the if(... clause, probably typo but should have been tested.
The definition of redirFile=stdout is invalid in the redirect, so it fails on the leading lines before the opening "kk" line.
If you fix the parenthesis and avoid the NULL redirect it works. But if you run it twice it appends to existing files instead of writing them from input, requires removal of pre-existing output files as extra step and can be confusing!
Nice try, see if you can tweak it up from here!
Last edited by astrogeek; 08-05-2015 at 02:50 AM.
Reason: Fixed my confusion and typos
No starting parenthesis and hence no closing parenthesis in the if. (A single statement doesn't require ripple bracket parenthesis so long as it ends with a semi colon).
Double redirection not really required in awk. The first use zaps the destination file. (By habit I wrote >>).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.