LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-06-2019, 12:06 PM   #1
nartotik
LQ Newbie
 
Registered: Nov 2019
Posts: 2

Rep: Reputation: Disabled
Question BASH : how to sort bunch of .log files ?


hi guys.
im new to bash scripting so i need your help.
i got an assignment that im having trouble to deal with.

i got a bunch of .log files that some of them are "corrupt"
example for a "corrupt" log file:
2019-08-22T13:33:44.123456789 Hello
World
2020-01-01T11:22:33.123456789 late

i need to write a script that sort the .log files in a given path (also in the sub directories of the given path) and keep the content of the log file.

example on 3 log file:
1.log
2019-08-22T13:33:44.123456789 Hello
World
2020-01-01T11:22:33.123456789 late
2.log
2019-09-44T13:44:21.987654321 Simple line
3.log
2019-08-22T13:33:44.123456789 Hello
World 2
2020-01-01T11:22:33.123456789 late 2

the output needs to be:
‫‪2019-08-22T13:33:44.123456789‬‬ ‫‪Hello‬‬
‫‪World‬‬
‫‪2019-08-22T13:33:44.123456789‬‬ ‫‪Hello‬‬
‫‪World‬‬ ‫‪2‬‬
‫‪2019-09-44T13:44:21.987654321‬‬ ‫‪Simple‬‬ ‫‪line‬‬
‫‪2020-01-01T11:22:33.123456789‬‬ ‫‪late‬‬
‫‪2020-01-01T11:22:33.123456789‬‬ ‫‪late‬‬ ‫‪2‬‬
Code:
my script so far:
#!/bin/bash
path=$1
if cd $path;then
	output="$(find ${dir} -name "*.log" | xargs cat | tr -d '\0' | sort -n -k1,1 -k2,2 )"
	echo "${output}"
	(exit 0)
else
	(exit 2);
fi
thanks in advance for all the helpers!!

Last edited by nartotik; 11-06-2019 at 12:23 PM.
 
Old 11-06-2019, 12:20 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,861

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
1. please use [code]code tags[/code] to post code (and keep formatting).
2. please use shellcheck to check your script
3. I do not understand what is the difference between the good and corrupt files.
 
Old 11-06-2019, 12:30 PM   #3
nartotik
LQ Newbie
 
Registered: Nov 2019
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
1. please use [code]code tags[/code] to post code (and keep formatting).
2. please use shellcheck to check your script
3. I do not understand what is the difference between the good and corrupt files.
1. edited
2. used it , i dont have syntax errors, i dont understand how can i do it.
3. the difference is that good log files dosent contains random words in some lines so i cant use sort command on the merged log files as expected , if im using it it pushes the random words to the end
 
Old 11-06-2019, 12:42 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
well

I have no idea what your script is trying to do
Code:
#!/bin/bash
path=$1
# change dir, so for so good ( not *needed* but another story )
if cd $path;then
	output="$(find ${dir} -name "*.log" | xargs cat | tr -d '\0' | sort -n -k1,1 -k2,2 )"
        # you are concatenating all the logs, trimming and sorting and capturing
        # the contents into a variable 
	echo "${output}"
        # no you have printed the contents of all the logs to screen
	(exit 0)
        # started a subshell and exited that subshell
else
	(exit 2);
        # started a subshell and exited that subshell  
fi
basically it does nothing useful


please try to explain the problem better

I'm with @pan64 in not understanding the good/bad logs


in the meantime https://mywiki.wooledge.org/BashGuide
that should fix the many bad habits you demonstrate in your script
 
Old 11-06-2019, 01:42 PM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
had a little think

if the log is delimited by something other than a "space" it will need to be adapted


Code:
#!/bin/bash
path="$1"
while read line
do
  [[ ${line} =~ ^[0-9]{4}(-[0-9]{2}){2} ]] \
    && printf "\n" \
    || printf " "
  printf "%s" "${line}"
done < <( cat ${path%/}/*.log ) | sort -k1


this begins to add some checking
Code:
#!/bin/bash
path="$1"

errorreport () {
Ecode=$?
case $Ecode in
    0)  
        :
        ;;
    4)
        echo "no log supplied"
        ;;
    5)
        echo "$path is not a directory"
        ;;
    *)
        echo "unknown error"
esac
exit $Ecode
}
trap errorreport EXIT

[[ -z $path ]] && exit 4
[[ -d $path ]] || exit 5

while read line
do
  [[ ${line} =~ ^[0-9]{4}(-[0-9]{2}){2} ]] \
    && printf "\n" \
    || printf " "
  printf "%s" "${line}"
done < <( cat ${path%/}/*.log ) | sort -k1

edit,
the output is
Code:
2019-08-22T13:33:44.123456789 Hello World
2019-08-22T13:33:44.123456789 Hello World 2
2019-09-44T13:44:21.987654321 Simple line
2020-01-01T11:22:33.123456789 late
2020-01-01T11:22:33.123456789 late 2
Edit 2
if you need help with the field separators
either upload a raw sample of your logs
or the output of

Code:
cat -A sample.log
basically we can use the read to load each line into an array with its elements based on the field separator.
We can then use printf to "re-print" the lines with correct field separator

Last edited by Firerat; 11-06-2019 at 01:53 PM.
 
  


Reply



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
How to log internal-sftp chroot jailed users access log to /var/log/sftp.log file LittleMaster Linux - Server 0 09-04-2018 03:45 PM
How do I do filtering in Perl (keep sort order and sort again by another means)? RavenLX Programming 9 12-19-2008 10:12 AM
selection sort compiles but does not sort the array as desired ganesha Programming 2 04-20-2008 07:44 AM
bash: sorting a bunch of files into two halfs by size, and other tasks PatrickMay16 General 2 01-19-2008 01:04 AM
Is there a line limit with the sort utility? Trying to sort 130 million lines of text gruffy Linux - General 4 08-10-2006 08:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:10 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
Open Source Consulting | Domain Registration