Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
how can I copy this block if for example find "13" in the first column and "-1" in the last ? Then, looping to the rest of the file doing the same all over again?
and then, go few lines above and copy from <event> to </event> of this block into another file..If the criteria are not satisfied, then loop into to next block... Maybe with awk is feasible but I am not that advanced
and then, go few lines above and copy from <event> to </event> of this block into another file..If the criteria are not satisfied, then loop into to next block... Maybe with awk is feasible but I am not that advanced
What have you done to find a solution to the problem? Other than to post here.
We will aid you when you help yourself to a solution. Provide us with what you have attempted and then maybe someone will be able to assist.
Yes for this block there is "13" "1" but for others is "13" and "-1" . To put it in other words, the first column could have "11" or "-11" "13" or "-13" "15" or "-15" and "-1" or "0" or "1" on the last column...All the possible combinations
@ Onebuck - Sorry, but I am that advanced to process this kind of complicated block...My experience is with single-line files...You would save my day if you could possibly indicate me how to start (do not tell me to read the awk manual though ;-)
Yes for this block there is "13" "1" but for others is "13" and "-1" . To put it in other words, the first column could have "11" or "-11" "13" or "-13" "15" or "-15" and "-1" or "0" or "1" on the last column...All the possible combinations
So this is information you need to give the first time as it is quite different than your original request.
Quote:
@ Onebuck - Sorry, but I am that advanced to process this kind of complicated block...My experience is with single-line files...You would save my day if you could possibly indicate me how to start (do not tell me to read the awk manual though ;-)
So you have said here you could solve it were a single line file. Show us how you would do that?
Yes for this block there is "13" "1" but for others is "13" and "-1" . To put it in other words, the first column could have "11" or "-11" "13" or "-13" "15" or "-15" and "-1" or "0" or "1" on the last column...All the possible combinations
@ Onebuck - Sorry, but I am that advanced to process this kind of complicated block...My experience is with single-line files...You would save my day if you could possibly indicate me how to start (do not tell me to read the awk manual though ;-)
Thanks for the links, but I could try something if you could possibly demonstrate me how to loop inside the different blocks that have the same header...
Thanks for the links, but I could try something if you could possibly demonstrate me how to loop inside the different blocks that have the same header...
You will get some examples that will lead to learning to develop a solution. If you do not understand something or get stuck with your script then present it. We will assist. You will learn much more by doing it yourself. LQ members will aid you when you help yourself to a solution. Provide us with what you have attempted and then maybe someone will be able to assist. I as other LQ members will help you but not carry you! Rewards are possible.
LQ members are volunteers and wish to share knowledge but I for one will not carry you. Especially if you show no personal effort.
So here you are demonstrating the use of which record you are looking to get information from by using NR.
Although you have again chosen an example that is no way related to the example data you have provided.
By this I mean the following part of the code:
Code:
/-15 1 4 4 0 0/
Will never work with the example as this regular expression does not appear
May I suggest, since your original question was:
Quote:
how can I copy this block if for example find "13" in the first column and "-1" in the last ?
That you look at which ever awk manual (physical or online) that you are using for the variables RS (record separator) and NF (number field)
These should allow you to say what you consider to be a record (ie between event tags) and which fields you are interested in within that record.
$/ = '</event>';
while (<>) {
^\s* # Optional white space at start of line
[+]?1[135] # 15
[-\d\sE+.]+ # numeric data
[-]?1.\.? # final 1
\s*$ # Optional white space at end of line
}
it will return correctly only those block requiring the + for the 1rst column and the -1 for the last one - If however I do
Code:
$/ = '</event>';
while (<>) {
^\s* # Optional white space at start of line
[-]?1[135] # 15
[-\d\sE+.]+ # numeric data
[+]?1.\.? # final 1 (not negative)
\s*$ # Optional white space at end of line
}
it copies ALL the blocks and not only the "opposite" combination... What I need is that IFF the first column match "+-11" || "+-13" || "+-15" AND the last one is "+1" (or whatever number I give, but the sign +/- makes the difference) only then copy block ..in the above example, I just require only "+" or "-" at one time ,but in any case the script looks failing at the second criteria matching... Of course if you can think of something in bash, you are most welcome!
Well I am not sure why the jump from awk to perl, other than you are more comfortable here (cool).
So I hate to state the obvious, but maybe you are too close to see, but your positive numbers do not have a plus sign!
In fact the only areas where a plus sign appears is in the exponent portions.
So, if we look at your regex:
Code:
[+]?1.\.?
This ask for zero or one +'s followed by a 1 then any character and then zero or one dots (.)
As the plus is nowhere to be found it is using the zero option. Note, this does not discount the minus (-) sign but rather that there is no plus (+).
After this it matches the rest of the line. I would point out, based on your input from post #13, that as the first dot (.) will already match the dot (.)
at the end of every line, the final part of your regex is mute (ie \.? is not required)
Yes, you are right... So, it is possible to get the "-1" entries but if I want ONLY the "1" how can I do it? So far, the "1" just copies both +1/-1 as you said, and this is why I used the [+]?1... Alternately, I was thinking that when I request the "-1" also remove these blocks from the source file (how would this be possible?) - Of course I would prefer the 1rst option , ie a syntax that would work the same for "-1" and "+1"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.