Create new file in bash using date as condition
I need to create a new file using bash based on the date provided in the file. The contents are provided in the notepad file like shown below. Here for every date mentioned I need to create a file and name it with that particular date.
Date: 01-01-2007 CPU 3 7098 21294 Monitor 2 6500 13000 Mouse 20 300 6000 Total: 40294 --------------------------------------- Date: 02-01-2007 CPU 3 7099 21297 Monitor 2 6501 13002 Mouse 20 301 6020 Total: 40319 --------------------------------------- As I am very new to shell scripting, not sure how to start with this request. |
Hi rashmi88 and welcome to LQ.
Some suggestions are to review the following links about bash scripting and make some attempts at a first script that just runs a command or two. As I say a lot, "Whatever you can type in the command line, you can put into a bash script." This is because a bash script is also command line commands, running automatically. Here are the links: Bash for Beginners Advanced Bash Scripting Guide My Bash Blog With regards to your question, you seem to have defined part of the intent or method needed to do your script. My concern is that many times people ask a seemingly simple first question, get past that, and then their real needs or requirements start showing up. Either case, it is fine to start fundamentally. You seem to be saying, "Find a date record, and create a new file using that date as part of the name", and then you have no further information about what goes into that file. To create a file on the command line you can do something like: Code:
$ echo abc > 123.txt Code:
$ newvar=abc |
removed due to Mods option
|
Quote:
|
Quote:
I'm interested in seeing the response from the OP. The project appears to be a challenging, but do-able, task. As stated, we just need more information first. |
removed due to Mods option
|
Sorry, for not being clear. User will give a filename as input argument, that file(a notepad file .txt) will contain the contents as shown below
Date: 01-01-2007 CPU 3 7098 21294 Monitor 2 6500 13000 Mouse 20 300 6000 Total: 40294 --------------------------------------- Date: 02-01-2007 CPU 3 7099 21297 Monitor 2 6501 13002 Mouse 20 301 6020 Total: 40319 --------------------------------------- Now, the script should read the contents of the file and check for the date and create a new file and name it with that date. For example, here we have 01-01-2007 as the first date available, script should read this from the file and create a new file and then copy the contents(CPU, monitor and details mentioned above) till the dashed line. For 02-01-2007, script should again create a new file and will have the contents mentioned till the dashed line. so, like this for every date, separate file needs to be created and store the contents. I know to create a new file, i know to name it on today's date. But my question here is how to read a date from a notepad file and then create a new file on that date and then copy contents!! Thanks in advance for the help. |
removed due to Mods option
|
Quote:
|
removed due to Mods option
|
^ i was just being obtuse. op should use any mixture of text manipulation programs available since linux (at least bash scripting) was designed mostly for automated text manipulation.
also, op should date things according to yyyy-mm-dd so that it sorts better. |
removed due to Mods option
|
@BW-userx:
This thread is not for you to demonstrate or cite your coding knowledge or for you to judge the OPs needs and intentions. The OP has been given good advice on how to script and perhaps they should start and post what they have to gain some further assistance. |
Quote:
then I will remove all my advice and examples on how one could "code" in order to do what the OP needs to do what he requested, and let him go and search and let him try and figured out how to do what he needs to do by learning everything that others suggested he learn by drawing from their coding knowledge and with and without citing any examples. or for you (me) to judge the OPs needs and intentions. you judged what he needs and his intentions are along with everyone else that said, here this is what you (OP) need in order to do what he (OP) need(s) to do. but it is not for me to do, just you and others but not me. That's interesting... I've removed all of my advice and examples and links to try and help guild him to figuring out how to do what he needs to do in the most simplest and non time consuming manner. |
Quote:
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. |
All times are GMT -5. The time now is 03:07 AM. |