LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-14-2017, 06:09 AM   #1
rashmi88
LQ Newbie
 
Registered: Jun 2017
Posts: 6

Rep: Reputation: Disabled
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.
 
Old 06-14-2017, 06:37 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,061
Blog Entries: 13

Rep: Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504
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.
 
Old 06-14-2017, 09:28 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,155

Rep: Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951
removed due to Mods option

Last edited by BW-userx; 06-14-2017 at 05:18 PM.
 
Old 06-14-2017, 09:31 AM   #4
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,046

Rep: Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332Reputation: 1332
Quote:
Originally Posted by BW-userx View Post
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.
 
1 members found this post helpful.
Old 06-14-2017, 11:15 AM   #5
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,822

Rep: Reputation: 1293Reputation: 1293Reputation: 1293Reputation: 1293Reputation: 1293Reputation: 1293Reputation: 1293Reputation: 1293Reputation: 1293
Quote:
Originally Posted by wpeckham View Post
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.
 
Old 06-14-2017, 11:30 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,155

Rep: Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951
removed due to Mods option

Last edited by BW-userx; 06-14-2017 at 05:18 PM.
 
Old 06-14-2017, 11:41 AM   #7
rashmi88
LQ Newbie
 
Registered: Jun 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-14-2017, 11:51 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,155

Rep: Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951
removed due to Mods option

Last edited by BW-userx; 06-14-2017 at 05:18 PM.
 
Old 06-14-2017, 12:09 PM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,258

Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
Quote:
Originally Posted by rashmi88 View Post
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.
 
Old 06-14-2017, 12:14 PM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,155

Rep: Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951
removed due to Mods option

Last edited by BW-userx; 06-14-2017 at 05:18 PM.
 
Old 06-14-2017, 12:17 PM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,258

Rep: Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914Reputation: 914
^ 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.

Last edited by schneidz; 06-14-2017 at 12:18 PM.
 
Old 06-14-2017, 12:32 PM   #12
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,155

Rep: Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951
removed due to Mods option

Last edited by BW-userx; 06-14-2017 at 05:18 PM.
 
Old 06-14-2017, 05:03 PM   #13
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,061
Blog Entries: 13

Rep: Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504Reputation: 3504
@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.
 
Old 06-14-2017, 05:17 PM   #14
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,155

Rep: Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951Reputation: 1951
Quote:
Originally Posted by rtmistler View Post
@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.

Last edited by BW-userx; 06-14-2017 at 06:09 PM.
 
Old 06-15-2017, 12:09 AM   #15
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,176
Blog Entries: 3

Rep: Reputation: 2065Reputation: 2065Reputation: 2065Reputation: 2065Reputation: 2065Reputation: 2065Reputation: 2065Reputation: 2065Reputation: 2065Reputation: 2065Reputation: 2065
Quote:
Originally Posted by rashmi88 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: Keep original mod date of a file and 'date' it back to same file? SilversleevesX Programming 4 07-16-2010 11:12 AM
[SOLVED] Use date with touch to create new file with date based name craigjl Linux - Newbie 5 03-12-2010 09:46 AM
need to create awk sript for generating two files from a file checking a condition vandana.parwani Programming 3 03-03-2010 11:15 AM
Bash script to read file and alter lines depending on create date ChristianHein Linux - General 13 08-04-2007 05:39 AM
howto create a file based on date in bash rohan208 Linux - Newbie 2 05-07-2004 03:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration