LinuxQuestions.org
Help answer threads with 0 replies.
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 09-24-2015, 03:33 PM   #1
Codfather1
LQ Newbie
 
Registered: Sep 2015
Posts: 3

Rep: Reputation: Disabled
Problem with Bash Loop dynamically


I am having problems with my script running correctly using Bash and what I want to do is this....

I have 3 sub-directories called /opt/log/NYclient, /opt/log/Patchlog and /opt/log/Systemlog. In each directory, there is a .log file. Now what I want to do is to cut the 2nd field and have the output going to files NYclient.log, Patchlog.log and Systemlog.log. Here is my script that seems to be erroring:

#!/bin/bash
for d in /opt/log/*; do
log="$(basename "$d").dat"
cut -d "," -f2 "$d"/*.dat > "$dat"
done

The files are located in /opt/log/NYclient/NYclient.log and the others follow the same. The script need to be dynamic as if I need to add additional.

Any help would be appreciated.

Last edited by Codfather1; 09-24-2015 at 03:35 PM.
 
Old 09-24-2015, 06:05 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,307

Rep: Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566
To start please use code tags when posting code, second you are not setting the 'dat' variable at the end of the 'cut' line, so redirection isn't going to work, also can you include examples of the log files, if they are too big just trim them to give a reasonable sample of the file.
 
Old 09-25-2015, 12:50 PM   #3
Codfather1
LQ Newbie
 
Registered: Sep 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Problem with Bash Loop Dynamically

Here is my log file that I'm cutting. I'm using the cut -d "," -f2 and that works if I just output it to a plain file but not what I'm trying to do. The $dat is not defined and any help would be appreciated. I'm new to the forum and this is my first posted. I'll try to keep in the correct format.

The 3 log files have this log information:

.064,clienta,15,316,20851,2347-1,104,
.065,clientb,15,316,15110,2347-2,104,
.064,clientc,15,316,20852,2347-1,104,
.069,clientd,15,316,27782,2347-1,104,
.064,cliente,15,316,10662,2347-4,104,

The $dat would actually have 3 separate files specified but I'm having troubles how to declare it.
 
Old 09-26-2015, 01:56 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,421

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
Ok, so the answer to $dat is you need to have a line somewhere that says:
Code:
dat=<file_name>
Or it if you need, it can be the entire path to a filename.

A few things on the code:

1. By doing the for loop in /var/log you will get a lot more than just those 3 directories

2. basename works fine, but another alternative is:
Code:
log=${d##*/}.dat
Looking at the code now, the log variable above is probably what you should be using in the redirection line

3. Do your logfiles actually end in '.dat'? Also, is there only one logfile in each location?
Code:
cut -d "," -f2 "$d"/*.dat > "$dat"
Hope some of that helps
 
Old 09-28-2015, 11:59 AM   #5
Codfather1
LQ Newbie
 
Registered: Sep 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Problem with Bash Loop Dynamically

Well I replaced my basename with your changes and it worked great. The only I will do is create separate directories and dump those files in the proper directories which I can do. Thank you very much as this was a learning experience. But I have one question:

log=${d##*/}.dat , what does the ## mean?

Thank you for everything.
 
Old 09-28-2015, 01:46 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,421

Rep: Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815Reputation: 2815
Search ## and %% on this page
 
  


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] Problem with for loop in bash linuxonbute Linux - Software 1 11-01-2014 05:59 AM
problem with loop bash ofer4 Linux - Newbie 10 04-07-2012 05:52 PM
Bash infinite loop problem? stp001 Linux - Newbie 6 03-15-2010 07:56 AM
bash while loop problem MadMusician_uk Programming 3 04-20-2006 12:52 PM
bash for loop problem deadlock Programming 5 09-04-2003 05:32 AM


All times are GMT -5. The time now is 06:04 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