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.
To preserve formatting place your code snippets inside [CODE]...[/CODE] tags. You may type those yourself or click the "#" button in the edit controls.
You appear to be using the right tool, awk, but it is not clear from your description how your data blocks are delineated. For example, does AAA****** always mark the beginning of a "block", or is the the END OF TRANSMISSION line?
If your code snippet represents your actual approach, then all you really need is a variable which is initially set to 1 for each file, then incremented at the beginning of each subsequent data block, wherever that may be found. Then simply append that variable to your print statement.
The way things usually work here at LQ is that you should try to find a working solution yourself, then ask for help when you get stuck somewhere, providing a minimal example which illustrates the difficulty you are having. This helps others to understand the problem itself and also gauge how to offer help in a way most appropriate to your demonstrated code preference and skill level.
You may want to review the Site FAQ for guidance in posting your questions and general forum usage. Especially, read the link in that page, How To Ask Questions The Smart Way. The more effort you put into understanding your problem and framing your questions, the better others can help!
Editing the original post then stating the same thing again is very confusing to those trying to undersstand your question. Please do not do that. Your restated question is also quite different than the original.
So, taking your restated question as the starting point, and continuing with awk as the tool of choice, that is a relatively simple problem. You have already answered it yourself, although you may not realize it.
You need only a counter variable which is incremented and prefixed to the print statement arguments, and reset to 1 each time if encounters AAA at the beginning of a line.
Take a look at your original awk code, ignore the filename portion of that code, and see if you can get it to work with a simple counter. If not, please post your code here so we can see what you have tried to do and try to explain how it is not working. That will help you to understand the problem better, and help those here to to provide useful replies.
Last edited by astrogeek; 03-13-2019 at 01:35 PM.
Reason: typo
uses an implicit test and print. The test can have side effects. For example, an initial empty line or "0" line is not printed, because $0 becomes "0" or "00" and the implicit test sees a number and becomes false.
An explicit print makes things clear:
Code:
awk '/^AAA/{c=1} {print c++$0}' file
Initial lines are printed with prefixes 0,1,2 regardless of their contents.
I don't know what behavior the O/P intended but software should be predictable, especially if it controls a Boeing 737 max 8...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.