LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Create new file in bash using date as condition (https://www.linuxquestions.org/questions/linux-newbie-8/create-new-file-in-bash-using-date-as-condition-4175607893/)

rashmi88 06-14-2017 06:09 AM

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.

rtmistler 06-14-2017 06:37 AM

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
And this will create a file named 123.txt and put the string abc into it. To go one step further, you can assign a variable to contain the string, "abc", and as I'm saying above, you can even do this directly in the command line:
Code:

$ newvar=abc
$ echo $newvar
abc
$ echo $newvar > 123.txt

Just some minor tips, you really will need to experiment a bit to get the results you wish for. I also suggest you start with a small input file where you keep a backup copy so that you can repeatedly try your initial scripts until you refine them enough to get where you wish it to be working.

BW-userx 06-14-2017 09:28 AM

removed due to Mods option

wpeckham 06-14-2017 09:31 AM

Quote:

Originally Posted by BW-userx (Post 5722641)
have written down or in your head gone through the steps in what is needed to be done to get the needed results yet?

Logic board (steps)

I recommend getting it on paper, with charts, graphs, or drawings if they seem to help. Every minute you save on design saved ten in coding, and hundreds in support and modification later.

scasey 06-14-2017 11:15 AM

Quote:

Originally Posted by wpeckham (Post 5722644)
I recommend getting it on paper, with charts, graphs, or drawings if they seem to help. Every minute you save on design saved ten in coding, and hundreds in support and modification later.

This is a tad off-topic, but I think wpeckham is being too conservative about the value of design, having spent years maintaining and modifying poorly designed applications. I think it's more like 1 minute in design saves hundreds in coding and thousands in support. Just sayin'

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.

BW-userx 06-14-2017 11:30 AM

removed due to Mods option

rashmi88 06-14-2017 11:41 AM

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.

BW-userx 06-14-2017 11:51 AM

removed due to Mods option

schneidz 06-14-2017 12:09 PM

Quote:

Originally Posted by rashmi88 (Post 5722688)
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.

sed, grep, cut, awk, paste, cat, tac, rev, tc... in that order.

BW-userx 06-14-2017 12:14 PM

removed due to Mods option

schneidz 06-14-2017 12:17 PM

^ 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.

BW-userx 06-14-2017 12:32 PM

removed due to Mods option

rtmistler 06-14-2017 05:03 PM

@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.

BW-userx 06-14-2017 05:17 PM

Quote:

Originally Posted by rtmistler (Post 5722783)
@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.

demonstrate or cite your (my) coding knowledge ?

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.

Turbocapitalist 06-15-2017 12:09 AM

Quote:

Originally Posted by rashmi88 (Post 5722604)
As I am very new to shell scripting, not sure how to start with this request.

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.

wpeckham 06-15-2017 07:23 AM

Quote:

Originally Posted by Turbocapitalist (Post 5722871)
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.
.

schneidz 06-15-2017 09:53 AM

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 ?

wpeckham 06-15-2017 10:50 AM

Quote:

Originally Posted by schneidz (Post 5723023)
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.

Turbocapitalist 06-15-2017 11:12 AM

Quote:

Originally Posted by schneidz (Post 5723023)
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

rashmi88 06-16-2017 06:06 AM

I needed it using awk, did not know it wasn't a part of bash. Below code worked as per need.
Thanks all.

awk -F' ' '{fn=$1".log";print>fn;close(fn)}' \FS='\n' OFS='\n' RS='---+\n' ORS='' filename

Turbocapitalist 06-16-2017 06:16 AM

Great. You could even trim the file names to just the date using sub()

Code:

awk -F' ' '{fn=$1".log";sub(/^.* /,"",fn);print>fn;close(fn)}' \FS='\n' OFS='\n' RS='---+\n' ORS='' filename
See "man awk" for the full manual.

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.

rashmi88 06-16-2017 10:31 PM

Thanks. But, i am looking for the file format to be <filename_YYYYMMDD.log>. Can anybody help on this please?

Turbocapitalist 06-16-2017 10:47 PM

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.

Shadow_7 06-17-2017 12:09 PM

Bash has length and string parts manipulation built in.

$ MYDATE="01-01-1970"; echo "MYDATE contains "$MYDATE" and has "${#MYDATE}" characters."; echo "YYYY="${MYDATE:6:4}" MM="${MYDATE:3:2}" DD="${MYDATE:0:2};

Shadow_7 06-17-2017 12:14 PM

Note to self, buy less tea. In that example the offsets assume a DD-MM-YYYY format. Apparently I woke up british or something this morning.

BW-userx 06-17-2017 12:29 PM

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?

or are you saying something else

http://www.theunixschool.com/2012/06...file-into.html

schneidz 06-17-2017 11:29 PM

Quote:

Originally Posted by rashmi88 (Post 5723642)
Thanks. But, i am looking for the file format to be <filename_YYYYMMDD.log>. Can anybody help on this please?

Code:

date +%Y-%j

wpeckham 06-18-2017 10:32 AM

Quote:

Originally Posted by schneidz (Post 5723951)
Code:

date +%Y-%j

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.

BW-userx 06-18-2017 01:37 PM

if you can use bash script then I got a solution for ya. but if it got a be awk then I'm out too, I do not use that.


All times are GMT -5. The time now is 03:51 AM.