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.
As the output to a file from Grep cannot be formatted I would be grateful if anyone can help me here with some formatting I could do on the file using Sed. I have searched around but none of the examples I have found address what I wish to do, namely, combine into blocks all the matches found within each file. Here is a sample:
I have tried using "| sed G " on the grep output but that merely inserts a blank space between every single entry which is not what I need. Any help would be appreciated.
The input field separator is set as a colon and then the whole file name is thus in field $1.
prev holds the previous value of $1.
If the previous value is different than the current $1 then print a new line.
But either way, always print out the line.
I agree that sed would not be the best choice, if at all.
Awk, or ... I thought bash supports a printf function.
The problem with these types of questions is that while this may solve post #1, it bears the question as to what other output manipulations are desired?
I'm going to be a contrarian, but this is child's play to do in Pascal. You can install Free Pascal from most repositories.
And the following source code runs just fine and Pascal runs fast.
program MyFmt;
Var
Save, Compare, Line: String;
Begin
ReadLn(Line);
Repeat
Save := Copy(Line,17,3);
Repeat
WriteLn(Line);
ReadLn(Line);
Compare := Copy(Line,17,3);
Until ((Eof) or (Compare <> Save));
WriteLn;
Until Eof;
End.
I said I was being a contrarian. People can use what they want to use.
I have used awk quite a lot, it does certain things very nicely like formatting data into sql statements. But it isn't the only way to do things, people should know how to code and know at least one compiled language. c++ is a good language for professional programmers, it is not a good language for hobbyists. It's harder to code and it is harder to debug. I've seen many posts where someone wants to learn to code, so they try to learn c or c++ just because that is what industry uses when in reality Pascal would be a better choice. Pascal is a lot simpler and easier for a hobbyist and gives nearly the same performance. Many who try to learn c or c++ end up just getting pissed at themselves.
I suspect the given code misses to write the last line.?
The natural method to read through a file is a
while not Eof
And, rather than a read-ahead, I recommend the use of a prev variable (previous line, to be compared with the current line), like in post #2.
Agreed, pascal was (probably still is) a great teaching language - as it was designed to be. I'm not aware of what the current status is, but with seemingly a new language appearing every few months, it'd probably have a struggle getting new converts.
Hi Turbocapitalist,
Thank you very much. Your response does exactly what I was looking for. I have not had any previous involvement with Awk as I was given to understand that it was a difficult language to master. It looks very powerful.
Thank you for your suggestion. On the plus side it kept everything within Grep and is feasible where there would only be a few files involved. However, the downside for me is that my Grep output will have a huge number of files whose names I obviously don't know in advance. This means that it would not be very productive having to run Grep, go through the output and select the file names and then run Grep again with your coding but this time specifying the actual file names. Thank you anyway. I appreciate your help.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.