[SOLVED] Create new file in bash using date as condition
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I'd say wpeckham's advice about figuring out the workflow is important. But unfortunately that workflow is somewhat dependent on the tool(s) used. Thus you have a bit of a circular dependency. To break that, I'd nudge you in the direction of awk or perl, mainly the former. I see a way to do it with two instructions with awk -- if the date field is always first in each block.
Regardless of which tools you start with, please post what you have begun so we can see the direction you are taking and can offer advice.
The concepts of planning I offered do not derive from the workflow, indeed may help DETERMINE the workflow. They depend upon analysis of the DATA flow as derived from the statement of the problem, data input, desired transformations, and data output, and not at all on the tools used. There is no circular dependency. I see why you think there may be, but you if you consider it as a programming problem there is none.
BASH can do this, PERL is almost DESIGNED to do this (and most things, it is one of the most versatile tools), or you can do this in python, Pascal, c, assembler, BASIC, FORTRAN, COBOL ... : the list is almost endless.
The OP needs to consider what he WANTS to use (BASH is an expressed preference, see the title of the thread) so we can assume that over awk, perl, sed, etc.
Bash can, using only internal commands and shell features, read files, compare and use strings, create files and folders, all of the critical pieces are there for this problem without calling awk, sed, grep, perl, or other external tools. He may, on consieration, wish to use one or more external tools as a 'helper' for his script, but it can be done without those.
.
i think by circular dependence he meant that it would be hard to draw a diagram without knowing the feature set of awk, for example ?
That makes sense. But it starts by assuming that you will first look at your tools to decide what you want to do. That seems backwards. First define properly what you want to do THEN look at your tools and find the right ones.
Both plans are partly out of line here. The question was how to do it in BASH, so awk, sed, etc. are off the table. Properly defining what you want to do is still valid.
I am looking forward to seeing what the first cut looks like. There are so many good ways to approach this.
i think by circular dependence he meant that it would be hard to draw a diagram without knowing the feature set of awk, for example ?
Yeah. That's about it. Also since this is a newbie subforum, the word "bash" often ends up meaning "anything except GUI" Hence the pointer to awk and Perl
awk is acessible via bash but not part of it. bash is just one of the interfaces to the system. You have bash, zsh, ksh, and dash on the one hand for text. On the other hand for graphics, you have Unity, XFCE, KDE, LXDE, and Openbox, just to name a few from either category.
Yes, check "man awk" for info on sub(), gsub(), match(), or substr() to see how far you can get with rearranging the existing numbers. Probably the latter two are most relevant. Maybe do not append the '.log' part to fn until you've got the first part of the file name settled.
As things get more complex, the problem becomes more appropriate for perl. But it should be doable within awk still.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,952
Rep:
Quote:
Originally Posted by rashmi88
Thanks. But, i am looking for the file format to be <filename_YYYYMMDD.log>. Can anybody help on this please?
if you know the pattern is going to be a filename[underscore]Numbers indicating a date. then why not search for the underscore then chop it off giving you the date to do with what you will?
OK, that is not the file format, the is the file NAME format. That is easy enough to put together in BASH, it should be doable easily in AWK (or a combination) but I do not AWK enough to say how.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.