LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-15-2017, 07:23 AM   #16
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,013

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301

Quote:
Originally Posted by Turbocapitalist View Post
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.
.

Last edited by wpeckham; 06-15-2017 at 07:25 AM.
 
Old 06-15-2017, 09:53 AM   #17
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 think by circular dependence he meant that it would be hard to draw a diagram without knowing the feature set of awk, for example ?
 
Old 06-15-2017, 10:50 AM   #18
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,013

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

Last edited by wpeckham; 06-15-2017 at 10:51 AM.
 
Old 06-15-2017, 11:12 AM   #19
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,112
Blog Entries: 3

Rep: Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013
Quote:
Originally Posted by schneidz View Post
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
 
Old 06-16-2017, 06:06 AM   #20
rashmi88
LQ Newbie
 
Registered: Jun 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-16-2017, 06:16 AM   #21
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,112
Blog Entries: 3

Rep: Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013
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.
 
Old 06-16-2017, 10:31 PM   #22
rashmi88
LQ Newbie
 
Registered: Jun 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks. But, i am looking for the file format to be <filename_YYYYMMDD.log>. Can anybody help on this please?
 
Old 06-16-2017, 10:47 PM   #23
Turbocapitalist
Senior Member
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 4,112
Blog Entries: 3

Rep: Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013
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.
 
Old 06-17-2017, 12:09 PM   #24
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 3,911
Blog Entries: 1

Rep: Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829
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};
 
Old 06-17-2017, 12:14 PM   #25
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 3,911
Blog Entries: 1

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

Rep: Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903Reputation: 1903
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

Last edited by BW-userx; 06-17-2017 at 02:01 PM.
 
Old 06-17-2017, 11:29 PM   #27
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
Thanks. But, i am looking for the file format to be <filename_YYYYMMDD.log>. Can anybody help on this please?
Code:
date +%Y-%j
 
Old 06-18-2017, 10:32 AM   #28
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,013

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by schneidz View Post
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.
 
Old 06-18-2017, 01:37 PM   #29
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,082

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


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 12:46 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